Skip to content
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
42 changes: 14 additions & 28 deletions src/Report/Xml/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,42 @@
namespace SebastianBergmann\CodeCoverage\Report\Xml;

use DOMElement;
use SebastianBergmann\CodeCoverage\ReportAlreadyFinalizedException;
use XMLWriter;

/**
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
*/
final class Coverage
{
private readonly XMLWriter $writer;
private readonly DOMElement $contextNode;
private bool $finalized = false;
private readonly string $line;

public function __construct(DOMElement $context, string $line)
{
$this->contextNode = $context;

$this->writer = new XMLWriter;
$this->writer->openMemory();
$this->writer->startElementNs(null, $context->nodeName, 'https://schema.phpunit.de/coverage/1.0');
$this->writer->writeAttribute('nr', $line);
$this->line = $line;
}

/**
* @throws ReportAlreadyFinalizedException
*/
public function addTest(string $test): void
public function finalize(array $tests): void
{
if ($this->finalized) {
// @codeCoverageIgnoreStart
throw new ReportAlreadyFinalizedException;
// @codeCoverageIgnoreEnd
$writer = new XMLWriter;
$writer->openMemory();
$writer->startElementNs(null, $this->contextNode->nodeName, 'https://schema.phpunit.de/coverage/1.0');
$writer->writeAttribute('nr', $this->line);

foreach ($tests as $test) {
$writer->startElement('covered');
$writer->writeAttribute('by', $test);
$writer->endElement();
}

$this->writer->startElement('covered');
$this->writer->writeAttribute('by', $test);
$this->writer->endElement();
}

public function finalize(): void
{
$this->writer->endElement();
$writer->endElement();

$fragment = $this->contextNode->ownerDocument->createDocumentFragment();
$fragment->appendXML($this->writer->outputMemory());
$fragment->appendXML($writer->outputMemory());

$this->contextNode->parentNode->replaceChild(
$fragment,
$this->contextNode,
);

$this->finalized = true;
}
}
7 changes: 1 addition & 6 deletions src/Report/Xml/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ private function processFile(FileNode $file, Directory $context): void
}

$coverage = $fileReport->lineCoverage((string) $line);

foreach ($tests as $test) {
$coverage->addTest($test);
}

$coverage->finalize();
$coverage->finalize($tests);
}

$fileReport->source()->setSourceCode(
Expand Down