diff --git a/src/Analyser/ClassCollector.php b/src/Analyser/ClassCollector.php index c25f286..2e8387b 100644 --- a/src/Analyser/ClassCollector.php +++ b/src/Analyser/ClassCollector.php @@ -18,11 +18,11 @@ use PhpParser\Node\Expr\Include_; use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Expr\List_; -use PhpParser\Node\Expr\Match_; use PhpParser\Node\Expr\Print_; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\MatchArm; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Case_; @@ -569,7 +569,7 @@ private function isComplexityBranch(Node $node): bool || $node instanceof BooleanOr || $node instanceof LogicalAnd || $node instanceof LogicalOr - || $node instanceof Match_; + || $node instanceof MatchArm; } /** diff --git a/tests/Analyser/ClassCollectorTest.php b/tests/Analyser/ClassCollectorTest.php index 4a2d0ba..558bb0f 100644 --- a/tests/Analyser/ClassCollectorTest.php +++ b/tests/Analyser/ClassCollectorTest.php @@ -853,6 +853,63 @@ public function second($y): int { $this->assertSame(4, $classNode->methods[0]->cyclomaticComplexity); } + public function testCountsEachMatchArmAsComplexityBranch(): void + { + $code = <<<'PHP' + 'a', + 2 => 'b', + 3 => 'c', + default => 'd', + }; + } +} +PHP; + $classNode = $this->collect($code); + + // Base 1 + 4 match arms = 5. + $this->assertSame(5, $classNode->methods[0]->cyclomaticComplexity); + } + + public function testMatchAndEquivalentSwitchHaveEqualComplexity(): void + { + $matchCode = <<<'PHP' + 'a', + 2 => 'b', + default => 'c', + }; + } +} +PHP; + $switchCode = <<<'PHP' +assertSame( + $this->collect($switchCode)->methods[0]->cyclomaticComplexity, + $this->collect($matchCode)->methods[0]->cyclomaticComplexity, + ); + } + public function testCollectsDependencies(): void { $code = <<<'PHP'