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