Skip to content

Commit ca65ac5

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

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,40 @@ 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 (!$item instanceof ArrayItem) {
179+
continue;
180+
}
181+
182+
if ($index === 0) {
183+
$lines[] = $item->getStartLine();
184+
185+
continue;
186+
}
187+
188+
if ($item->key instanceof BinaryOp ||
189+
$item->value instanceof BinaryOp) {
190+
continue;
191+
}
192+
193+
if ($item->key instanceof Node\Expr\Variable ||
194+
$item->key instanceof Node\Expr\ClassConstFetch
195+
) {
196+
$lines[] = $item->key->getStartLine();
197+
198+
continue;
199+
}
200+
201+
if ($item->value instanceof Node\Expr\Variable ||
202+
$item->value instanceof Node\Expr\ClassConstFetch
203+
) {
204+
$lines[] = $item->value->getStartLine();
205+
}
177206
}
207+
208+
return $lines;
178209
}
179210

180211
if ($node instanceof ClassMethod) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
'x' => [
20+
$name,
21+
2,
22+
self::BAR,
23+
]
24+
];
25+
}
26+
}

tests/tests/Data/RawCodeCoverageDataTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public function testHeavyIndentationIsHandledCorrectly(): void
333333
);
334334
}
335335

336-
public function testEmtpyConstructorIsMarkedAsExecutable(): void
336+
public function testEmptyConstructorIsMarkedAsExecutable(): void
337337
{
338338
$file = TEST_FILES_PATH . 'source_with_empty_constructor.php';
339339

@@ -384,6 +384,25 @@ 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+
20,
400+
22,
401+
],
402+
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
403+
);
404+
}
405+
387406
public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): void
388407
{
389408
$file = TEST_FILES_PATH . 'source_with_multiline_constant_return.php';

0 commit comments

Comments
 (0)