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

Empty lines not executable v2 #807

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
79 changes: 31 additions & 48 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ final class CodeCoverage
*/
private $parentClassesExcludedFromUnintentionallyCoveredCodeCheck = [];

/**
* Determine if the data has been initialized or not.
*
* @var bool
*/
private $isInitialized = false;

/**
* @var ?CoveredFileAnalyser
*/
Expand Down Expand Up @@ -151,10 +144,9 @@ public function getReport(): Directory
*/
public function clear(): void
{
$this->isInitialized = false;
$this->currentId = null;
$this->data = new ProcessedCodeCoverageData;
$this->tests = [];
$this->currentId = null;
$this->data = new ProcessedCodeCoverageData;
$this->tests = [];
}

/**
Expand All @@ -170,8 +162,12 @@ public function filter(): Filter
*/
public function getData(bool $raw = false): ProcessedCodeCoverageData
{
if (!$raw && $this->includeUncoveredFiles) {
$this->addUncoveredFilesFromFilter();
if (!$raw) {
if ($this->processUncoveredFiles) {
$this->processUncoveredFilesFromFilter();
} elseif ($this->includeUncoveredFiles) {
$this->addUncoveredFilesFromFilter();
}
}

return $this->data;
Expand Down Expand Up @@ -212,10 +208,6 @@ public function start($id, bool $clear = false): void
$this->clear();
}

if ($this->isInitialized === false) {
$this->initializeData();
}

$this->currentId = $id;

$this->driver->start();
Expand Down Expand Up @@ -510,7 +502,7 @@ private function addUncoveredFilesFromFilter(): void
{
$uncoveredFiles = array_diff(
$this->filter->files(),
array_keys($this->data->lineCoverage())
$this->data->coveredFiles()
);

foreach ($uncoveredFiles as $uncoveredFile) {
Expand All @@ -526,6 +518,27 @@ private function addUncoveredFilesFromFilter(): void
}
}

/**
* @throws UnintentionallyCoveredCodeException
*/
private function processUncoveredFilesFromFilter(): void
{
$uncoveredFiles = array_diff(
$this->filter->files(),
$this->data->coveredFiles()
);

$this->driver->start();

foreach ($uncoveredFiles as $uncoveredFile) {
if (file_exists($uncoveredFile)) {
include_once $uncoveredFile;
}
}

$this->append($this->driver->stop(), self::UNCOVERED_FILES);
}

/**
* @throws UnintentionallyCoveredCodeException
* @throws ReflectionException
Expand Down Expand Up @@ -628,36 +641,6 @@ private function processUnintentionallyCoveredUnits(array $unintentionallyCovere
return array_values($unintentionallyCoveredUnits);
}

/**
* @throws UnintentionallyCoveredCodeException
*/
private function initializeData(): void
{
$this->isInitialized = true;

if ($this->processUncoveredFiles) {
// by collecting dead code data here on an initial pass, future runs with test data do not need to
if ($this->driver->canDetectDeadCode()) {
$this->driver->enableDeadCodeDetection();
}

$this->driver->start();

foreach ($this->filter->files() as $file) {
if ($this->filter->isFile($file)) {
include_once $file;
}
}

// having now collected dead code for the entire list of files, we can safely skip this data on subsequent runs
if ($this->driver->canDetectDeadCode()) {
$this->driver->disableDeadCodeDetection();
}

$this->append($this->driver->stop(), self::UNCOVERED_FILES);
}
}

private function coveredFileAnalyser(): CoveredFileAnalyser
{
if ($this->coveredFileAnalyser !== null) {
Expand Down
43 changes: 43 additions & 0 deletions src/RawCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
final class RawCodeCoverageData
{
/**
* @var array<string, array<int>>
*/
private static $emptyLineCache = [];

/**
* @var array
*
Expand Down Expand Up @@ -94,6 +99,8 @@ private function __construct(array $lineCoverage, array $functionCoverage)
{
$this->lineCoverage = $lineCoverage;
$this->functionCoverage = $functionCoverage;

$this->skipEmptyLines();
}

public function clear(): void
Expand Down Expand Up @@ -181,4 +188,40 @@ public function removeCoverageDataForLines(string $filename, array $lines): void
}
}
}

/**
* 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 skipped over for coverage purposes.
*
* @see https://github.com/sebastianbergmann/php-code-coverage/issues/799
*/
private function skipEmptyLines(): void
{
foreach ($this->lineCoverage as $filename => $coverage) {
foreach ($this->getEmptyLinesForFile($filename) as $emptyLine) {
unset($this->lineCoverage[$filename][$emptyLine]);
}
}
}

private function getEmptyLinesForFile(string $filename): array
{
if (!isset(self::$emptyLineCache[$filename])) {
self::$emptyLineCache[$filename] = [];

if (is_file($filename)) {
$sourceLines = explode("\n", file_get_contents($filename));

foreach ($sourceLines as $line => $source) {
if (trim($source) === '') {
self::$emptyLineCache[$filename][] = ($line + 1);
}
}
}
}

return self::$emptyLineCache[$filename];
}
}
1 change: 0 additions & 1 deletion tests/tests/RawCodeCoverageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ public function testCoverageForFileWithInlineAnnotations(): void
26 => -1,
35 => -1,
36 => -1,
37 => -1,
],
$coverage->lineCoverage()[$filename]
);
Expand Down