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

Store captured branch/path function data #754

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
29 changes: 21 additions & 8 deletions src/RawCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,43 @@ final class RawCodeCoverageData
*/
private $lineCoverage = [];

/**
* @var array
*
* @see https://xdebug.org/docs/code_coverage for format
*/
private $functionCoverage = [];

public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self
{
return new self($rawCoverage);
return new self($rawCoverage, []);
}

public static function fromXdebugWithPathCoverage(array $rawCoverage): self
{
$lineCoverage = [];
$lineCoverage = $functionCoverage = [];

foreach ($rawCoverage as $file => $fileCoverageData) {
$lineCoverage[$file] = $fileCoverageData['lines'];
if (isset($fileCoverageData['functions'])) {
$lineCoverage[$file] = $fileCoverageData['lines'];
$functionCoverage[$file] = $fileCoverageData['functions'];
} else { // not every file has functions, Xdebug outputs just line data for these
$lineCoverage[$file] = $fileCoverageData;
}
}

return new self($lineCoverage);
return new self($lineCoverage, $functionCoverage);
}

private function __construct(array $lineCoverage)
private function __construct(array $lineCoverage, array $functionCoverage)
{
$this->lineCoverage = $lineCoverage;
$this->lineCoverage = $lineCoverage;
$this->functionCoverage = $functionCoverage;
}

public function clear(): void
{
$this->lineCoverage = [];
$this->lineCoverage = $this->functionCoverage = [];

Choose a reason for hiding this comment

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

I do not like this and will change it two separate statements on separate lines after merging.

}

public function getLineCoverage(): array
Expand All @@ -54,7 +67,7 @@ public function getLineCoverage(): array

public function removeCoverageDataForFile(string $filename): void
{
unset($this->lineCoverage[$filename]);
unset($this->lineCoverage[$filename], $this->functionCoverage[$filename]);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/tests/RawCodeCoverageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function testLineDataFromStandardXDebugFormat(): void
}

/**
* In the path-coverage XDebug format, the line data exists inside a "lines" array key.
* In the path-coverage XDebug format, the line data exists inside a "lines" array key where the file has
* classes or functions. For files without them, the data is stored in the line-only format.
*/
public function testLineDataFromPathCoverageXDebugFormat(): void
{
Expand All @@ -45,6 +46,11 @@ public function testLineDataFromPathCoverageXDebugFormat(): void

],
],
'/some/path/justAScript.php' => [
18 => 1,
19 => -2,
113 => -1,
],
];

$lineData = [
Expand All @@ -53,6 +59,11 @@ public function testLineDataFromPathCoverageXDebugFormat(): void
9 => -2,
13 => -1,
],
'/some/path/justAScript.php' => [
18 => 1,
19 => -2,
113 => -1,
],
];

$dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);
Expand Down