Mercurial > hg > isophonics-drupal-site
comparison vendor/phpunit/php-code-coverage/src/CodeCoverage/Report/Crap4j.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 /* | |
3 * This file is part of the PHP_CodeCoverage 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 | |
11 /** | |
12 * @since Class available since Release 2.0.0 | |
13 */ | |
14 class PHP_CodeCoverage_Report_Crap4j | |
15 { | |
16 /** | |
17 * @var int | |
18 */ | |
19 private $threshold; | |
20 | |
21 /** | |
22 * @param int $threshold | |
23 */ | |
24 public function __construct($threshold = 30) | |
25 { | |
26 if (!is_int($threshold)) { | |
27 throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory( | |
28 1, | |
29 'integer' | |
30 ); | |
31 } | |
32 | |
33 $this->threshold = $threshold; | |
34 } | |
35 | |
36 /** | |
37 * @param PHP_CodeCoverage $coverage | |
38 * @param string $target | |
39 * @param string $name | |
40 * @return string | |
41 */ | |
42 public function process(PHP_CodeCoverage $coverage, $target = null, $name = null) | |
43 { | |
44 $document = new DOMDocument('1.0', 'UTF-8'); | |
45 $document->formatOutput = true; | |
46 | |
47 $root = $document->createElement('crap_result'); | |
48 $document->appendChild($root); | |
49 | |
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']))); | |
53 | |
54 $stats = $document->createElement('stats'); | |
55 $methodsNode = $document->createElement('methods'); | |
56 | |
57 $report = $coverage->getReport(); | |
58 unset($coverage); | |
59 | |
60 $fullMethodCount = 0; | |
61 $fullCrapMethodCount = 0; | |
62 $fullCrapLoad = 0; | |
63 $fullCrap = 0; | |
64 | |
65 foreach ($report as $item) { | |
66 $namespace = 'global'; | |
67 | |
68 if (!$item instanceof PHP_CodeCoverage_Report_Node_File) { | |
69 continue; | |
70 } | |
71 | |
72 $file = $document->createElement('file'); | |
73 $file->setAttribute('name', $item->getPath()); | |
74 | |
75 $classes = $item->getClassesAndTraits(); | |
76 | |
77 foreach ($classes as $className => $class) { | |
78 foreach ($class['methods'] as $methodName => $method) { | |
79 $crapLoad = $this->getCrapLoad($method['crap'], $method['ccn'], $method['coverage']); | |
80 | |
81 $fullCrap += $method['crap']; | |
82 $fullCrapLoad += $crapLoad; | |
83 $fullMethodCount++; | |
84 | |
85 if ($method['crap'] >= $this->threshold) { | |
86 $fullCrapMethodCount++; | |
87 } | |
88 | |
89 $methodNode = $document->createElement('method'); | |
90 | |
91 if (!empty($class['package']['namespace'])) { | |
92 $namespace = $class['package']['namespace']; | |
93 } | |
94 | |
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))); | |
104 | |
105 $methodsNode->appendChild($methodNode); | |
106 } | |
107 } | |
108 } | |
109 | |
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)); | |
115 | |
116 if ($fullMethodCount > 0) { | |
117 $crapMethodPercent = $this->roundValue((100 * $fullCrapMethodCount) / $fullMethodCount); | |
118 } else { | |
119 $crapMethodPercent = 0; | |
120 } | |
121 | |
122 $stats->appendChild($document->createElement('crapMethodPercent', $crapMethodPercent)); | |
123 | |
124 $root->appendChild($stats); | |
125 $root->appendChild($methodsNode); | |
126 | |
127 if ($target !== null) { | |
128 if (!is_dir(dirname($target))) { | |
129 mkdir(dirname($target), 0777, true); | |
130 } | |
131 | |
132 return $document->save($target); | |
133 } else { | |
134 return $document->saveXML(); | |
135 } | |
136 } | |
137 | |
138 /** | |
139 * @param float $crapValue | |
140 * @param int $cyclomaticComplexity | |
141 * @param float $coveragePercent | |
142 * @return float | |
143 */ | |
144 private function getCrapLoad($crapValue, $cyclomaticComplexity, $coveragePercent) | |
145 { | |
146 $crapLoad = 0; | |
147 | |
148 if ($crapValue >= $this->threshold) { | |
149 $crapLoad += $cyclomaticComplexity * (1.0 - $coveragePercent / 100); | |
150 $crapLoad += $cyclomaticComplexity / $this->threshold; | |
151 } | |
152 | |
153 return $crapLoad; | |
154 } | |
155 | |
156 /** | |
157 * @param float $value | |
158 * @return float | |
159 */ | |
160 private function roundValue($value) | |
161 { | |
162 return round($value, 2); | |
163 } | |
164 } |