diff --git a/src/StaticAnalysis/ExecutableLinesFindingVisitor.php b/src/StaticAnalysis/ExecutableLinesFindingVisitor.php index 4ffccc6be..6aeff42d8 100644 --- a/src/StaticAnalysis/ExecutableLinesFindingVisitor.php +++ b/src/StaticAnalysis/ExecutableLinesFindingVisitor.php @@ -172,9 +172,36 @@ private function getLines(Node $node): array return [$node->getEndLine()]; } - if ($node->items[0] instanceof ArrayItem) { - return [$node->items[0]->getStartLine()]; + $lines = []; + + foreach ($node->items as $index => $item) { + if (!$item instanceof ArrayItem) { + continue; + } + + if ($index === 0) { + $lines[] = $item->getStartLine(); + + continue; + } + + if ($item->key instanceof BinaryOp || + $item->value instanceof BinaryOp) { + continue; + } + + if ($item->key instanceof Node\Expr\Variable) { + $lines[] = $item->key->getStartLine(); + + continue; + } + + if ($item->value instanceof Node\Expr\Variable) { + $lines[] = $item->value->getStartLine(); + } } + + return $lines; } if ($node instanceof ClassMethod) { diff --git a/tests/_files/source_with_return_array_with_variable.php b/tests/_files/source_with_return_array_with_variable.php new file mode 100644 index 000000000..9e8e7fba7 --- /dev/null +++ b/tests/_files/source_with_return_array_with_variable.php @@ -0,0 +1,22 @@ + $number, + $name . '2' => $number + 2, + $number, + 'x' => [ + $name, + 2, + ] + ]; + } +} diff --git a/tests/tests/Data/RawCodeCoverageDataTest.php b/tests/tests/Data/RawCodeCoverageDataTest.php index edf38ea16..070365c14 100644 --- a/tests/tests/Data/RawCodeCoverageDataTest.php +++ b/tests/tests/Data/RawCodeCoverageDataTest.php @@ -333,7 +333,7 @@ public function testHeavyIndentationIsHandledCorrectly(): void ); } - public function testEmtpyConstructorIsMarkedAsExecutable(): void + public function testEmptyConstructorIsMarkedAsExecutable(): void { $file = TEST_FILES_PATH . 'source_with_empty_constructor.php'; @@ -384,6 +384,23 @@ public function testReturnStatementWithOnlyAnArrayWithScalarReturnsTheFirstEleme ); } + public function testReturnArrayWithVariable(): void + { + $file = TEST_FILES_PATH . 'source_with_return_array_with_variable.php'; + + $this->assertEquals( + [ + 8, + 9, + 12, + 13, + 15, + 17, + ], + array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file]) + ); + } + public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): void { $file = TEST_FILES_PATH . 'source_with_multiline_constant_return.php';