5 * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * @package CodeCoverage
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://github.com/sebastianbergmann/php-code-coverage
43 * @since File available since Release 1.0.0
46 require_once 'PHP/CodeCoverage.php';
47 require_once 'PHP/Token/Stream/CachingFactory.php';
50 * Generates a Clover XML logfile from an PHP_CodeCoverage object.
53 * @package CodeCoverage
54 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
55 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
56 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
57 * @version Release: 1.0.4
58 * @link http://github.com/sebastianbergmann/php-code-coverage
59 * @since Class available since Release 1.0.0
61 class PHP_CodeCoverage_Report_Clover
64 * @param PHP_CodeCoverage $coverage
65 * @param string $target
69 public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL)
71 $document = new DOMDocument('1.0', 'UTF-8');
72 $document->formatOutput = TRUE;
74 $root = $document->createElement('coverage');
75 $root->setAttribute('generated', (int)$_SERVER['REQUEST_TIME']);
76 $document->appendChild($root);
78 $project = $document->createElement('project');
79 $project->setAttribute('timestamp', (int)$_SERVER['REQUEST_TIME']);
81 if (is_string($name)) {
82 $project->setAttribute('name', $name);
85 $root->appendChild($project);
87 $files = $coverage->getSummary();
90 $projectStatistics = array(
96 'coveredMethods' => 0,
98 'coveredConditionals' => 0,
100 'coveredStatements' => 0
103 foreach ($files as $filename => $data) {
104 $namespace = 'global';
106 if (file_exists($filename)) {
107 $fileStatistics = array(
110 'coveredMethods' => 0,
112 'coveredConditionals' => 0,
114 'coveredStatements' => 0
117 $file = $document->createElement('file');
118 $file->setAttribute('name', $filename);
120 $tokens = PHP_Token_Stream_CachingFactory::get($filename);
121 $classesInFile = $tokens->getClasses();
122 $linesOfCode = $tokens->getLinesOfCode();
125 $ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored(
131 foreach ($classesInFile as $className => $_class) {
132 $classStatistics = array(
134 'coveredMethods' => 0,
136 'coveredConditionals' => 0,
138 'coveredStatements' => 0
141 foreach ($_class['methods'] as $methodName => $method) {
142 $classStatistics['methods']++;
146 $methodLinesCovered = 0;
148 for ($i = $method['startLine'];
149 $i <= $method['endLine'];
151 if (isset($ignoredLines[$i])) {
158 if (isset($files[$filename][$i])) {
159 if ($files[$filename][$i] != -2) {
160 $classStatistics['statements']++;
164 if (is_array($files[$filename][$i])) {
165 $classStatistics['coveredStatements']++;
166 $methodLinesCovered++;
167 $count = count($files[$filename][$i]);
170 else if ($files[$filename][$i] == -2) {
177 $methodCount = max($methodCount, $count);
187 if ($methodCount > 0) {
188 $classStatistics['coveredMethods']++;
191 $lines[$method['startLine']] = array(
192 'count' => $methodCount,
193 'crap' => PHP_CodeCoverage_Util::crap(
195 PHP_CodeCoverage_Util::percent(
201 'name' => $methodName
205 $package = PHP_CodeCoverage_Util::getPackageInformation(
206 $className, $_class['docblock']
209 if (!empty($package['namespace'])) {
210 $namespace = $package['namespace'];
213 $class = $document->createElement('class');
214 $class->setAttribute('name', $className);
215 $class->setAttribute('namespace', $namespace);
217 if (!empty($package['fullPackage'])) {
218 $class->setAttribute(
219 'fullPackage', $package['fullPackage']
223 if (!empty($package['category'])) {
224 $class->setAttribute(
225 'category', $package['category']
229 if (!empty($package['package'])) {
230 $class->setAttribute(
231 'package', $package['package']
235 if (!empty($package['subpackage'])) {
236 $class->setAttribute(
237 'subpackage', $package['subpackage']
241 $file->appendChild($class);
243 $metrics = $document->createElement('metrics');
245 $metrics->setAttribute(
246 'methods', $classStatistics['methods']
249 $metrics->setAttribute(
250 'coveredmethods', $classStatistics['coveredMethods']
253 $metrics->setAttribute(
254 'conditionals', $classStatistics['conditionals']
257 $metrics->setAttribute(
258 'coveredconditionals',
259 $classStatistics['coveredConditionals']
262 $metrics->setAttribute(
263 'statements', $classStatistics['statements']
266 $metrics->setAttribute(
268 $classStatistics['coveredStatements']
271 $metrics->setAttribute(
273 $classStatistics['conditionals'] +
274 $classStatistics['statements'] +
275 $classStatistics['methods']
278 $metrics->setAttribute(
280 $classStatistics['coveredConditionals'] +
281 $classStatistics['coveredStatements'] +
282 $classStatistics['coveredMethods']
285 $class->appendChild($metrics);
287 $fileStatistics['methods'] += $classStatistics['methods'];
288 $fileStatistics['coveredMethods'] += $classStatistics['coveredMethods'];
289 $fileStatistics['conditionals'] += $classStatistics['conditionals'];
290 $fileStatistics['coveredConditionals'] += $classStatistics['coveredConditionals'];
291 $fileStatistics['statements'] += $classStatistics['statements'];
292 $fileStatistics['coveredStatements'] += $classStatistics['coveredStatements'];
293 $fileStatistics['classes']++;
296 foreach ($data as $_line => $_data) {
297 if (isset($lines[$_line]) || isset($ignoredLines[$_line])) {
302 $fileStatistics['statements']++;
304 if (is_array($_data)) {
305 $count = count($_data);
306 $fileStatistics['coveredStatements']++;
311 $lines[$_line] = array(
320 foreach ($lines as $_line => $_data) {
321 if (isset($ignoredLines[$_line])) {
325 $line = $document->createElement('line');
326 $line->setAttribute('num', $_line);
327 $line->setAttribute('type', $_data['type']);
329 if (isset($_data['name'])) {
330 $line->setAttribute('name', $_data['name']);
333 if (isset($_data['crap'])) {
334 $line->setAttribute('crap', $_data['crap']);
337 $line->setAttribute('count', $_data['count']);
339 $file->appendChild($line);
342 $metrics = $document->createElement('metrics');
344 $metrics->setAttribute('loc', $linesOfCode['loc']);
345 $metrics->setAttribute('ncloc', $linesOfCode['ncloc']);
346 $metrics->setAttribute('classes', $fileStatistics['classes']);
347 $metrics->setAttribute('methods', $fileStatistics['methods']);
349 $metrics->setAttribute(
350 'coveredmethods', $fileStatistics['coveredMethods']
353 $metrics->setAttribute(
354 'conditionals', $fileStatistics['conditionals']
357 $metrics->setAttribute(
358 'coveredconditionals', $fileStatistics['coveredConditionals']
361 $metrics->setAttribute(
362 'statements', $fileStatistics['statements']
365 $metrics->setAttribute(
366 'coveredstatements', $fileStatistics['coveredStatements']
369 $metrics->setAttribute(
371 $fileStatistics['conditionals'] +
372 $fileStatistics['statements'] +
373 $fileStatistics['methods']
376 $metrics->setAttribute(
378 $fileStatistics['coveredConditionals'] +
379 $fileStatistics['coveredStatements'] +
380 $fileStatistics['coveredMethods']
383 $file->appendChild($metrics);
385 if ($namespace == 'global') {
386 $project->appendChild($file);
388 if (!isset($packages[$namespace])) {
389 $packages[$namespace] = $document->createElement(
393 $packages[$namespace]->setAttribute('name', $namespace);
394 $project->appendChild($packages[$namespace]);
397 $packages[$namespace]->appendChild($file);
400 $projectStatistics['loc'] += $linesOfCode['loc'];
401 $projectStatistics['ncloc'] += $linesOfCode['ncloc'];
402 $projectStatistics['classes'] += $fileStatistics['classes'];
403 $projectStatistics['methods'] += $fileStatistics['methods'];
404 $projectStatistics['coveredMethods'] += $fileStatistics['coveredMethods'];
405 $projectStatistics['conditionals'] += $fileStatistics['conditionals'];
406 $projectStatistics['coveredConditionals'] += $fileStatistics['coveredConditionals'];
407 $projectStatistics['statements'] += $fileStatistics['statements'];
408 $projectStatistics['coveredStatements'] += $fileStatistics['coveredStatements'];
409 $projectStatistics['files']++;
413 $metrics = $document->createElement('metrics');
415 $metrics->setAttribute('files', $projectStatistics['files']);
416 $metrics->setAttribute('loc', $projectStatistics['loc']);
417 $metrics->setAttribute('ncloc', $projectStatistics['ncloc']);
418 $metrics->setAttribute('classes', $projectStatistics['classes']);
419 $metrics->setAttribute('methods', $projectStatistics['methods']);
421 $metrics->setAttribute(
422 'coveredmethods', $projectStatistics['coveredMethods']
425 $metrics->setAttribute(
426 'conditionals', $projectStatistics['conditionals']
429 $metrics->setAttribute(
430 'coveredconditionals', $projectStatistics['coveredConditionals']
433 $metrics->setAttribute(
434 'statements', $projectStatistics['statements']
437 $metrics->setAttribute(
438 'coveredstatements', $projectStatistics['coveredStatements']
441 $metrics->setAttribute(
443 $projectStatistics['conditionals'] +
444 $projectStatistics['statements'] +
445 $projectStatistics['methods']
448 $metrics->setAttribute(
450 $projectStatistics['coveredConditionals'] +
451 $projectStatistics['coveredStatements'] +
452 $projectStatistics['coveredMethods']
455 $project->appendChild($metrics);
457 if ($target !== NULL) {
458 if (!is_dir(dirname($target))) {
459 mkdir(dirname($target), 0777, TRUE);
462 return $document->save($target);
464 return $document->saveXML();