Skip to content

Commit

Permalink
Merge branch '10.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 29, 2024
2 parents 580d663 + 09c0fda commit 64fddbe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public function enterNode(Node $node): void
$node instanceof Node\Stmt\Use_ ||
$node instanceof Node\Stmt\UseUse ||
$node instanceof Node\Expr\ConstFetch ||
$node instanceof Node\Expr\Match_ ||
$node instanceof Node\Expr\Variable ||
$node instanceof Node\Expr\Throw_ ||
$node instanceof Node\ComplexType ||
Expand All @@ -117,6 +116,18 @@ public function enterNode(Node $node): void
return;
}

if ($node instanceof Node\Expr\Match_) {
foreach ($node->arms as $arm) {
$this->setLineBranch(
$arm->body->getStartLine(),
$arm->body->getEndLine(),
++$this->nextBranch,
);
}

return;
}

if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Throw_) {
$this->setLineBranch($node->expr->expr->getEndLine(), $node->expr->expr->getEndLine(), ++$this->nextBranch);

Expand Down
16 changes: 16 additions & 0 deletions tests/_files/source_match_expression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
final class MatchExpr
{
public int $result;

public function __construct(int $value)
{
$this->result = match ($value) {
0 => 4,
1 => 5,
2 => 6,
3 => 7,
default => 8,
};
}
}
35 changes: 31 additions & 4 deletions tests/tests/StaticAnalysis/ExecutableLinesFindingVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,56 @@
use PhpParser\ParserFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\Ticket;
use PHPUnit\Framework\TestCase;

#[CoversClass(ExecutableLinesFindingVisitor::class)]
final class ExecutableLinesFindingVisitorTest extends TestCase
{
public function testExecutableLinesAreGroupedByBranch(): void
{
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines.php');
$this->doTestSelfDescribingAssert(TEST_FILES_PATH . 'source_for_branched_exec_lines.php');
}

#[RequiresPhp('>=8.1')]
public function testExecutableLinesAreGroupedByBranchPhp81(): void
{
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php81.php');
$this->doTestSelfDescribingAssert(TEST_FILES_PATH . 'source_for_branched_exec_lines_php81.php');
}

#[RequiresPhp('>=8.2')]
public function testExecutableLinesAreGroupedByBranchPhp82(): void
{
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php82.php');
$this->doTestSelfDescribingAssert(TEST_FILES_PATH . 'source_for_branched_exec_lines_php82.php');
}

private function doTestSelfDescribingAsset(string $filename): void
#[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/967')]
public function testMatchArmsAreProcessedCorrectly(): void
{
$source = file_get_contents(__DIR__ . '/../../_files/source_match_expression.php');
$parser = (new ParserFactory)->createForHostVersion();
$nodes = $parser->parse($source);
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);

$traverser = new NodeTraverser;
$traverser->addVisitor($executableLinesFindingVisitor);
$traverser->traverse($nodes);

$this->assertSame(
[
8 => 2,
9 => 3,
10 => 4,
11 => 5,
12 => 6,
13 => 7,
14 => 2,
],
$executableLinesFindingVisitor->executableLinesGroupedByBranch(),
);
}

private function doTestSelfDescribingAssert(string $filename): void
{
$source = file_get_contents($filename);
$parser = (new ParserFactory)->createForHostVersion();
Expand Down

0 comments on commit 64fddbe

Please sign in to comment.