Skip to content

Commit 57a6c1c

Browse files
Slamdunksebastianbergmann
authored andcommitted
Multiline strings: skip internal lines
1 parent 3cdf791 commit 57a6c1c

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

+15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public function __construct(string $source)
5454

5555
public function enterNode(Node $node): void
5656
{
57+
if ($node instanceof Node\Scalar\String_ ||
58+
$node instanceof Node\Scalar\EncapsedStringPart
59+
) {
60+
$startLine = $node->getStartLine() + 1;
61+
$endLine = $node->getEndLine() - 1;
62+
63+
if ($startLine <= $endLine) {
64+
foreach (range($startLine, $endLine) as $line) {
65+
unset($this->executableLinesGroupedByBranch[$line]);
66+
}
67+
}
68+
69+
return;
70+
}
71+
5772
if ($node instanceof Node\Stmt\Declare_ ||
5873
$node instanceof Node\Stmt\DeclareDeclare ||
5974
$node instanceof Node\Stmt\Else_ ||

tests/_files/source_for_branched_exec_lines.php

+32
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,38 @@ public function withEarlyReturns()
506506
return; // +1
507507
$var = 2; // +1
508508
}
509+
public function withMultilineStrings()
510+
{
511+
$var = 1; // +1
512+
$singleQuote = // +1
513+
'start // 0
514+
a
515+
$var
516+
z
517+
end'; // 0
518+
$doubleQuote = // +1
519+
"start // 0
520+
a
521+
$var // 0
522+
z
523+
end"; // 0
524+
$nowDoc = // +1
525+
<<<'LINE_ADDED_IN_TEST'
526+
start
527+
a
528+
$var
529+
z
530+
end
531+
LINE_ADDED_IN_TEST; // 0
532+
$hereDoc = // +1
533+
<<<LINE_ADDED_IN_TEST
534+
start // 0
535+
a
536+
$var // 0
537+
z
538+
end
539+
LINE_ADDED_IN_TEST; // 0
540+
}
509541
}
510542

511543
interface MyInterface

tests/tests/StaticAnalysis/ExecutableLinesFindingVisitorTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function explode;
1313
use function file_get_contents;
1414
use function preg_match;
15+
use function strpos;
1516
use PhpParser\Lexer;
1617
use PhpParser\NodeTraverser;
1718
use PhpParser\ParserFactory;
@@ -65,6 +66,12 @@ private function doTestSelfDescribingAsset(string $filename): void
6566
$branch = 0;
6667

6768
foreach ($linesFromSource as $lineNumber => $line) {
69+
if (false !== strpos($line, 'LINE_ADDED_IN_TEST')) {
70+
$expected[1 + $lineNumber] = $branch;
71+
72+
continue;
73+
}
74+
6875
if (1 !== preg_match('#^\s*[^/].+// (?<branchIncrement>[+-]?\d+)$#', $line, $matches)) {
6976
continue;
7077
}

0 commit comments

Comments
 (0)