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_Crap4j
22 * @param int $threshold
24 public function __construct($threshold = 30)
26 if (!is_int($threshold)) {
27 throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
33 $this->threshold = $threshold;
37 * @param PHP_CodeCoverage $coverage
38 * @param string $target
42 public function process(PHP_CodeCoverage $coverage, $target = null, $name = null)
44 $document = new DOMDocument('1.0', 'UTF-8');
45 $document->formatOutput = true;
47 $root = $document->createElement('crap_result');
48 $document->appendChild($root);
50 $project = $document->createElement('project', is_string($name) ? $name : '');
51 $root->appendChild($project);
52 $root->appendChild($document->createElement('timestamp', date('Y-m-d H:i:s', (int) $_SERVER['REQUEST_TIME'])));
54 $stats = $document->createElement('stats');
55 $methodsNode = $document->createElement('methods');
57 $report = $coverage->getReport();
61 $fullCrapMethodCount = 0;
65 foreach ($report as $item) {
66 $namespace = 'global';
68 if (!$item instanceof PHP_CodeCoverage_Report_Node_File) {
72 $file = $document->createElement('file');
73 $file->setAttribute('name', $item->getPath());
75 $classes = $item->getClassesAndTraits();
77 foreach ($classes as $className => $class) {
78 foreach ($class['methods'] as $methodName => $method) {
79 $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']);
81 $fullCrap += $method['crap'];
82 $fullCrapLoad += $crapLoad;
85 if ($method['crap'] >= $this->threshold) {
86 $fullCrapMethodCount++;
89 $methodNode = $document->createElement('method');
91 if (!empty($class['package']['namespace'])) {
92 $namespace = $class['package']['namespace'];
95 $methodNode->appendChild($document->createElement('package', $namespace));
96 $methodNode->appendChild($document->createElement('className', $className));
97 $methodNode->appendChild($document->createElement('methodName', $methodName));
98 $methodNode->appendChild($document->createElement('methodSignature', htmlspecialchars($method['signature'])));
99 $methodNode->appendChild($document->createElement('fullMethod', htmlspecialchars($method['signature'])));
100 $methodNode->appendChild($document->createElement('crap', $this->roundValue($method['crap'])));
101 $methodNode->appendChild($document->createElement('complexity', $method['ccn']));
102 $methodNode->appendChild($document->createElement('coverage', $this->roundValue($method['coverage'])));
103 $methodNode->appendChild($document->createElement('crapLoad', round($crapLoad)));
105 $methodsNode->appendChild($methodNode);
110 $stats->appendChild($document->createElement('name', 'Method Crap Stats'));
111 $stats->appendChild($document->createElement('methodCount', $fullMethodCount));
112 $stats->appendChild($document->createElement('crapMethodCount', $fullCrapMethodCount));
113 $stats->appendChild($document->createElement('crapLoad', round($fullCrapLoad)));
114 $stats->appendChild($document->createElement('totalCrap', $fullCrap));
116 if ($fullMethodCount > 0) {
117 $crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount);
119 $crapMethodPercent = 0;
122 $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent));
124 $root->appendChild($stats);
125 $root->appendChild($methodsNode);
127 if ($target !== null) {
128 if (!is_dir(dirname($target))) {
129 mkdir(dirname($target), 0777, true);
132 return $document->save($target);
134 return $document->saveXML();
139 * @param float $crapValue
140 * @param int $cyclomaticComplexity
141 * @param float $coveragePercent
144 private function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent)
148 if ($crapValue >= $this->threshold) {
149 $crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100);
150 $crapLoad += $cyclomaticComplexity / $this->threshold;
157 * @param float $value
160 private function roundValue($value)
162 return round($value, 2);