3 * This file is part of the php-code-coverage 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.
11 namespace SebastianBergmann\CodeCoverage\Node
;
13 use SebastianBergmann\CodeCoverage\InvalidArgumentException
;
16 * Represents a directory in the code coverage information tree.
18 class Directory
extends AbstractNode
implements \IteratorAggregate
23 private $children = [];
28 private $directories = [];
53 private $linesOfCode = null;
58 private $numFiles = -1;
63 private $numExecutableLines = -1;
68 private $numExecutedLines = -1;
73 private $numClasses = -1;
78 private $numTestedClasses = -1;
83 private $numTraits = -1;
88 private $numTestedTraits = -1;
93 private $numMethods = -1;
98 private $numTestedMethods = -1;
103 private $numFunctions = -1;
108 private $numTestedFunctions = -1;
111 * Returns the number of files in/under this node.
115 public function count()
117 if ($this->numFiles
== -1) {
120 foreach ($this->children
as $child) {
121 $this->numFiles +
= count($child);
125 return $this->numFiles
;
129 * Returns an iterator for this node.
131 * @return \RecursiveIteratorIterator
133 public function getIterator()
135 return new \
RecursiveIteratorIterator(
137 \RecursiveIteratorIterator
::SELF_FIRST
142 * Adds a new directory.
144 * @param string $name
148 public function addDirectory($name)
150 $directory = new self($name, $this);
152 $this->children
[] = $directory;
153 $this->directories
[] = &$this->children
[count($this->children
) - 1];
161 * @param string $name
162 * @param array $coverageData
163 * @param array $testData
164 * @param bool $cacheTokens
168 * @throws InvalidArgumentException
170 public function addFile($name, array $coverageData, array $testData, $cacheTokens)
180 $this->children
[] = $file;
181 $this->files
[] = &$this->children
[count($this->children
) - 1];
183 $this->numExecutableLines
= -1;
184 $this->numExecutedLines
= -1;
190 * Returns the directories in this directory.
194 public function getDirectories()
196 return $this->directories
;
200 * Returns the files in this directory.
204 public function getFiles()
210 * Returns the child nodes of this node.
214 public function getChildNodes()
216 return $this->children
;
220 * Returns the classes of this node.
224 public function getClasses()
226 if ($this->classes
=== null) {
229 foreach ($this->children
as $child) {
230 $this->classes
= array_merge(
237 return $this->classes
;
241 * Returns the traits of this node.
245 public function getTraits()
247 if ($this->traits
=== null) {
250 foreach ($this->children
as $child) {
251 $this->traits
= array_merge(
258 return $this->traits
;
262 * Returns the functions of this node.
266 public function getFunctions()
268 if ($this->functions
=== null) {
269 $this->functions
= [];
271 foreach ($this->children
as $child) {
272 $this->functions
= array_merge(
274 $child->getFunctions()
279 return $this->functions
;
283 * Returns the LOC/CLOC/NCLOC of this node.
287 public function getLinesOfCode()
289 if ($this->linesOfCode
=== null) {
290 $this->linesOfCode
= ['loc' => 0, 'cloc' => 0, 'ncloc' => 0];
292 foreach ($this->children
as $child) {
293 $linesOfCode = $child->getLinesOfCode();
295 $this->linesOfCode
['loc'] +
= $linesOfCode['loc'];
296 $this->linesOfCode
['cloc'] +
= $linesOfCode['cloc'];
297 $this->linesOfCode
['ncloc'] +
= $linesOfCode['ncloc'];
301 return $this->linesOfCode
;
305 * Returns the number of executable lines.
309 public function getNumExecutableLines()
311 if ($this->numExecutableLines
== -1) {
312 $this->numExecutableLines
= 0;
314 foreach ($this->children
as $child) {
315 $this->numExecutableLines +
= $child->getNumExecutableLines();
319 return $this->numExecutableLines
;
323 * Returns the number of executed lines.
327 public function getNumExecutedLines()
329 if ($this->numExecutedLines
== -1) {
330 $this->numExecutedLines
= 0;
332 foreach ($this->children
as $child) {
333 $this->numExecutedLines +
= $child->getNumExecutedLines();
337 return $this->numExecutedLines
;
341 * Returns the number of classes.
345 public function getNumClasses()
347 if ($this->numClasses
== -1) {
348 $this->numClasses
= 0;
350 foreach ($this->children
as $child) {
351 $this->numClasses +
= $child->getNumClasses();
355 return $this->numClasses
;
359 * Returns the number of tested classes.
363 public function getNumTestedClasses()
365 if ($this->numTestedClasses
== -1) {
366 $this->numTestedClasses
= 0;
368 foreach ($this->children
as $child) {
369 $this->numTestedClasses +
= $child->getNumTestedClasses();
373 return $this->numTestedClasses
;
377 * Returns the number of traits.
381 public function getNumTraits()
383 if ($this->numTraits
== -1) {
384 $this->numTraits
= 0;
386 foreach ($this->children
as $child) {
387 $this->numTraits +
= $child->getNumTraits();
391 return $this->numTraits
;
395 * Returns the number of tested traits.
399 public function getNumTestedTraits()
401 if ($this->numTestedTraits
== -1) {
402 $this->numTestedTraits
= 0;
404 foreach ($this->children
as $child) {
405 $this->numTestedTraits +
= $child->getNumTestedTraits();
409 return $this->numTestedTraits
;
413 * Returns the number of methods.
417 public function getNumMethods()
419 if ($this->numMethods
== -1) {
420 $this->numMethods
= 0;
422 foreach ($this->children
as $child) {
423 $this->numMethods +
= $child->getNumMethods();
427 return $this->numMethods
;
431 * Returns the number of tested methods.
435 public function getNumTestedMethods()
437 if ($this->numTestedMethods
== -1) {
438 $this->numTestedMethods
= 0;
440 foreach ($this->children
as $child) {
441 $this->numTestedMethods +
= $child->getNumTestedMethods();
445 return $this->numTestedMethods
;
449 * Returns the number of functions.
453 public function getNumFunctions()
455 if ($this->numFunctions
== -1) {
456 $this->numFunctions
= 0;
458 foreach ($this->children
as $child) {
459 $this->numFunctions +
= $child->getNumFunctions();
463 return $this->numFunctions
;
467 * Returns the number of tested functions.
471 public function getNumTestedFunctions()
473 if ($this->numTestedFunctions
== -1) {
474 $this->numTestedFunctions
= 0;
476 foreach ($this->children
as $child) {
477 $this->numTestedFunctions +
= $child->getNumTestedFunctions();
481 return $this->numTestedFunctions
;