Skip to content

Store processed branch/path function data #755

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

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
14 changes: 14 additions & 0 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ abstract class Driver
*/
public const LINE_NOT_EXECUTABLE = -2;

/**
* @var int
*
* @see http://xdebug.org/docs/code_coverage
*/
public const BRANCH_HIT = 1;

/**
* @var int
*
* @see http://xdebug.org/docs/code_coverage
*/
public const BRANCH_NOT_HIT = 0;

/**
* @var bool
*/
Expand Down
76 changes: 75 additions & 1 deletion src/ProcessedCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ final class ProcessedCodeCoverageData
*/
private $lineCoverage = [];

/**
* Function coverage data.
* Maintains base format of raw data (@see https://xdebug.org/docs/code_coverage), but each 'hit' entry is an array
* of testcase ids
*
* @var array
*/
private $functionCoverage = [];

public function initializeFilesThatAreSeenTheFirstTime(RawCodeCoverageData $rawData): void
{
foreach ($rawData->getLineCoverage() as $file => $lines) {
Expand All @@ -35,6 +44,22 @@ public function initializeFilesThatAreSeenTheFirstTime(RawCodeCoverageData $rawD
}
}
}

foreach ($rawData->getFunctionCoverage() as $file => $functions) {
if (!isset($this->functionCoverage[$file])) {
$this->functionCoverage[$file] = $functions;

foreach ($this->functionCoverage[$file] as $functionName => $functionData) {
foreach (\array_keys($this->functionCoverage[$file][$functionName]['branches']) as $branchId) {
$this->functionCoverage[$file][$functionName]['branches'][$branchId]['hit'] = [];
}

foreach (\array_keys($this->functionCoverage[$file][$functionName]['paths']) as $pathId) {
$this->functionCoverage[$file][$functionName]['paths'][$pathId]['hit'] = [];
}
}
}
}
}

public function markCodeAsExecutedByTestCase(string $testCaseId, RawCodeCoverageData $executedCode): void
Expand All @@ -48,6 +73,26 @@ public function markCodeAsExecutedByTestCase(string $testCaseId, RawCodeCoverage
}
}
}

foreach ($executedCode->getFunctionCoverage() as $file => $functions) {
foreach ($functions as $functionName => $functionData) {
foreach ($functionData['branches'] as $branchId => $branchData) {
if ($branchData['hit'] === Driver::BRANCH_HIT) {
if (!\in_array($testCaseId, $this->functionCoverage[$file][$functionName]['branches'][$branchId]['hit'], true)) {
$this->functionCoverage[$file][$functionName]['branches'][$branchId]['hit'][] = $testCaseId;
}
}
}

foreach ($functionData['paths'] as $pathId => $pathData) {
if ($pathData['hit'] === Driver::BRANCH_HIT) {
if (!\in_array($testCaseId, $this->functionCoverage[$file][$functionName]['paths'][$pathId]['hit'], true)) {
$this->functionCoverage[$file][$functionName]['paths'][$pathId]['hit'][] = $testCaseId;
}
}
}
}
}
}

public function setLineCoverage(array $lineCoverage): void
Expand All @@ -62,6 +107,13 @@ public function getLineCoverage(): array
return $this->lineCoverage;
}

public function getFunctionCoverage(): array
{
\ksort($this->functionCoverage);

return $this->functionCoverage;
}

public function getCoveredFiles(): array
{
return \array_keys($this->lineCoverage);
Expand All @@ -70,7 +122,11 @@ public function getCoveredFiles(): array
public function renameFile(string $oldFile, string $newFile): void
{
$this->lineCoverage[$newFile] = $this->lineCoverage[$oldFile];
unset($this->lineCoverage[$oldFile]);

if (isset($this->functionCoverage[$oldFile])) {
$this->functionCoverage[$newFile] = $this->functionCoverage[$oldFile];
}
unset($this->lineCoverage[$oldFile], $this->functionCoverage[$oldFile]);
}

public function merge(self $newData): void
Expand Down Expand Up @@ -103,6 +159,24 @@ public function merge(self $newData): void
}
}
}

foreach ($newData->functionCoverage as $file => $functions) {
if (!isset($this->functionCoverage[$file])) {
$this->functionCoverage[$file] = $functions;

continue;
}

foreach ($functions as $functionName => $functionData) {
foreach ($functionData['branches'] as $branchId => $branchData) {
$this->functionCoverage[$file][$functionName]['branches'][$branchId]['hit'] = \array_unique(\array_merge($this->functionCoverage[$file][$functionName]['branches'][$branchId]['hit'], $branchData['hit']));
}

foreach ($functionData['paths'] as $pathId => $pathData) {
$this->functionCoverage[$file][$functionName]['paths'][$pathId]['hit'] = \array_unique(\array_merge($this->functionCoverage[$file][$functionName]['paths'][$pathId]['hit'], $pathData['hit']));
}
}
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/RawCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function getLineCoverage(): array
return $this->lineCoverage;
}

public function getFunctionCoverage(): array
{
return $this->functionCoverage;
}

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