5 * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * @package CodeCoverage
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://github.com/sebastianbergmann/php-code-coverage
43 * @since File available since Release 1.1.0
47 * Represents a directory in the code coverage information tree.
50 * @package CodeCoverage
51 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
52 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
53 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
54 * @version Release: 1.1.1
55 * @link http://github.com/sebastianbergmann/php-code-coverage
56 * @since Class available since Release 1.1.0
58 class PHP_CodeCoverage_Report_Node_Directory extends PHP_CodeCoverage_Report_Node implements IteratorAggregate
61 * @var PHP_CodeCoverage_Report_Node[]
63 protected $children = array();
66 * @var PHP_CodeCoverage_Report_Node_Directory[]
68 protected $directories = array();
71 * @var PHP_CodeCoverage_Report_Node_File[]
73 protected $files = array();
93 protected $linesOfCode = NULL;
98 protected $numFiles = -1;
103 protected $numExecutableLines = -1;
108 protected $numExecutedLines = -1;
113 protected $numClasses = -1;
118 protected $numTestedClasses = -1;
123 protected $numTraits = -1;
128 protected $numTestedTraits = -1;
133 protected $numMethods = -1;
138 protected $numTestedMethods = -1;
143 protected $numFunctions = -1;
148 protected $numTestedFunctions = -1;
151 * Returns the number of files in/under this node.
155 public function count()
157 if ($this->numFiles == -1) {
160 foreach ($this->children as $child) {
161 $this->numFiles += count($child);
165 return $this->numFiles;
169 * Returns an iterator for this node.
171 * @return RecursiveIteratorIterator
173 public function getIterator()
175 return new RecursiveIteratorIterator(
176 new PHP_CodeCoverage_Report_Node_Iterator($this),
177 RecursiveIteratorIterator::SELF_FIRST
182 * Adds a new directory.
184 * @param string $name
185 * @return PHP_CodeCoverage_Report_Node_Directory
187 public function addDirectory($name)
189 $directory = new PHP_CodeCoverage_Report_Node_Directory($name, $this);
191 $this->children[] = $directory;
192 $this->directories[] = &$this->children[count($this->children) - 1];
200 * @param string $name
201 * @param array $coverageData
202 * @param array $testData
203 * @param boolean $cacheTokens
204 * @return PHP_CodeCoverage_Report_Node_File
205 * @throws PHP_CodeCoverage_Exception
207 public function addFile($name, array $coverageData, array $testData, $cacheTokens)
209 $file = new PHP_CodeCoverage_Report_Node_File(
210 $name, $this, $coverageData, $testData, $cacheTokens
213 $this->children[] = $file;
214 $this->files[] = &$this->children[count($this->children) - 1];
216 $this->numExecutableLines = -1;
217 $this->numExecutedLines = -1;
223 * Returns the directories in this directory.
227 public function getDirectories()
229 return $this->directories;
233 * Returns the files in this directory.
237 public function getFiles()
243 * Returns the child nodes of this node.
247 public function getChildNodes()
249 return $this->children;
253 * Returns the classes of this node.
257 public function getClasses()
259 if ($this->classes === NULL) {
260 $this->classes = array();
262 foreach ($this->children as $child) {
263 $this->classes = array_merge(
264 $this->classes, $child->getClasses()
269 return $this->classes;
273 * Returns the traits of this node.
277 public function getTraits()
279 if ($this->traits === NULL) {
280 $this->traits = array();
282 foreach ($this->children as $child) {
283 $this->traits = array_merge(
284 $this->traits, $child->getTraits()
289 return $this->traits;
293 * Returns the functions of this node.
297 public function getFunctions()
299 if ($this->functions === NULL) {
300 $this->functions = array();
302 foreach ($this->children as $child) {
303 $this->functions = array_merge(
304 $this->functions, $child->getFunctions()
309 return $this->functions;
313 * Returns the LOC/CLOC/NCLOC of this node.
317 public function getLinesOfCode()
319 if ($this->linesOfCode === NULL) {
320 $this->linesOfCode = array('loc' => 0, 'cloc' => 0, 'ncloc' => 0);
322 foreach ($this->children as $child) {
323 $linesOfCode = $child->getLinesOfCode();
325 $this->linesOfCode['loc'] += $linesOfCode['loc'];
326 $this->linesOfCode['cloc'] += $linesOfCode['cloc'];
327 $this->linesOfCode['ncloc'] += $linesOfCode['ncloc'];
331 return $this->linesOfCode;
335 * Returns the number of executable lines.
339 public function getNumExecutableLines()
341 if ($this->numExecutableLines == -1) {
342 $this->numExecutableLines = 0;
344 foreach ($this->children as $child) {
345 $this->numExecutableLines += $child->getNumExecutableLines();
349 return $this->numExecutableLines;
353 * Returns the number of executed lines.
357 public function getNumExecutedLines()
359 if ($this->numExecutedLines == -1) {
360 $this->numExecutedLines = 0;
362 foreach ($this->children as $child) {
363 $this->numExecutedLines += $child->getNumExecutedLines();
367 return $this->numExecutedLines;
371 * Returns the number of classes.
375 public function getNumClasses()
377 if ($this->numClasses == -1) {
378 $this->numClasses = 0;
380 foreach ($this->children as $child) {
381 $this->numClasses += $child->getNumClasses();
385 return $this->numClasses;
389 * Returns the number of tested classes.
393 public function getNumTestedClasses()
395 if ($this->numTestedClasses == -1) {
396 $this->numTestedClasses = 0;
398 foreach ($this->children as $child) {
399 $this->numTestedClasses += $child->getNumTestedClasses();
403 return $this->numTestedClasses;
407 * Returns the number of traits.
411 public function getNumTraits()
413 if ($this->numTraits == -1) {
414 $this->numTraits = 0;
416 foreach ($this->children as $child) {
417 $this->numTraits += $child->getNumTraits();
421 return $this->numTraits;
425 * Returns the number of tested traits.
429 public function getNumTestedTraits()
431 if ($this->numTestedTraits == -1) {
432 $this->numTestedTraits = 0;
434 foreach ($this->children as $child) {
435 $this->numTestedTraits += $child->getNumTestedTraits();
439 return $this->numTestedTraits;
443 * Returns the number of methods.
447 public function getNumMethods()
449 if ($this->numMethods == -1) {
450 $this->numMethods = 0;
452 foreach ($this->children as $child) {
453 $this->numMethods += $child->getNumMethods();
457 return $this->numMethods;
461 * Returns the number of tested methods.
465 public function getNumTestedMethods()
467 if ($this->numTestedMethods == -1) {
468 $this->numTestedMethods = 0;
470 foreach ($this->children as $child) {
471 $this->numTestedMethods += $child->getNumTestedMethods();
475 return $this->numTestedMethods;
479 * Returns the number of functions.
483 public function getNumFunctions()
485 if ($this->numFunctions == -1) {
486 $this->numFunctions = 0;
488 foreach ($this->children as $child) {
489 $this->numFunctions += $child->getNumFunctions();
493 return $this->numFunctions;
497 * Returns the number of tested functions.
501 public function getNumTestedFunctions()
503 if ($this->numTestedFunctions == -1) {
504 $this->numTestedFunctions = 0;
506 foreach ($this->children as $child) {
507 $this->numTestedFunctions += $child->getNumTestedFunctions();
511 return $this->numTestedFunctions;