Skip to content

Commit

Permalink
Merge pull request #344 from fabianbadoi/fix-343
Browse files Browse the repository at this point in the history
Fix issue #343 (Cyclomatic Complexity not reported correctly)
  • Loading branch information
UFOMelkor authored May 24, 2018
2 parents 8c609bc + 6303cb8 commit 7c2ec25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ public function leaveNode(Node $node)
// iterate over children, recursively
$cb = function ($node) use (&$cb) {
$ccn = 0;
if (isset($node->stmts) && $node->stmts) {
foreach ($node->stmts as $child) {
$ccn += $cb($child);

foreach (get_object_vars($node) as $name => $member) {
foreach (is_array($member) ? $member : [$member] as $member_item) {
if ($member_item instanceof Node) {
$ccn += $cb($member_item);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testCyclomaticComplexityOfMethodsIsWellCalculated($example, $cla
public function provideExamplesForClasses()
{
return [
[ __DIR__.'/../../examples/cyclomatic1.php', 'A', 4],
[ __DIR__.'/../../examples/cyclomatic1.php', 'A', 8],
[ __DIR__.'/../../examples/cyclomatic1.php', 'B', 5],
[ __DIR__.'/../../examples/cyclomatic_anon.php', 'Foo\C', 1],
];
Expand All @@ -62,7 +62,7 @@ public function provideExamplesForClasses()
public function provideExamplesForMethods()
{
return [
[ __DIR__.'/../../examples/cyclomatic1.php', 'A', 3],
[ __DIR__.'/../../examples/cyclomatic1.php', 'A', 6],
[ __DIR__.'/../../examples/cyclomatic1.php', 'B', 5],
];
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Metric/examples/cyclomatic1.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public function foo2()
{
if(true) {

} else {
if (true) {

} else if (true) {

} elseif (true) {

} elseif (true) {

}
}
}

Expand Down

0 comments on commit 7c2ec25

Please sign in to comment.