Skip to content

Commit

Permalink
fix nested array analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Nov 11, 2022
1 parent db61a96 commit 3b8871e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp;
Expand Down Expand Up @@ -250,7 +251,7 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
}

if ($node instanceof Case_) {
if (null === $node->cond) {
if (null === $node->cond) { // default case
return [];
}

Expand Down Expand Up @@ -290,6 +291,10 @@ private function getNodeStartLine(NodeAbstract $node): int
return $node->getEndLine();
}

if ($node->items[0] instanceof ArrayItem) {
return $this->getNodeStartLine($node->items[0]->value);
}

return $this->getNodeStartLine($node->items[0]);
}

Expand Down
43 changes: 43 additions & 0 deletions tests/_files/source_with_multiline_constant_return.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,47 @@ public function emptyMethodWithComment(): void
{
// empty method with comment
}

public function simpleArrayConstArrayEmpty(): array
{
return
[
// empty array with comment
];
}

public function nestedArrayConstArrayEmpty(): array
{
return
[
[
// empty subarray with comment
]
];
}

public function nestedArrayConstArrayOne(): array
{
return
[
[
<<<'EOF'
1
EOF,
]
];
}

public function nestedArrayConstArrayTwo(): array
{
return
[
[
<<<'EOF'
1
EOF,
2,
]
];
}
}
4 changes: 4 additions & 0 deletions tests/tests/Data/RawCodeCoverageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): vo
492,
506,
511,
518,
527,
537,
549,
],
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
);
Expand Down

0 comments on commit 3b8871e

Please sign in to comment.