1<?php
2/**
3 * PHP_CodeCoverage
4 *
5 * Copyright (c) 2009-2013, Sebastian Bergmann <sebastian@phpunit.de>.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 *   * Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *
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
18 *     distribution.
19 *
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.
23 *
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.
36 *
37 * @category   PHP
38 * @package    CodeCoverage
39 * @author     Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright  2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
42 * @link       http://github.com/sebastianbergmann/php-code-coverage
43 * @since      File available since Release 1.1.0
44 */
45
46/**
47 * Base class for nodes in the code coverage information tree.
48 *
49 * @category   PHP
50 * @package    CodeCoverage
51 * @author     Sebastian Bergmann <sebastian@phpunit.de>
52 * @copyright  2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
53 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
54 * @link       http://github.com/sebastianbergmann/php-code-coverage
55 * @since      Class available since Release 1.1.0
56 */
57abstract class PHP_CodeCoverage_Report_Node implements Countable
58{
59    /**
60     * @var string
61     */
62    protected $name;
63
64    /**
65     * @var string
66     */
67    protected $path;
68
69    /**
70     * @var array
71     */
72    protected $pathArray;
73
74    /**
75     * @var PHP_CodeCoverage_Report_Node
76     */
77    protected $parent;
78
79    /**
80     * @var string
81     */
82    protected $id;
83
84    /**
85     * Constructor.
86     *
87     * @param string                       $name
88     * @param PHP_CodeCoverage_Report_Node $parent
89     */
90    public function __construct($name, PHP_CodeCoverage_Report_Node $parent = NULL)
91    {
92        if (substr($name, -1) == '/') {
93            $name = substr($name, 0, -1);
94        }
95
96        $this->name   = $name;
97        $this->parent = $parent;
98    }
99
100    /**
101     * @return string
102     */
103    public function getName()
104    {
105        return $this->name;
106    }
107
108    /**
109     * @return string
110     */
111    public function getId()
112    {
113        if ($this->id === NULL) {
114            $parent = $this->getParent();
115
116            if ($parent === NULL) {
117                $this->id = 'index';
118            } else {
119                $parentId = $parent->getId();
120
121                if ($parentId == 'index') {
122                    $this->id = str_replace(':', '_', $this->name);
123                } else {
124                    $this->id = $parentId . '_' . $this->name;
125                }
126            }
127        }
128
129        return $this->id;
130    }
131
132    /**
133     * @return string
134     */
135    public function getPath()
136    {
137        if ($this->path === NULL) {
138            if ($this->parent === NULL) {
139                $this->path = $this->name;
140            } else {
141                $this->path = $this->parent->getPath() . '/' . $this->name;
142            }
143        }
144
145        return $this->path;
146    }
147
148    /**
149     * @return array
150     */
151    public function getPathAsArray()
152    {
153        if ($this->pathArray === NULL) {
154            if ($this->parent === NULL) {
155                $this->pathArray = array();
156            } else {
157                $this->pathArray = $this->parent->getPathAsArray();
158            }
159
160            $this->pathArray[] = $this;
161        }
162
163        return $this->pathArray;
164    }
165
166    /**
167     * @return PHP_CodeCoverage_Report_Node
168     */
169    public function getParent()
170    {
171        return $this->parent;
172    }
173
174    /**
175     * Returns the percentage of classes that has been tested.
176     *
177     * @param  boolean $asString
178     * @return integer
179     */
180    public function getTestedClassesPercent($asString = TRUE)
181    {
182        return PHP_CodeCoverage_Util::percent(
183          $this->getNumTestedClasses(),
184          $this->getNumClasses(),
185          $asString
186        );
187    }
188
189    /**
190     * Returns the percentage of traits that has been tested.
191     *
192     * @param  boolean $asString
193     * @return integer
194     */
195    public function getTestedTraitsPercent($asString = TRUE)
196    {
197        return PHP_CodeCoverage_Util::percent(
198          $this->getNumTestedTraits(),
199          $this->getNumTraits(),
200          $asString
201        );
202    }
203
204    /**
205     * Returns the percentage of traits that has been tested.
206     *
207     * @param  boolean $asString
208     * @return integer
209     * @since  Method available since Release 1.2.0
210     */
211    public function getTestedClassesAndTraitsPercent($asString = TRUE)
212    {
213        return PHP_CodeCoverage_Util::percent(
214          $this->getNumTestedClassesAndTraits(),
215          $this->getNumClassesAndTraits(),
216          $asString
217        );
218    }
219
220    /**
221     * Returns the percentage of methods that has been tested.
222     *
223     * @param  boolean $asString
224     * @return integer
225     */
226    public function getTestedMethodsPercent($asString = TRUE)
227    {
228        return PHP_CodeCoverage_Util::percent(
229          $this->getNumTestedMethods(),
230          $this->getNumMethods(),
231          $asString
232        );
233    }
234
235    /**
236     * Returns the percentage of executed lines.
237     *
238     * @param  boolean $asString
239     * @return integer
240     */
241    public function getLineExecutedPercent($asString = TRUE)
242    {
243        return PHP_CodeCoverage_Util::percent(
244          $this->getNumExecutedLines(),
245          $this->getNumExecutableLines(),
246          $asString
247        );
248    }
249
250    /**
251     * Returns the number of classes and traits.
252     *
253     * @return integer
254     * @since  Method available since Release 1.2.0
255     */
256    public function getNumClassesAndTraits()
257    {
258        return $this->getNumClasses() + $this->getNumTraits();
259    }
260
261    /**
262     * Returns the number of tested classes and traits.
263     *
264     * @return integer
265     * @since  Method available since Release 1.2.0
266     */
267    public function getNumTestedClassesAndTraits()
268    {
269        return $this->getNumTestedClasses() + $this->getNumTestedTraits();
270    }
271
272    /**
273     * Returns the classes and traits of this node.
274     *
275     * @return array
276     * @since  Method available since Release 1.2.0
277     */
278    public function getClassesAndTraits()
279    {
280        return array_merge($this->getClasses(), $this->getTraits());
281    }
282
283    /**
284     * Returns the classes of this node.
285     *
286     * @return array
287     */
288    abstract public function getClasses();
289
290    /**
291     * Returns the traits of this node.
292     *
293     * @return array
294     */
295    abstract public function getTraits();
296
297    /**
298     * Returns the functions of this node.
299     *
300     * @return array
301     */
302    abstract public function getFunctions();
303
304    /**
305     * Returns the LOC/CLOC/NCLOC of this node.
306     *
307     * @return array
308     */
309    abstract public function getLinesOfCode();
310
311    /**
312     * Returns the number of executable lines.
313     *
314     * @return integer
315     */
316    abstract public function getNumExecutableLines();
317
318    /**
319     * Returns the number of executed lines.
320     *
321     * @return integer
322     */
323    abstract public function getNumExecutedLines();
324
325    /**
326     * Returns the number of classes.
327     *
328     * @return integer
329     */
330    abstract public function getNumClasses();
331
332    /**
333     * Returns the number of tested classes.
334     *
335     * @return integer
336     */
337    abstract public function getNumTestedClasses();
338
339    /**
340     * Returns the number of traits.
341     *
342     * @return integer
343     */
344    abstract public function getNumTraits();
345
346    /**
347     * Returns the number of tested traits.
348     *
349     * @return integer
350     */
351    abstract public function getNumTestedTraits();
352
353    /**
354     * Returns the number of methods.
355     *
356     * @return integer
357     */
358    abstract public function getNumMethods();
359
360    /**
361     * Returns the number of tested methods.
362     *
363     * @return integer
364     */
365    abstract public function getNumTestedMethods();
366
367    /**
368     * Returns the number of functions.
369     *
370     * @return integer
371     */
372    abstract public function getNumFunctions();
373
374    /**
375     * Returns the number of tested functions.
376     *
377     * @return integer
378     */
379    abstract public function getNumTestedFunctions();
380}
381