1<?php 2/* 3 * This file is part of the php-code-coverage package. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11namespace SebastianBergmann\CodeCoverage\Report\Xml; 12 13use SebastianBergmann\CodeCoverage\RuntimeException; 14 15class Coverage 16{ 17 /** 18 * @var \XMLWriter 19 */ 20 private $writer; 21 22 /** 23 * @var \DOMElement 24 */ 25 private $contextNode; 26 27 /** 28 * @var bool 29 */ 30 private $finalized = false; 31 32 public function __construct(\DOMElement $context, $line) 33 { 34 $this->contextNode = $context; 35 36 $this->writer = new \XMLWriter; 37 $this->writer->openMemory(); 38 $this->writer->startElementNs(null, $context->nodeName, 'http://schema.phpunit.de/coverage/1.0'); 39 $this->writer->writeAttribute('nr', $line); 40 } 41 42 public function addTest($test) 43 { 44 if ($this->finalized) { 45 throw new RuntimeException('Coverage Report already finalized'); 46 } 47 48 $this->writer->startElement('covered'); 49 $this->writer->writeAttribute('by', $test); 50 $this->writer->endElement(); 51 } 52 53 public function finalize() 54 { 55 $this->writer->endElement(); 56 57 $fragment = $this->contextNode->ownerDocument->createDocumentFragment(); 58 $fragment->appendXML($this->writer->outputMemory()); 59 60 $this->contextNode->parentNode->replaceChild( 61 $fragment, 62 $this->contextNode 63 ); 64 65 $this->finalized = true; 66 } 67} 68