3 * This file is part of the PHP_CodeCoverage package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * @since Class available since Release 2.0.0
14 class PHP_CodeCoverage_Report_XML
22 * @var PHP_CodeCoverage_Report_XML_Project
26 public function process(PHP_CodeCoverage $coverage, $target)
28 if (substr($target, -1, 1) != DIRECTORY_SEPARATOR) {
29 $target .= DIRECTORY_SEPARATOR;
32 $this->target = $target;
33 $this->initTargetDirectory($target);
35 $report = $coverage->getReport();
37 $this->project = new PHP_CodeCoverage_Report_XML_Project(
38 $coverage->getReport()->getName()
41 $this->processTests($coverage->getTests());
42 $this->processDirectory($report, $this->project);
44 $index = $this->project->asDom();
45 $index->formatOutput = true;
46 $index->preserveWhiteSpace = false;
47 $index->save($target . '/index.xml');
50 private function initTargetDirectory($dir)
52 if (file_exists($dir)) {
54 throw new PHP_CodeCoverage_Exception(
55 "'$dir' exists but is not a directory."
59 if (!is_writable($dir)) {
60 throw new PHP_CodeCoverage_Exception(
61 "'$dir' exists but is not writable."
64 } elseif (!@mkdir($dir, 0777, true)) {
65 throw new PHP_CodeCoverage_Exception(
66 "'$dir' could not be created."
71 private function processDirectory(PHP_CodeCoverage_Report_Node_Directory $directory, PHP_CodeCoverage_Report_XML_Node $context)
73 $dirObject = $context->addDirectory($directory->getName());
75 $this->setTotals($directory, $dirObject->getTotals());
77 foreach ($directory as $node) {
78 if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) {
79 $this->processDirectory($node, $dirObject);
83 if ($node instanceof PHP_CodeCoverage_Report_Node_File) {
84 $this->processFile($node, $dirObject);
88 throw new PHP_CodeCoverage_Exception(
89 'Unknown node type for XML report'
94 private function processFile(PHP_CodeCoverage_Report_Node_File $file, PHP_CodeCoverage_Report_XML_Directory $context)
96 $fileObject = $context->addFile(
98 $file->getId() . '.xml'
101 $this->setTotals($file, $fileObject->getTotals());
103 $fileReport = new PHP_CodeCoverage_Report_XML_File_Report(
107 $this->setTotals($file, $fileReport->getTotals());
109 foreach ($file->getClassesAndTraits() as $unit) {
110 $this->processUnit($unit, $fileReport);
113 foreach ($file->getFunctions() as $function) {
114 $this->processFunction($function, $fileReport);
117 foreach ($file->getCoverageData() as $line => $tests) {
118 if (!is_array($tests) || count($tests) == 0) {
122 $coverage = $fileReport->getLineCoverage($line);
124 foreach ($tests as $test) {
125 $coverage->addTest($test);
128 $coverage->finalize();
131 $this->initTargetDirectory(
132 $this->target . dirname($file->getId()) . '/'
135 $fileDom = $fileReport->asDom();
136 $fileDom->formatOutput = true;
137 $fileDom->preserveWhiteSpace = false;
138 $fileDom->save($this->target . $file->getId() . '.xml');
141 private function processUnit($unit, PHP_CodeCoverage_Report_XML_File_Report $report)
143 if (isset($unit['className'])) {
144 $unitObject = $report->getClassObject($unit['className']);
146 $unitObject = $report->getTraitObject($unit['traitName']);
149 $unitObject->setLines(
151 $unit['executableLines'],
152 $unit['executedLines']
155 $unitObject->setCrap($unit['crap']);
157 $unitObject->setPackage(
158 $unit['package']['fullPackage'],
159 $unit['package']['package'],
160 $unit['package']['subpackage'],
161 $unit['package']['category']
164 $unitObject->setNamespace($unit['package']['namespace']);
166 foreach ($unit['methods'] as $method) {
167 $methodObject = $unitObject->addMethod($method['methodName']);
168 $methodObject->setSignature($method['signature']);
169 $methodObject->setLines($method['startLine'], $method['endLine']);
170 $methodObject->setCrap($method['crap']);
171 $methodObject->setTotals(
172 $method['executableLines'],
173 $method['executedLines'],
179 private function processFunction($function, PHP_CodeCoverage_Report_XML_File_Report $report)
181 $functionObject = $report->getFunctionObject($function['functionName']);
183 $functionObject->setSignature($function['signature']);
184 $functionObject->setLines($function['startLine']);
185 $functionObject->setCrap($function['crap']);
186 $functionObject->setTotals($function['executableLines'], $function['executedLines'], $function['coverage']);
189 private function processTests(array $tests)
191 $testsObject = $this->project->getTests();
193 foreach ($tests as $test => $result) {
194 if ($test == 'UNCOVERED_FILES_FROM_WHITELIST') {
198 $testsObject->addTest($test, $result);
202 private function setTotals(PHP_CodeCoverage_Report_Node $node, PHP_CodeCoverage_Report_XML_Totals $totals)
204 $loc = $node->getLinesOfCode();
206 $totals->setNumLines(
210 $node->getNumExecutableLines(),
211 $node->getNumExecutedLines()
214 $totals->setNumClasses(
215 $node->getNumClasses(),
216 $node->getNumTestedClasses()
219 $totals->setNumTraits(
220 $node->getNumTraits(),
221 $node->getNumTestedTraits()
224 $totals->setNumMethods(
225 $node->getNumMethods(),
226 $node->getNumTestedMethods()
229 $totals->setNumFunctions(
230 $node->getNumFunctions(),
231 $node->getNumTestedFunctions()