Mercurial > hg > isophonics-drupal-site
comparison vendor/phpunit/php-code-coverage/src/Report/Clover.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
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 | |
11 namespace SebastianBergmann\CodeCoverage\Report; | |
12 | |
13 use SebastianBergmann\CodeCoverage\CodeCoverage; | |
14 use SebastianBergmann\CodeCoverage\Node\File; | |
15 use SebastianBergmann\CodeCoverage\RuntimeException; | |
16 | |
17 /** | |
18 * Generates a Clover XML logfile from a code coverage object. | |
19 */ | |
20 class Clover | |
21 { | |
22 /** | |
23 * @param CodeCoverage $coverage | |
24 * @param string $target | |
25 * @param string $name | |
26 * | |
27 * @return string | |
28 * | |
29 * @throws \SebastianBergmann\CodeCoverage\RuntimeException | |
30 */ | |
31 public function process(CodeCoverage $coverage, $target = null, $name = null) | |
32 { | |
33 $xmlDocument = new \DOMDocument('1.0', 'UTF-8'); | |
34 $xmlDocument->formatOutput = true; | |
35 | |
36 $xmlCoverage = $xmlDocument->createElement('coverage'); | |
37 $xmlCoverage->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']); | |
38 $xmlDocument->appendChild($xmlCoverage); | |
39 | |
40 $xmlProject = $xmlDocument->createElement('project'); | |
41 $xmlProject->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']); | |
42 | |
43 if (\is_string($name)) { | |
44 $xmlProject->setAttribute('name', $name); | |
45 } | |
46 | |
47 $xmlCoverage->appendChild($xmlProject); | |
48 | |
49 $packages = []; | |
50 $report = $coverage->getReport(); | |
51 unset($coverage); | |
52 | |
53 foreach ($report as $item) { | |
54 if (!$item instanceof File) { | |
55 continue; | |
56 } | |
57 | |
58 /* @var File $item */ | |
59 | |
60 $xmlFile = $xmlDocument->createElement('file'); | |
61 $xmlFile->setAttribute('name', $item->getPath()); | |
62 | |
63 $classes = $item->getClassesAndTraits(); | |
64 $coverage = $item->getCoverageData(); | |
65 $lines = []; | |
66 $namespace = 'global'; | |
67 | |
68 foreach ($classes as $className => $class) { | |
69 $classStatements = 0; | |
70 $coveredClassStatements = 0; | |
71 $coveredMethods = 0; | |
72 $classMethods = 0; | |
73 | |
74 foreach ($class['methods'] as $methodName => $method) { | |
75 if ($method['executableLines'] == 0) { | |
76 continue; | |
77 } | |
78 | |
79 $classMethods++; | |
80 $classStatements += $method['executableLines']; | |
81 $coveredClassStatements += $method['executedLines']; | |
82 | |
83 if ($method['coverage'] == 100) { | |
84 $coveredMethods++; | |
85 } | |
86 | |
87 $methodCount = 0; | |
88 | |
89 foreach (\range($method['startLine'], $method['endLine']) as $line) { | |
90 if (isset($coverage[$line]) && ($coverage[$line] !== null)) { | |
91 $methodCount = \max($methodCount, \count($coverage[$line])); | |
92 } | |
93 } | |
94 | |
95 $lines[$method['startLine']] = [ | |
96 'ccn' => $method['ccn'], | |
97 'count' => $methodCount, | |
98 'crap' => $method['crap'], | |
99 'type' => 'method', | |
100 'visibility' => $method['visibility'], | |
101 'name' => $methodName | |
102 ]; | |
103 } | |
104 | |
105 if (!empty($class['package']['namespace'])) { | |
106 $namespace = $class['package']['namespace']; | |
107 } | |
108 | |
109 $xmlClass = $xmlDocument->createElement('class'); | |
110 $xmlClass->setAttribute('name', $className); | |
111 $xmlClass->setAttribute('namespace', $namespace); | |
112 | |
113 if (!empty($class['package']['fullPackage'])) { | |
114 $xmlClass->setAttribute( | |
115 'fullPackage', | |
116 $class['package']['fullPackage'] | |
117 ); | |
118 } | |
119 | |
120 if (!empty($class['package']['category'])) { | |
121 $xmlClass->setAttribute( | |
122 'category', | |
123 $class['package']['category'] | |
124 ); | |
125 } | |
126 | |
127 if (!empty($class['package']['package'])) { | |
128 $xmlClass->setAttribute( | |
129 'package', | |
130 $class['package']['package'] | |
131 ); | |
132 } | |
133 | |
134 if (!empty($class['package']['subpackage'])) { | |
135 $xmlClass->setAttribute( | |
136 'subpackage', | |
137 $class['package']['subpackage'] | |
138 ); | |
139 } | |
140 | |
141 $xmlFile->appendChild($xmlClass); | |
142 | |
143 $xmlMetrics = $xmlDocument->createElement('metrics'); | |
144 $xmlMetrics->setAttribute('complexity', $class['ccn']); | |
145 $xmlMetrics->setAttribute('methods', $classMethods); | |
146 $xmlMetrics->setAttribute('coveredmethods', $coveredMethods); | |
147 $xmlMetrics->setAttribute('conditionals', 0); | |
148 $xmlMetrics->setAttribute('coveredconditionals', 0); | |
149 $xmlMetrics->setAttribute('statements', $classStatements); | |
150 $xmlMetrics->setAttribute('coveredstatements', $coveredClassStatements); | |
151 $xmlMetrics->setAttribute('elements', $classMethods + $classStatements /* + conditionals */); | |
152 $xmlMetrics->setAttribute('coveredelements', $coveredMethods + $coveredClassStatements /* + coveredconditionals */); | |
153 $xmlClass->appendChild($xmlMetrics); | |
154 } | |
155 | |
156 foreach ($coverage as $line => $data) { | |
157 if ($data === null || isset($lines[$line])) { | |
158 continue; | |
159 } | |
160 | |
161 $lines[$line] = [ | |
162 'count' => \count($data), 'type' => 'stmt' | |
163 ]; | |
164 } | |
165 | |
166 \ksort($lines); | |
167 | |
168 foreach ($lines as $line => $data) { | |
169 $xmlLine = $xmlDocument->createElement('line'); | |
170 $xmlLine->setAttribute('num', $line); | |
171 $xmlLine->setAttribute('type', $data['type']); | |
172 | |
173 if (isset($data['name'])) { | |
174 $xmlLine->setAttribute('name', $data['name']); | |
175 } | |
176 | |
177 if (isset($data['visibility'])) { | |
178 $xmlLine->setAttribute('visibility', $data['visibility']); | |
179 } | |
180 | |
181 if (isset($data['ccn'])) { | |
182 $xmlLine->setAttribute('complexity', $data['ccn']); | |
183 } | |
184 | |
185 if (isset($data['crap'])) { | |
186 $xmlLine->setAttribute('crap', $data['crap']); | |
187 } | |
188 | |
189 $xmlLine->setAttribute('count', $data['count']); | |
190 $xmlFile->appendChild($xmlLine); | |
191 } | |
192 | |
193 $linesOfCode = $item->getLinesOfCode(); | |
194 | |
195 $xmlMetrics = $xmlDocument->createElement('metrics'); | |
196 $xmlMetrics->setAttribute('loc', $linesOfCode['loc']); | |
197 $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); | |
198 $xmlMetrics->setAttribute('classes', $item->getNumClassesAndTraits()); | |
199 $xmlMetrics->setAttribute('methods', $item->getNumMethods()); | |
200 $xmlMetrics->setAttribute('coveredmethods', $item->getNumTestedMethods()); | |
201 $xmlMetrics->setAttribute('conditionals', 0); | |
202 $xmlMetrics->setAttribute('coveredconditionals', 0); | |
203 $xmlMetrics->setAttribute('statements', $item->getNumExecutableLines()); | |
204 $xmlMetrics->setAttribute('coveredstatements', $item->getNumExecutedLines()); | |
205 $xmlMetrics->setAttribute('elements', $item->getNumMethods() + $item->getNumExecutableLines() /* + conditionals */); | |
206 $xmlMetrics->setAttribute('coveredelements', $item->getNumTestedMethods() + $item->getNumExecutedLines() /* + coveredconditionals */); | |
207 $xmlFile->appendChild($xmlMetrics); | |
208 | |
209 if ($namespace == 'global') { | |
210 $xmlProject->appendChild($xmlFile); | |
211 } else { | |
212 if (!isset($packages[$namespace])) { | |
213 $packages[$namespace] = $xmlDocument->createElement( | |
214 'package' | |
215 ); | |
216 | |
217 $packages[$namespace]->setAttribute('name', $namespace); | |
218 $xmlProject->appendChild($packages[$namespace]); | |
219 } | |
220 | |
221 $packages[$namespace]->appendChild($xmlFile); | |
222 } | |
223 } | |
224 | |
225 $linesOfCode = $report->getLinesOfCode(); | |
226 | |
227 $xmlMetrics = $xmlDocument->createElement('metrics'); | |
228 $xmlMetrics->setAttribute('files', \count($report)); | |
229 $xmlMetrics->setAttribute('loc', $linesOfCode['loc']); | |
230 $xmlMetrics->setAttribute('ncloc', $linesOfCode['ncloc']); | |
231 $xmlMetrics->setAttribute('classes', $report->getNumClassesAndTraits()); | |
232 $xmlMetrics->setAttribute('methods', $report->getNumMethods()); | |
233 $xmlMetrics->setAttribute('coveredmethods', $report->getNumTestedMethods()); | |
234 $xmlMetrics->setAttribute('conditionals', 0); | |
235 $xmlMetrics->setAttribute('coveredconditionals', 0); | |
236 $xmlMetrics->setAttribute('statements', $report->getNumExecutableLines()); | |
237 $xmlMetrics->setAttribute('coveredstatements', $report->getNumExecutedLines()); | |
238 $xmlMetrics->setAttribute('elements', $report->getNumMethods() + $report->getNumExecutableLines() /* + conditionals */); | |
239 $xmlMetrics->setAttribute('coveredelements', $report->getNumTestedMethods() + $report->getNumExecutedLines() /* + coveredconditionals */); | |
240 $xmlProject->appendChild($xmlMetrics); | |
241 | |
242 $buffer = $xmlDocument->saveXML(); | |
243 | |
244 if ($target !== null) { | |
245 if (!\is_dir(\dirname($target))) { | |
246 \mkdir(\dirname($target), 0777, true); | |
247 } | |
248 | |
249 if (@\file_put_contents($target, $buffer) === false) { | |
250 throw new RuntimeException( | |
251 \sprintf( | |
252 'Could not write to "%s', | |
253 $target | |
254 ) | |
255 ); | |
256 } | |
257 } | |
258 | |
259 return $buffer; | |
260 } | |
261 } |