Skip to content

Commit 64fddbe

Browse files
Merge branch '10.1'
2 parents 580d663 + 09c0fda commit 64fddbe

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public function enterNode(Node $node): void
105105
$node instanceof Node\Stmt\Use_ ||
106106
$node instanceof Node\Stmt\UseUse ||
107107
$node instanceof Node\Expr\ConstFetch ||
108-
$node instanceof Node\Expr\Match_ ||
109108
$node instanceof Node\Expr\Variable ||
110109
$node instanceof Node\Expr\Throw_ ||
111110
$node instanceof Node\ComplexType ||
@@ -117,6 +116,18 @@ public function enterNode(Node $node): void
117116
return;
118117
}
119118

119+
if ($node instanceof Node\Expr\Match_) {
120+
foreach ($node->arms as $arm) {
121+
$this->setLineBranch(
122+
$arm->body->getStartLine(),
123+
$arm->body->getEndLine(),
124+
++$this->nextBranch,
125+
);
126+
}
127+
128+
return;
129+
}
130+
120131
if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Throw_) {
121132
$this->setLineBranch($node->expr->expr->getEndLine(), $node->expr->expr->getEndLine(), ++$this->nextBranch);
122133

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
final class MatchExpr
3+
{
4+
public int $result;
5+
6+
public function __construct(int $value)
7+
{
8+
$this->result = match ($value) {
9+
0 => 4,
10+
1 => 5,
11+
2 => 6,
12+
3 => 7,
13+
default => 8,
14+
};
15+
}
16+
}

tests/tests/StaticAnalysis/ExecutableLinesFindingVisitorTest.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,56 @@
1717
use PhpParser\ParserFactory;
1818
use PHPUnit\Framework\Attributes\CoversClass;
1919
use PHPUnit\Framework\Attributes\RequiresPhp;
20+
use PHPUnit\Framework\Attributes\Ticket;
2021
use PHPUnit\Framework\TestCase;
2122

2223
#[CoversClass(ExecutableLinesFindingVisitor::class)]
2324
final class ExecutableLinesFindingVisitorTest extends TestCase
2425
{
2526
public function testExecutableLinesAreGroupedByBranch(): void
2627
{
27-
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines.php');
28+
$this->doTestSelfDescribingAssert(TEST_FILES_PATH . 'source_for_branched_exec_lines.php');
2829
}
2930

3031
#[RequiresPhp('>=8.1')]
3132
public function testExecutableLinesAreGroupedByBranchPhp81(): void
3233
{
33-
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php81.php');
34+
$this->doTestSelfDescribingAssert(TEST_FILES_PATH . 'source_for_branched_exec_lines_php81.php');
3435
}
3536

3637
#[RequiresPhp('>=8.2')]
3738
public function testExecutableLinesAreGroupedByBranchPhp82(): void
3839
{
39-
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php82.php');
40+
$this->doTestSelfDescribingAssert(TEST_FILES_PATH . 'source_for_branched_exec_lines_php82.php');
4041
}
4142

42-
private function doTestSelfDescribingAsset(string $filename): void
43+
#[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/967')]
44+
public function testMatchArmsAreProcessedCorrectly(): void
45+
{
46+
$source = file_get_contents(__DIR__ . '/../../_files/source_match_expression.php');
47+
$parser = (new ParserFactory)->createForHostVersion();
48+
$nodes = $parser->parse($source);
49+
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);
50+
51+
$traverser = new NodeTraverser;
52+
$traverser->addVisitor($executableLinesFindingVisitor);
53+
$traverser->traverse($nodes);
54+
55+
$this->assertSame(
56+
[
57+
8 => 2,
58+
9 => 3,
59+
10 => 4,
60+
11 => 5,
61+
12 => 6,
62+
13 => 7,
63+
14 => 2,
64+
],
65+
$executableLinesFindingVisitor->executableLinesGroupedByBranch(),
66+
);
67+
}
68+
69+
private function doTestSelfDescribingAssert(string $filename): void
4370
{
4471
$source = file_get_contents($filename);
4572
$parser = (new ParserFactory)->createForHostVersion();

0 commit comments

Comments
 (0)