-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the calculation of cyclomatic complexity
- xor has not been covered yet. - The ternary operator and the coalesce operator has been count both as 2. Also the default case of a switch statement has been counted. But as the else statement is not counted because it represents the default path (and can be elimated), the default case should not be counted (corresponds to else) and both the ternary and the coalesce operator should be counted as 1, because they can be exchanged by if-else / if which would be counted as 1. The spaceship operator by contrast is now counted as 2, because it would need if-elseif-else to replace it. - continue does not increase the cyclomatic complexity as its no decision. It's the if around the continue which increases the ccn.
- Loading branch information
Showing
3 changed files
with
95 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
class SwitchCase // ccn2: 4 | ||
{ | ||
function __invoke() | ||
{ | ||
switch ('abc') { | ||
case 'abc': | ||
case 'def': | ||
case 'hij': | ||
break; | ||
default: | ||
} | ||
} | ||
} | ||
|
||
class IfElseif // ccn2: 7 | ||
{ | ||
function __invoke() | ||
{ | ||
if (true) { | ||
if (true) { | ||
} elseif (true) { | ||
} else { | ||
} | ||
} elseif (true) { | ||
if (false) { | ||
} | ||
} | ||
|
||
if (true) { | ||
} | ||
} | ||
} | ||
|
||
class Loops // ccn2: 5 | ||
{ | ||
function __invoke() | ||
{ | ||
while (true) { | ||
do { | ||
} while (false); | ||
} | ||
foreach (array() as $each) { | ||
for ($i = 0; $i < 0; ++$i) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
class CatchIt // ccn2: 3 | ||
{ | ||
function __invoke() | ||
{ | ||
try { | ||
} catch (Exception $e) { | ||
} catch (Throwable $e) { | ||
} finally { | ||
} | ||
} | ||
} | ||
|
||
class Logical // ccn2: 11 | ||
{ | ||
function __invoke() | ||
{ | ||
$a = (true || false) and (false && true) or (true xor false); | ||
$b = $a ? 1 : 2; | ||
$c = $b ?: 0; | ||
$d = $b ?? $c; | ||
$e = $b <=> $d; | ||
} | ||
} |