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

Encapsulate processed data #749

Merged
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
109 changes: 12 additions & 97 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ final class CodeCoverage
/**
* Code coverage data.
*
* @var array
* @var ProcessedCodeCoverageData
*/
private $data = [];
private $data;

/**
* @var array
Expand Down Expand Up @@ -151,6 +151,7 @@ public function __construct(Driver $driver = null, Filter $filter = null)
$this->filter = $filter;

$this->wizard = new Wizard;
$this->data = new ProcessedCodeCoverageData();
}

/**
Expand All @@ -172,7 +173,7 @@ public function clear(): void
{
$this->isInitialized = false;
$this->currentId = null;
$this->data = [];
$this->data = new ProcessedCodeCoverageData();
$this->tests = [];
$this->report = null;
}
Expand All @@ -188,7 +189,7 @@ public function filter(): Filter
/**
* Returns the collected code coverage data.
*/
public function getData(bool $raw = false): array
public function getData(bool $raw = false): ProcessedCodeCoverageData
{
if (!$raw && $this->addUncoveredFilesFromWhitelist) {
$this->addUncoveredFilesFromWhitelist();
Expand All @@ -200,7 +201,7 @@ public function getData(bool $raw = false): array
/**
* Sets the coverage data.
*/
public function setData(array $data): void
public function setData(ProcessedCodeCoverageData $data): void
{
$this->data = $data;
$this->report = null;
Expand Down Expand Up @@ -297,7 +298,7 @@ public function append(RawCodeCoverageData $rawData, $id = null, bool $append =

$this->applyWhitelistFilter($rawData);
$this->applyIgnoredLinesFilter($rawData);
$this->initializeFilesThatAreSeenTheFirstTime($rawData);
$this->data->initializeFilesThatAreSeenTheFirstTime($rawData);

if (!$append) {
return;
Expand Down Expand Up @@ -339,19 +340,7 @@ public function append(RawCodeCoverageData $rawData, $id = null, bool $append =

$this->tests[$id] = ['size' => $size, 'status' => $status];

foreach ($rawData->getLineCoverage() as $file => $lines) {
if (!$this->filter->isFile($file)) {
continue;
}

foreach ($lines as $k => $v) {
if ($v === Driver::LINE_EXECUTED) {
if (empty($this->data[$file][$k]) || !\in_array($id, $this->data[$file][$k])) {
$this->data[$file][$k][] = $id;
}
}
}
}
$this->data->markCodeAsExecutedByTestCase($id, $rawData);

$this->report = null;
}
Expand All @@ -367,36 +356,7 @@ public function merge(self $that): void
\array_merge($this->filter->getWhitelistedFiles(), $that->filter()->getWhitelistedFiles())
);

foreach ($that->data as $file => $lines) {
if (!isset($this->data[$file])) {
if (!$this->filter->isFiltered($file)) {
$this->data[$file] = $lines;
}

continue;
}

// we should compare the lines if any of two contains data
$compareLineNumbers = \array_unique(
\array_merge(
\array_keys($this->data[$file]),
\array_keys($that->data[$file])
)
);

foreach ($compareLineNumbers as $line) {
$thatPriority = $this->getLinePriority($that->data[$file], $line);
$thisPriority = $this->getLinePriority($this->data[$file], $line);

if ($thatPriority > $thisPriority) {
$this->data[$file][$line] = $that->data[$file][$line];
} elseif ($thatPriority === $thisPriority && \is_array($this->data[$file][$line])) {
$this->data[$file][$line] = \array_unique(
\array_merge($this->data[$file][$line], $that->data[$file][$line])
);
}
}
}
$this->data->merge($that->data);

$this->tests = \array_merge($this->tests, $that->getTests());
$this->report = null;
Expand Down Expand Up @@ -457,38 +417,6 @@ public function setUnintentionallyCoveredSubclassesWhitelist(array $whitelist):
$this->unintentionallyCoveredSubclassesWhitelist = $whitelist;
}

/**
* Determine the priority for a line
*
* 1 = the line is not set
* 2 = the line has not been tested
* 3 = the line is dead code
* 4 = the line has been tested
*
* During a merge, a higher number is better.
*
* @param array $data
* @param int $line
*
* @return int
*/
private function getLinePriority($data, $line)
{
if (!\array_key_exists($line, $data)) {
return 1;
}

if (\is_array($data[$line]) && \count($data[$line]) === 0) {
return 2;
}

if ($data[$line] === null) {
return 3;
}

return 4;
}

/**
* Applies the @covers annotation filtering.
*
Expand Down Expand Up @@ -559,19 +487,6 @@ private function applyIgnoredLinesFilter(RawCodeCoverageData $data): void
}
}

private function initializeFilesThatAreSeenTheFirstTime(RawCodeCoverageData $data): void
{
foreach ($data->getLineCoverage() as $file => $lines) {
if (!isset($this->data[$file]) && $this->filter->isFile($file)) {
$this->data[$file] = [];

foreach ($lines as $k => $v) {
$this->data[$file][$k] = $v === -2 ? null : [];
}
}
}
}

/**
* @throws CoveredCodeNotExecutedException
* @throws InvalidArgumentException
Expand All @@ -585,7 +500,7 @@ private function addUncoveredFilesFromWhitelist(): void
$data = [];
$uncoveredFiles = \array_diff(
$this->filter->getWhitelist(),
\array_keys($this->data)
\array_keys($this->data->getLineCoverage())
);

foreach ($uncoveredFiles as $uncoveredFile) {
Expand All @@ -602,7 +517,7 @@ private function addUncoveredFilesFromWhitelist(): void
}
}

$this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST');
$this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST');
}

private function getLinesToBeIgnored(string $fileName): array
Expand Down Expand Up @@ -972,7 +887,7 @@ private function initializeData(): void
$data[$file] = $fileCoverage;
}

$this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST');
$this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Driver/PCOV.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function stop(): RawCodeCoverageData

\pcov\clear();

return new RawCodeCoverageData($collect);
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($collect);
}
}
2 changes: 1 addition & 1 deletion src/Driver/PHPDBG.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function stop(): RawCodeCoverageData

$fetchedLines = \array_merge($fetchedLines, $sourceLines);

return new RawCodeCoverageData($this->detectExecutedLines($fetchedLines, $dbgData));
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($this->detectExecutedLines($fetchedLines, $dbgData));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public function stop(): RawCodeCoverageData

\xdebug_stop_code_coverage();

return new RawCodeCoverageData($data);
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data);
}
}
26 changes: 0 additions & 26 deletions src/Exception/UnknownCoverageDataFormatException.php

This file was deleted.

29 changes: 13 additions & 16 deletions src/Node/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
namespace SebastianBergmann\CodeCoverage\Node;

use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\ProcessedCodeCoverageData;

final class Builder
{
public function build(CodeCoverage $coverage): Directory
{
$files = $coverage->getData();
$commonPath = $this->reducePaths($files);
$data = clone $coverage->getData(); // clone because path munging is destructive to the original data
$commonPath = $this->reducePaths($data);
$root = new Directory(
$commonPath,
null
);

$this->addItems(
$root,
$this->buildDirectoryStructure($files),
$this->buildDirectoryStructure($data),
$coverage->getTests(),
$coverage->getCacheTokens()
);
Expand Down Expand Up @@ -90,8 +91,9 @@ private function addItems(Directory $root, array $items, array $tests, bool $cac
* )
* </code>
*/
private function buildDirectoryStructure(array $files): array
private function buildDirectoryStructure(ProcessedCodeCoverageData $data): array
{
$files = $data->getLineCoverage();
$result = [];

foreach ($files as $path => $file) {
Expand Down Expand Up @@ -152,20 +154,18 @@ private function buildDirectoryStructure(array $files): array
* )
* </code>
*/
private function reducePaths(array &$files): string
private function reducePaths(ProcessedCodeCoverageData $coverage): string
{
if (empty($files)) {
if (empty($coverage->getCoveredFiles())) {
return '.';
}

$commonPath = '';
$paths = \array_keys($files);
$paths = $coverage->getCoveredFiles();

if (\count($files) === 1) {
if (\count($paths) === 1) {
$commonPath = \dirname($paths[0]) . \DIRECTORY_SEPARATOR;
$files[\basename($paths[0])] = $files[$paths[0]];

unset($files[$paths[0]]);
$coverage->renameFile($paths[0], \basename($paths[0]));

return $commonPath;
}
Expand Down Expand Up @@ -212,16 +212,13 @@ private function reducePaths(array &$files): string
}
}

$original = \array_keys($files);
$original = $coverage->getCoveredFiles();
$max = \count($original);

for ($i = 0; $i < $max; $i++) {
$files[\implode(\DIRECTORY_SEPARATOR, $paths[$i])] = $files[$original[$i]];
unset($files[$original[$i]]);
$coverage->renameFile($original[$i], \implode(\DIRECTORY_SEPARATOR, $paths[$i]));
}

\ksort($files);

return \substr($commonPath, 0, -1);
}
}
Loading