Skip to content

Commit 3b8871e

Browse files
committed
fix nested array analysis
1 parent db61a96 commit 3b8871e

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PhpParser\Node;
1313
use PhpParser\Node\Expr\Array_;
14+
use PhpParser\Node\Expr\ArrayItem;
1415
use PhpParser\Node\Expr\ArrayDimFetch;
1516
use PhpParser\Node\Expr\Assign;
1617
use PhpParser\Node\Expr\BinaryOp;
@@ -250,7 +251,7 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
250251
}
251252

252253
if ($node instanceof Case_) {
253-
if (null === $node->cond) {
254+
if (null === $node->cond) { // default case
254255
return [];
255256
}
256257

@@ -290,6 +291,10 @@ private function getNodeStartLine(NodeAbstract $node): int
290291
return $node->getEndLine();
291292
}
292293

294+
if ($node->items[0] instanceof ArrayItem) {
295+
return $this->getNodeStartLine($node->items[0]->value);
296+
}
297+
293298
return $this->getNodeStartLine($node->items[0]);
294299
}
295300

tests/_files/source_with_multiline_constant_return.php

+43
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,47 @@ public function emptyMethodWithComment(): void
509509
{
510510
// empty method with comment
511511
}
512+
513+
public function simpleArrayConstArrayEmpty(): array
514+
{
515+
return
516+
[
517+
// empty array with comment
518+
];
519+
}
520+
521+
public function nestedArrayConstArrayEmpty(): array
522+
{
523+
return
524+
[
525+
[
526+
// empty subarray with comment
527+
]
528+
];
529+
}
530+
531+
public function nestedArrayConstArrayOne(): array
532+
{
533+
return
534+
[
535+
[
536+
<<<'EOF'
537+
1
538+
EOF,
539+
]
540+
];
541+
}
542+
543+
public function nestedArrayConstArrayTwo(): array
544+
{
545+
return
546+
[
547+
[
548+
<<<'EOF'
549+
1
550+
EOF,
551+
2,
552+
]
553+
];
554+
}
512555
}

tests/tests/Data/RawCodeCoverageDataTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): vo
533533
492,
534534
506,
535535
511,
536+
518,
537+
527,
538+
537,
539+
549,
536540
],
537541
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
538542
);

0 commit comments

Comments
 (0)