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.0.0
46 require_once 'PHP/CodeCoverage/Report/HTML/Node/Iterator.php';
49 * Represents a directory in the code coverage information tree.
52 * @package CodeCoverage
53 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
54 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
55 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
56 * @version Release: 1.0.4
57 * @link http://github.com/sebastianbergmann/php-code-coverage
58 * @since Class available since Release 1.0.0
60 class PHP_CodeCoverage_Report_HTML_Node_Directory extends PHP_CodeCoverage_Report_HTML_Node implements IteratorAggregate
63 * @var PHP_CodeCoverage_Report_HTML_Node[]
65 protected $children = array();
68 * @var PHP_CodeCoverage_Report_HTML_Node_Directory[]
70 protected $directories = array();
73 * @var PHP_CodeCoverage_Report_HTML_Node_File[]
75 protected $files = array();
85 protected $numExecutableLines = -1;
90 protected $numExecutedLines = -1;
95 protected $numClasses = -1;
100 protected $numTestedClasses = -1;
105 protected $numMethods = -1;
110 protected $numTestedMethods = -1;
113 * Returns an iterator for this node.
115 * @return RecursiveIteratorIterator
117 public function getIterator()
119 return new RecursiveIteratorIterator(
120 new PHP_CodeCoverage_Report_HTML_Node_Iterator($this),
121 RecursiveIteratorIterator::SELF_FIRST
126 * Adds a new directory.
128 * @return PHP_CodeCoverage_Report_HTML_Node_Directory
130 public function addDirectory($name)
132 $directory = new PHP_CodeCoverage_Report_HTML_Node_Directory(
136 $this->children[] = $directory;
137 $this->directories[] = &$this->children[count($this->children) - 1];
145 * @param string $name
146 * @param array $lines
147 * @param boolean $yui
148 * @param boolean $highlight
149 * @return PHP_CodeCoverage_Report_HTML_Node_File
150 * @throws RuntimeException
152 public function addFile($name, array $lines, $yui, $highlight)
154 $file = new PHP_CodeCoverage_Report_HTML_Node_File(
155 $name, $this, $lines, $yui, $highlight
158 $this->children[] = $file;
159 $this->files[] = &$this->children[count($this->children) - 1];
161 $this->numExecutableLines = -1;
162 $this->numExecutedLines = -1;
168 * Returns the directories in this directory.
172 public function getDirectories()
174 return $this->directories;
178 * Returns the files in this directory.
182 public function getFiles()
188 * Returns the child nodes of this node.
192 public function getChildNodes()
194 return $this->children;
198 * Returns the classes of this node.
202 public function getClasses()
204 if ($this->classes === NULL) {
205 $this->classes = array();
207 foreach ($this->children as $child) {
208 $this->classes = array_merge(
209 $this->classes, $child->getClasses()
214 return $this->classes;
218 * Returns the number of executable lines.
222 public function getNumExecutableLines()
224 if ($this->numExecutableLines == -1) {
225 $this->numExecutableLines = 0;
227 foreach ($this->children as $child) {
228 $this->numExecutableLines += $child->getNumExecutableLines();
232 return $this->numExecutableLines;
236 * Returns the number of executed lines.
240 public function getNumExecutedLines()
242 if ($this->numExecutedLines == -1) {
243 $this->numExecutedLines = 0;
245 foreach ($this->children as $child) {
246 $this->numExecutedLines += $child->getNumExecutedLines();
250 return $this->numExecutedLines;
254 * Returns the number of classes.
258 public function getNumClasses()
260 if ($this->numClasses == -1) {
261 $this->numClasses = 0;
263 foreach ($this->children as $child) {
264 $this->numClasses += $child->getNumClasses();
268 return $this->numClasses;
272 * Returns the number of tested classes.
276 public function getNumTestedClasses()
278 if ($this->numTestedClasses == -1) {
279 $this->numTestedClasses = 0;
281 foreach ($this->children as $child) {
282 $this->numTestedClasses += $child->getNumTestedClasses();
286 return $this->numTestedClasses;
290 * Returns the number of methods.
294 public function getNumMethods()
296 if ($this->numMethods == -1) {
297 $this->numMethods = 0;
299 foreach ($this->children as $child) {
300 $this->numMethods += $child->getNumMethods();
304 return $this->numMethods;
308 * Returns the number of tested methods.
312 public function getNumTestedMethods()
314 if ($this->numTestedMethods == -1) {
315 $this->numTestedMethods = 0;
317 foreach ($this->children as $child) {
318 $this->numTestedMethods += $child->getNumTestedMethods();
322 return $this->numTestedMethods;
328 * @param string $target
329 * @param string $title
330 * @param string $charset
331 * @param integer $lowUpperBound
332 * @param integer $highLowerBound
333 * @param string $generator
335 public function render($target, $title, $charset = 'UTF-8', $lowUpperBound = 35, $highLowerBound = 70, $generator = '')
338 $target, $title, $charset, $lowUpperBound, $highLowerBound, $generator
341 foreach ($this->children as $child) {
352 $this->children = array();
356 * @param string $target
357 * @param string $title
358 * @param string $charset
359 * @param integer $lowUpperBound
360 * @param integer $highLowerBound
361 * @param string $generator
363 protected function doRender($target, $title, $charset, $lowUpperBound, $highLowerBound, $generator)
365 $cleanId = PHP_CodeCoverage_Util::getSafeFilename($this->getId());
366 $file = $target . $cleanId . '.html';
368 $template = new Text_Template(
369 PHP_CodeCoverage_Report_HTML::$templatePath . 'directory.html'
372 $this->setTemplateVars($template, $title, $charset, $generator);
376 'total_item' => $this->renderTotalItem(
377 $lowUpperBound, $highLowerBound
379 'items' => $this->renderItems(
380 $lowUpperBound, $highLowerBound
382 'low_upper_bound' => $lowUpperBound,
383 'high_lower_bound' => $highLowerBound
387 $template->renderTo($file);
389 $this->directories = array();
390 $this->files = array();
394 * @param float $lowUpperBound
395 * @param float $highLowerBound
398 protected function renderItems($lowUpperBound, $highLowerBound)
400 $items = $this->doRenderItems(
401 $this->directories, $lowUpperBound, $highLowerBound, 'coverDirectory'
404 $items .= $this->doRenderItems(
405 $this->files, $lowUpperBound, $highLowerBound, 'coverFile'
412 * @param array $items
413 * @param float $lowUpperBound
414 * @param float $highLowerBound
415 * @param string $itemClass
418 protected function doRenderItems(array $items, $lowUpperBound, $highLowerBound, $itemClass)
422 foreach ($items as $item) {
423 $result .= $this->doRenderItemObject(
424 $item, $lowUpperBound, $highLowerBound, NULL, $itemClass