Skip to content

Commit df0b90f

Browse files
committed
Fix return with multiline variable in array and with expr not be coverage
1 parent 2fde509 commit df0b90f

4 files changed

+76
-2
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,43 @@ private function getLines(Node $node): array
172172
return [$node->getEndLine()];
173173
}
174174

175-
if ($node->items[0] instanceof ArrayItem) {
176-
return [$node->items[0]->getStartLine()];
175+
$lines = [];
176+
177+
foreach ($node->items as $index => $item) {
178+
if ($index === 0) {
179+
$lines[] = $item->getStartLine();
180+
181+
continue;
182+
}
183+
184+
if ($item->key instanceof BinaryOp || $item->value instanceof BinaryOp) {
185+
continue;
186+
}
187+
188+
if ($item->key instanceof Node\Expr\Variable ||
189+
$item->key instanceof Node\Expr\ClassConstFetch
190+
) {
191+
$lines[] = $item->key->getStartLine();
192+
193+
continue;
194+
}
195+
196+
if ($item->value instanceof Node\Expr\Variable ||
197+
$item->value instanceof Node\Expr\ClassConstFetch
198+
) {
199+
$lines[] = $item->value->getStartLine();
200+
201+
continue;
202+
}
203+
}
204+
205+
return $lines;
206+
}
207+
208+
if ($node instanceof ArrayItem) {
209+
if (($node->left instanceof Node\Expr\Variable && !$node->right instanceof Node\Expr\BinaryOp) ||
210+
($node->right instanceof Node\Expr\Variable && !$node->left instanceof Node\Expr\BinaryOp)) {
211+
return [$node->left->getStartLine()];
177212
}
178213
}
179214

tests/_files/source_with_multiline_constant_return.php

+1
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,5 @@ public function Spaceship(): int
244244
1
245245
;
246246
}
247+
247248
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace SebastianBergmann\CodeCoverage\TestFixture;
3+
4+
class Foo
5+
{
6+
private const BAR = 'bar';
7+
8+
public function one(): array
9+
{
10+
$name = 'Rain';
11+
$number = 2;
12+
return [
13+
$name,
14+
$name => $number,
15+
$name . '2' => $number + 2,
16+
$number,
17+
self::BAR,
18+
self::BAR .'2',
19+
];
20+
}
21+
}

tests/tests/Data/RawCodeCoverageDataTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,23 @@ public function testReturnStatementWithOnlyAnArrayWithScalarReturnsTheFirstEleme
384384
);
385385
}
386386

387+
public function testReturnArrayWithVariableOrClassConst(): void
388+
{
389+
$file = TEST_FILES_PATH . 'source_with_return_array_with_variable_or_class_const.php';
390+
391+
$this->assertEquals(
392+
[
393+
10,
394+
11,
395+
13,
396+
14,
397+
16,
398+
17,
399+
],
400+
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
401+
);
402+
}
403+
387404
public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): void
388405
{
389406
$file = TEST_FILES_PATH . 'source_with_multiline_constant_return.php';

0 commit comments

Comments
 (0)