Skip to content

Fix multiline variable in array and with expr not be coverage #955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions tests/_files/source_with_return_array_with_variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace SebastianBergmann\CodeCoverage\TestFixture;

class Foo
{
public function one(): array
{
$name = 'Rain';
$number = 2;

return [
$name,
$name => $number,
$name . '2' => $number + 2,
$number,
'x' => [
$name,
2,
]
];
}
}
19 changes: 18 additions & 1 deletion tests/tests/Data/RawCodeCoverageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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';
Expand Down