Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Analyser/ClassCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/Analyser/ClassCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,63 @@ public function second($y): int {
$this->assertSame(4, $classNode->methods[0]->cyclomaticComplexity);
}

public function testCountsEachMatchArmAsComplexityBranch(): void
{
$code = <<<'PHP'
<?php
class Foo {
public function bar(int $x): string {
return match ($x) {
1 => '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'
<?php
class Foo {
public function bar(int $x): string {
return match ($x) {
1 => 'a',
2 => 'b',
default => 'c',
};
}
}
PHP;
$switchCode = <<<'PHP'
<?php
class Foo {
public function bar(int $x): string {
switch ($x) {
case 1:
return 'a';
case 2:
return 'b';
default:
return 'c';
}
}
}
PHP;

$this->assertSame(
$this->collect($switchCode)->methods[0]->cyclomaticComplexity,
$this->collect($matchCode)->methods[0]->cyclomaticComplexity,
);
}

public function testCollectsDependencies(): void
{
$code = <<<'PHP'
Expand Down
Loading