Skip to content
Closed
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
9 changes: 9 additions & 0 deletions src/StaticAnalysis/Visitor/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ public function enterNode(Node $node): null
return null;
}

if ($node instanceof Node\Stmt\Expression &&
(
$node->expr instanceof Node\Scalar ||
$node->expr instanceof Node\Expr\ConstFetch
)
) {
return null;
}

if ($node instanceof Node\Stmt\Enum_ ||
$node instanceof Node\Stmt\Function_ ||
$node instanceof Node\Stmt\Class_ ||
Expand Down
16 changes: 16 additions & 0 deletions tests/_files/source_with_scalar_literals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
final class ScalarLiterals
{
public static function testFunction()
{
'a string';
null;
$x = 5;
true;
false;
$x += 1;
1;
1.1;
return $x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ public function testMatchTrueDoesNotMarkOpenerAndCloserAsExecutable(): void
$this->assertArrayHasKey(12, $executableLines);
}

#[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/1156')]
public function testScalarLiteralsNotMarkedAsExecutable(): void
{
$source = file_get_contents(__DIR__ . '/../../../_files/source_with_scalar_literals.php');
$parser = (new ParserFactory)->createForHostVersion();
$nodes = $parser->parse($source);
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);

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

$executableLines = $executableLinesFindingVisitor->executableLinesGroupedByBranch();

$this->assertArrayNotHasKey(6, $executableLines);
$this->assertArrayNotHasKey(7, $executableLines);
$this->assertArrayNotHasKey(9, $executableLines);
$this->assertArrayNotHasKey(10, $executableLines);
$this->assertArrayNotHasKey(12, $executableLines);
$this->assertArrayNotHasKey(13, $executableLines);
$this->assertArrayHasKey(8, $executableLines);
$this->assertArrayHasKey(11, $executableLines);
$this->assertArrayHasKey(14, $executableLines);
}

private function doTestSelfDescribingAssert(string $filename): void
{
$source = file_get_contents($filename);
Expand Down