Chris@14
|
1 <?php
|
Chris@14
|
2 /*
|
Chris@14
|
3 * This file is part of the php-code-coverage package.
|
Chris@14
|
4 *
|
Chris@14
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@14
|
6 *
|
Chris@14
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
8 * file that was distributed with this source code.
|
Chris@14
|
9 */
|
Chris@14
|
10
|
Chris@14
|
11 namespace SebastianBergmann\CodeCoverage\Node;
|
Chris@14
|
12
|
Chris@14
|
13 use SebastianBergmann\CodeCoverage\Util;
|
Chris@14
|
14
|
Chris@14
|
15 /**
|
Chris@14
|
16 * Base class for nodes in the code coverage information tree.
|
Chris@14
|
17 */
|
Chris@14
|
18 abstract class AbstractNode implements \Countable
|
Chris@14
|
19 {
|
Chris@14
|
20 /**
|
Chris@14
|
21 * @var string
|
Chris@14
|
22 */
|
Chris@14
|
23 private $name;
|
Chris@14
|
24
|
Chris@14
|
25 /**
|
Chris@14
|
26 * @var string
|
Chris@14
|
27 */
|
Chris@14
|
28 private $path;
|
Chris@14
|
29
|
Chris@14
|
30 /**
|
Chris@14
|
31 * @var array
|
Chris@14
|
32 */
|
Chris@14
|
33 private $pathArray;
|
Chris@14
|
34
|
Chris@14
|
35 /**
|
Chris@14
|
36 * @var AbstractNode
|
Chris@14
|
37 */
|
Chris@14
|
38 private $parent;
|
Chris@14
|
39
|
Chris@14
|
40 /**
|
Chris@14
|
41 * @var string
|
Chris@14
|
42 */
|
Chris@14
|
43 private $id;
|
Chris@14
|
44
|
Chris@14
|
45 /**
|
Chris@14
|
46 * Constructor.
|
Chris@14
|
47 *
|
Chris@14
|
48 * @param string $name
|
Chris@14
|
49 * @param AbstractNode $parent
|
Chris@14
|
50 */
|
Chris@14
|
51 public function __construct($name, self $parent = null)
|
Chris@14
|
52 {
|
Chris@14
|
53 if (\substr($name, -1) == '/') {
|
Chris@14
|
54 $name = \substr($name, 0, -1);
|
Chris@14
|
55 }
|
Chris@14
|
56
|
Chris@14
|
57 $this->name = $name;
|
Chris@14
|
58 $this->parent = $parent;
|
Chris@14
|
59 }
|
Chris@14
|
60
|
Chris@14
|
61 /**
|
Chris@14
|
62 * @return string
|
Chris@14
|
63 */
|
Chris@14
|
64 public function getName()
|
Chris@14
|
65 {
|
Chris@14
|
66 return $this->name;
|
Chris@14
|
67 }
|
Chris@14
|
68
|
Chris@14
|
69 /**
|
Chris@14
|
70 * @return string
|
Chris@14
|
71 */
|
Chris@14
|
72 public function getId()
|
Chris@14
|
73 {
|
Chris@14
|
74 if ($this->id === null) {
|
Chris@14
|
75 $parent = $this->getParent();
|
Chris@14
|
76
|
Chris@14
|
77 if ($parent === null) {
|
Chris@14
|
78 $this->id = 'index';
|
Chris@14
|
79 } else {
|
Chris@14
|
80 $parentId = $parent->getId();
|
Chris@14
|
81
|
Chris@14
|
82 if ($parentId == 'index') {
|
Chris@14
|
83 $this->id = \str_replace(':', '_', $this->name);
|
Chris@14
|
84 } else {
|
Chris@14
|
85 $this->id = $parentId . '/' . $this->name;
|
Chris@14
|
86 }
|
Chris@14
|
87 }
|
Chris@14
|
88 }
|
Chris@14
|
89
|
Chris@14
|
90 return $this->id;
|
Chris@14
|
91 }
|
Chris@14
|
92
|
Chris@14
|
93 /**
|
Chris@14
|
94 * @return string
|
Chris@14
|
95 */
|
Chris@14
|
96 public function getPath()
|
Chris@14
|
97 {
|
Chris@14
|
98 if ($this->path === null) {
|
Chris@14
|
99 if ($this->parent === null || $this->parent->getPath() === null || $this->parent->getPath() === false) {
|
Chris@14
|
100 $this->path = $this->name;
|
Chris@14
|
101 } else {
|
Chris@14
|
102 $this->path = $this->parent->getPath() . '/' . $this->name;
|
Chris@14
|
103 }
|
Chris@14
|
104 }
|
Chris@14
|
105
|
Chris@14
|
106 return $this->path;
|
Chris@14
|
107 }
|
Chris@14
|
108
|
Chris@14
|
109 /**
|
Chris@14
|
110 * @return array
|
Chris@14
|
111 */
|
Chris@14
|
112 public function getPathAsArray()
|
Chris@14
|
113 {
|
Chris@14
|
114 if ($this->pathArray === null) {
|
Chris@14
|
115 if ($this->parent === null) {
|
Chris@14
|
116 $this->pathArray = [];
|
Chris@14
|
117 } else {
|
Chris@14
|
118 $this->pathArray = $this->parent->getPathAsArray();
|
Chris@14
|
119 }
|
Chris@14
|
120
|
Chris@14
|
121 $this->pathArray[] = $this;
|
Chris@14
|
122 }
|
Chris@14
|
123
|
Chris@14
|
124 return $this->pathArray;
|
Chris@14
|
125 }
|
Chris@14
|
126
|
Chris@14
|
127 /**
|
Chris@14
|
128 * @return AbstractNode
|
Chris@14
|
129 */
|
Chris@14
|
130 public function getParent()
|
Chris@14
|
131 {
|
Chris@14
|
132 return $this->parent;
|
Chris@14
|
133 }
|
Chris@14
|
134
|
Chris@14
|
135 /**
|
Chris@14
|
136 * Returns the percentage of classes that has been tested.
|
Chris@14
|
137 *
|
Chris@14
|
138 * @param bool $asString
|
Chris@14
|
139 *
|
Chris@14
|
140 * @return int
|
Chris@14
|
141 */
|
Chris@14
|
142 public function getTestedClassesPercent($asString = true)
|
Chris@14
|
143 {
|
Chris@14
|
144 return Util::percent(
|
Chris@14
|
145 $this->getNumTestedClasses(),
|
Chris@14
|
146 $this->getNumClasses(),
|
Chris@14
|
147 $asString
|
Chris@14
|
148 );
|
Chris@14
|
149 }
|
Chris@14
|
150
|
Chris@14
|
151 /**
|
Chris@14
|
152 * Returns the percentage of traits that has been tested.
|
Chris@14
|
153 *
|
Chris@14
|
154 * @param bool $asString
|
Chris@14
|
155 *
|
Chris@14
|
156 * @return int
|
Chris@14
|
157 */
|
Chris@14
|
158 public function getTestedTraitsPercent($asString = true)
|
Chris@14
|
159 {
|
Chris@14
|
160 return Util::percent(
|
Chris@14
|
161 $this->getNumTestedTraits(),
|
Chris@14
|
162 $this->getNumTraits(),
|
Chris@14
|
163 $asString
|
Chris@14
|
164 );
|
Chris@14
|
165 }
|
Chris@14
|
166
|
Chris@14
|
167 /**
|
Chris@14
|
168 * Returns the percentage of classes and traits that has been tested.
|
Chris@14
|
169 *
|
Chris@14
|
170 * @param bool $asString
|
Chris@14
|
171 *
|
Chris@14
|
172 * @return int
|
Chris@14
|
173 */
|
Chris@14
|
174 public function getTestedClassesAndTraitsPercent($asString = true)
|
Chris@14
|
175 {
|
Chris@14
|
176 return Util::percent(
|
Chris@14
|
177 $this->getNumTestedClassesAndTraits(),
|
Chris@14
|
178 $this->getNumClassesAndTraits(),
|
Chris@14
|
179 $asString
|
Chris@14
|
180 );
|
Chris@14
|
181 }
|
Chris@14
|
182
|
Chris@14
|
183 /**
|
Chris@14
|
184 * Returns the percentage of functions that has been tested.
|
Chris@14
|
185 *
|
Chris@14
|
186 * @param bool $asString
|
Chris@14
|
187 *
|
Chris@14
|
188 * @return int
|
Chris@14
|
189 */
|
Chris@14
|
190 public function getTestedFunctionsPercent($asString = true)
|
Chris@14
|
191 {
|
Chris@14
|
192 return Util::percent(
|
Chris@14
|
193 $this->getNumTestedFunctions(),
|
Chris@14
|
194 $this->getNumFunctions(),
|
Chris@14
|
195 $asString
|
Chris@14
|
196 );
|
Chris@14
|
197 }
|
Chris@14
|
198
|
Chris@14
|
199 /**
|
Chris@14
|
200 * Returns the percentage of methods that has been tested.
|
Chris@14
|
201 *
|
Chris@14
|
202 * @param bool $asString
|
Chris@14
|
203 *
|
Chris@14
|
204 * @return int
|
Chris@14
|
205 */
|
Chris@14
|
206 public function getTestedMethodsPercent($asString = true)
|
Chris@14
|
207 {
|
Chris@14
|
208 return Util::percent(
|
Chris@14
|
209 $this->getNumTestedMethods(),
|
Chris@14
|
210 $this->getNumMethods(),
|
Chris@14
|
211 $asString
|
Chris@14
|
212 );
|
Chris@14
|
213 }
|
Chris@14
|
214
|
Chris@14
|
215 /**
|
Chris@14
|
216 * Returns the percentage of functions and methods that has been tested.
|
Chris@14
|
217 *
|
Chris@14
|
218 * @param bool $asString
|
Chris@14
|
219 *
|
Chris@14
|
220 * @return int
|
Chris@14
|
221 */
|
Chris@14
|
222 public function getTestedFunctionsAndMethodsPercent($asString = true)
|
Chris@14
|
223 {
|
Chris@14
|
224 return Util::percent(
|
Chris@14
|
225 $this->getNumTestedFunctionsAndMethods(),
|
Chris@14
|
226 $this->getNumFunctionsAndMethods(),
|
Chris@14
|
227 $asString
|
Chris@14
|
228 );
|
Chris@14
|
229 }
|
Chris@14
|
230
|
Chris@14
|
231 /**
|
Chris@14
|
232 * Returns the percentage of executed lines.
|
Chris@14
|
233 *
|
Chris@14
|
234 * @param bool $asString
|
Chris@14
|
235 *
|
Chris@14
|
236 * @return int
|
Chris@14
|
237 */
|
Chris@14
|
238 public function getLineExecutedPercent($asString = true)
|
Chris@14
|
239 {
|
Chris@14
|
240 return Util::percent(
|
Chris@14
|
241 $this->getNumExecutedLines(),
|
Chris@14
|
242 $this->getNumExecutableLines(),
|
Chris@14
|
243 $asString
|
Chris@14
|
244 );
|
Chris@14
|
245 }
|
Chris@14
|
246
|
Chris@14
|
247 /**
|
Chris@14
|
248 * Returns the number of classes and traits.
|
Chris@14
|
249 *
|
Chris@14
|
250 * @return int
|
Chris@14
|
251 */
|
Chris@14
|
252 public function getNumClassesAndTraits()
|
Chris@14
|
253 {
|
Chris@14
|
254 return $this->getNumClasses() + $this->getNumTraits();
|
Chris@14
|
255 }
|
Chris@14
|
256
|
Chris@14
|
257 /**
|
Chris@14
|
258 * Returns the number of tested classes and traits.
|
Chris@14
|
259 *
|
Chris@14
|
260 * @return int
|
Chris@14
|
261 */
|
Chris@14
|
262 public function getNumTestedClassesAndTraits()
|
Chris@14
|
263 {
|
Chris@14
|
264 return $this->getNumTestedClasses() + $this->getNumTestedTraits();
|
Chris@14
|
265 }
|
Chris@14
|
266
|
Chris@14
|
267 /**
|
Chris@14
|
268 * Returns the classes and traits of this node.
|
Chris@14
|
269 *
|
Chris@14
|
270 * @return array
|
Chris@14
|
271 */
|
Chris@14
|
272 public function getClassesAndTraits()
|
Chris@14
|
273 {
|
Chris@14
|
274 return \array_merge($this->getClasses(), $this->getTraits());
|
Chris@14
|
275 }
|
Chris@14
|
276
|
Chris@14
|
277 /**
|
Chris@14
|
278 * Returns the number of functions and methods.
|
Chris@14
|
279 *
|
Chris@14
|
280 * @return int
|
Chris@14
|
281 */
|
Chris@14
|
282 public function getNumFunctionsAndMethods()
|
Chris@14
|
283 {
|
Chris@14
|
284 return $this->getNumFunctions() + $this->getNumMethods();
|
Chris@14
|
285 }
|
Chris@14
|
286
|
Chris@14
|
287 /**
|
Chris@14
|
288 * Returns the number of tested functions and methods.
|
Chris@14
|
289 *
|
Chris@14
|
290 * @return int
|
Chris@14
|
291 */
|
Chris@14
|
292 public function getNumTestedFunctionsAndMethods()
|
Chris@14
|
293 {
|
Chris@14
|
294 return $this->getNumTestedFunctions() + $this->getNumTestedMethods();
|
Chris@14
|
295 }
|
Chris@14
|
296
|
Chris@14
|
297 /**
|
Chris@14
|
298 * Returns the functions and methods of this node.
|
Chris@14
|
299 *
|
Chris@14
|
300 * @return array
|
Chris@14
|
301 */
|
Chris@14
|
302 public function getFunctionsAndMethods()
|
Chris@14
|
303 {
|
Chris@14
|
304 return \array_merge($this->getFunctions(), $this->getMethods());
|
Chris@14
|
305 }
|
Chris@14
|
306
|
Chris@14
|
307 /**
|
Chris@14
|
308 * Returns the classes of this node.
|
Chris@14
|
309 *
|
Chris@14
|
310 * @return array
|
Chris@14
|
311 */
|
Chris@14
|
312 abstract public function getClasses();
|
Chris@14
|
313
|
Chris@14
|
314 /**
|
Chris@14
|
315 * Returns the traits of this node.
|
Chris@14
|
316 *
|
Chris@14
|
317 * @return array
|
Chris@14
|
318 */
|
Chris@14
|
319 abstract public function getTraits();
|
Chris@14
|
320
|
Chris@14
|
321 /**
|
Chris@14
|
322 * Returns the functions of this node.
|
Chris@14
|
323 *
|
Chris@14
|
324 * @return array
|
Chris@14
|
325 */
|
Chris@14
|
326 abstract public function getFunctions();
|
Chris@14
|
327
|
Chris@14
|
328 /**
|
Chris@14
|
329 * Returns the LOC/CLOC/NCLOC of this node.
|
Chris@14
|
330 *
|
Chris@14
|
331 * @return array
|
Chris@14
|
332 */
|
Chris@14
|
333 abstract public function getLinesOfCode();
|
Chris@14
|
334
|
Chris@14
|
335 /**
|
Chris@14
|
336 * Returns the number of executable lines.
|
Chris@14
|
337 *
|
Chris@14
|
338 * @return int
|
Chris@14
|
339 */
|
Chris@14
|
340 abstract public function getNumExecutableLines();
|
Chris@14
|
341
|
Chris@14
|
342 /**
|
Chris@14
|
343 * Returns the number of executed lines.
|
Chris@14
|
344 *
|
Chris@14
|
345 * @return int
|
Chris@14
|
346 */
|
Chris@14
|
347 abstract public function getNumExecutedLines();
|
Chris@14
|
348
|
Chris@14
|
349 /**
|
Chris@14
|
350 * Returns the number of classes.
|
Chris@14
|
351 *
|
Chris@14
|
352 * @return int
|
Chris@14
|
353 */
|
Chris@14
|
354 abstract public function getNumClasses();
|
Chris@14
|
355
|
Chris@14
|
356 /**
|
Chris@14
|
357 * Returns the number of tested classes.
|
Chris@14
|
358 *
|
Chris@14
|
359 * @return int
|
Chris@14
|
360 */
|
Chris@14
|
361 abstract public function getNumTestedClasses();
|
Chris@14
|
362
|
Chris@14
|
363 /**
|
Chris@14
|
364 * Returns the number of traits.
|
Chris@14
|
365 *
|
Chris@14
|
366 * @return int
|
Chris@14
|
367 */
|
Chris@14
|
368 abstract public function getNumTraits();
|
Chris@14
|
369
|
Chris@14
|
370 /**
|
Chris@14
|
371 * Returns the number of tested traits.
|
Chris@14
|
372 *
|
Chris@14
|
373 * @return int
|
Chris@14
|
374 */
|
Chris@14
|
375 abstract public function getNumTestedTraits();
|
Chris@14
|
376
|
Chris@14
|
377 /**
|
Chris@14
|
378 * Returns the number of methods.
|
Chris@14
|
379 *
|
Chris@14
|
380 * @return int
|
Chris@14
|
381 */
|
Chris@14
|
382 abstract public function getNumMethods();
|
Chris@14
|
383
|
Chris@14
|
384 /**
|
Chris@14
|
385 * Returns the number of tested methods.
|
Chris@14
|
386 *
|
Chris@14
|
387 * @return int
|
Chris@14
|
388 */
|
Chris@14
|
389 abstract public function getNumTestedMethods();
|
Chris@14
|
390
|
Chris@14
|
391 /**
|
Chris@14
|
392 * Returns the number of functions.
|
Chris@14
|
393 *
|
Chris@14
|
394 * @return int
|
Chris@14
|
395 */
|
Chris@14
|
396 abstract public function getNumFunctions();
|
Chris@14
|
397
|
Chris@14
|
398 /**
|
Chris@14
|
399 * Returns the number of tested functions.
|
Chris@14
|
400 *
|
Chris@14
|
401 * @return int
|
Chris@14
|
402 */
|
Chris@14
|
403 abstract public function getNumTestedFunctions();
|
Chris@14
|
404 }
|