Skip to content
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

Force empty lines non executable #805

Merged
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
2 changes: 2 additions & 0 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public function filter(): Filter
*/
public function getData(bool $raw = false): ProcessedCodeCoverageData
{
$this->data->finalize();

if (!$raw) {
if ($this->processUncoveredFiles) {
$this->processUncoveredFilesFromFilter();
Expand Down
28 changes: 28 additions & 0 deletions src/ProcessedCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,34 @@ public function merge(self $newData): void
}
}

public function finalize(): void
{
$this->ensureEmptyLinesAreMarkedNonExecutable();
}

/**
* At the end of a file, the PHP interpreter always sees an implicit return. Where this occurs in a file that has
* e.g. a class definition, that line cannot be invoked from a test and results in confusing coverage. This engine
* implementation detail therefore needs to be masked which is done here by simply ensuring that all empty lines
* are considered non-executable for coverage purposes.
*
* @see https://github.com/sebastianbergmann/php-code-coverage/issues/799
*/
private function ensureEmptyLinesAreMarkedNonExecutable(): void
{
foreach ($this->lineCoverage as $filename => $coverage) {
if (is_file($filename)) {
$sourceLines = explode("\n", file_get_contents($filename));

foreach ($coverage as $line => $lineCoverage) {
if (is_array($lineCoverage) && trim($sourceLines[$line - 1]) === '') {
Copy link
Contributor Author

@dvdoug dvdoug Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The is_array isn't technically necessary, without it empty lines that are already non-executable are simply marked non-executable again. However I found it helpful for debugging to ensure that this only triggered on the cases I was expecting it to

$this->lineCoverage[$filename][$line] = null;
}
}
}
}
}

/**
* Determine the priority for a line.
*
Expand Down
62 changes: 31 additions & 31 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,48 +1122,48 @@ protected function getXdebugDataForNamespacedBankAccount()
return [
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
14 => 1,
15 => -2,
13 => 1,
14 => -2,
18 => -1,
19 => -1,
20 => -1,
21 => -1,
22 => -1,
24 => -1,
28 => -1,
30 => -1,
31 => -2,
35 => -1,
37 => -1,
38 => -2,
23 => -1,
27 => -1,
29 => -1,
30 => -2,
34 => -1,
36 => -1,
37 => -2,
],
]),
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
14 => 1,
19 => 1,
22 => 1,
35 => 1,
13 => 1,
18 => 1,
21 => 1,
34 => 1,
],
]),
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
14 => 1,
19 => 1,
22 => 1,
28 => 1,
13 => 1,
18 => 1,
21 => 1,
27 => 1,
],
]),
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'NamespacedBankAccount.php' => [
14 => 1,
13 => 1,
18 => 1,
19 => 1,
20 => 1,
21 => 1,
24 => 1,
28 => 1,
30 => 1,
35 => 1,
37 => 1,
23 => 1,
27 => 1,
29 => 1,
34 => 1,
36 => 1,
],
]),
];
Expand All @@ -1190,7 +1190,7 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage

$coverage->stop(
true,
[TEST_FILES_PATH . 'NamespacedBankAccount.php' => range(12, 15)]
[TEST_FILES_PATH . 'NamespacedBankAccount.php' => range(11, 14)]
);

$coverage->start(
Expand All @@ -1199,7 +1199,7 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage

$coverage->stop(
true,
[TEST_FILES_PATH . 'NamespacedBankAccount.php' => range(33, 38)]
[TEST_FILES_PATH . 'NamespacedBankAccount.php' => range(32, 37)]
);

$coverage->start(
Expand All @@ -1208,7 +1208,7 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage

$coverage->stop(
true,
[TEST_FILES_PATH . 'NamespacedBankAccount.php' => range(26, 31)]
[TEST_FILES_PATH . 'NamespacedBankAccount.php' => range(25, 30)]
);

$coverage->start(
Expand All @@ -1219,9 +1219,9 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage
true,
[
TEST_FILES_PATH . 'NamespacedBankAccount.php' => array_merge(
range(12, 15),
range(26, 31),
range(33, 38)
range(11, 14),
range(25, 30),
range(32, 37)
),
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/NamespacedBankAccount-text.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Code Coverage Report:
SomeNamespace\BankAccount
Methods: ( 0/ 0) Lines: ( 0/ 0)
SomeNamespace\BankAccountTrait
Methods: 75.00% ( 3/ 4) Lines: 55.56% ( 5/ 9)
Methods: 75.00% ( 3/ 4) Lines: 50.00% ( 5/ 10)