| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- /*
- * This file is part of the php-code-coverage package.
- *
- * (c) Sebastian Bergmann <sebastian@phpunit.de>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace SebastianBergmann\CodeCoverage\Node;
- use SebastianBergmann\CodeCoverage\Util;
- /**
- * Base class for nodes in the code coverage information tree.
- */
- abstract class AbstractNode implements \Countable
- {
- /**
- * @var string
- */
- private $name;
- /**
- * @var string
- */
- private $path;
- /**
- * @var array
- */
- private $pathArray;
- /**
- * @var AbstractNode
- */
- private $parent;
- /**
- * @var string
- */
- private $id;
- public function __construct(string $name, self $parent = null)
- {
- if (\substr($name, -1) == \DIRECTORY_SEPARATOR) {
- $name = \substr($name, 0, -1);
- }
- $this->name = $name;
- $this->parent = $parent;
- }
- public function getName(): string
- {
- return $this->name;
- }
- public function getId(): string
- {
- if ($this->id === null) {
- $parent = $this->getParent();
- if ($parent === null) {
- $this->id = 'index';
- } else {
- $parentId = $parent->getId();
- if ($parentId === 'index') {
- $this->id = \str_replace(':', '_', $this->name);
- } else {
- $this->id = $parentId . '/' . $this->name;
- }
- }
- }
- return $this->id;
- }
- public function getPath(): string
- {
- if ($this->path === null) {
- if ($this->parent === null || $this->parent->getPath() === null || $this->parent->getPath() === false) {
- $this->path = $this->name;
- } else {
- $this->path = $this->parent->getPath() . \DIRECTORY_SEPARATOR . $this->name;
- }
- }
- return $this->path;
- }
- public function getPathAsArray(): array
- {
- if ($this->pathArray === null) {
- if ($this->parent === null) {
- $this->pathArray = [];
- } else {
- $this->pathArray = $this->parent->getPathAsArray();
- }
- $this->pathArray[] = $this;
- }
- return $this->pathArray;
- }
- public function getParent(): ?self
- {
- return $this->parent;
- }
- /**
- * Returns the percentage of classes that has been tested.
- *
- * @return int|string
- */
- public function getTestedClassesPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumTestedClasses(),
- $this->getNumClasses(),
- $asString
- );
- }
- /**
- * Returns the percentage of traits that has been tested.
- *
- * @return int|string
- */
- public function getTestedTraitsPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumTestedTraits(),
- $this->getNumTraits(),
- $asString
- );
- }
- /**
- * Returns the percentage of classes and traits that has been tested.
- *
- * @return int|string
- */
- public function getTestedClassesAndTraitsPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumTestedClassesAndTraits(),
- $this->getNumClassesAndTraits(),
- $asString
- );
- }
- /**
- * Returns the percentage of functions that has been tested.
- *
- * @return int|string
- */
- public function getTestedFunctionsPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumTestedFunctions(),
- $this->getNumFunctions(),
- $asString
- );
- }
- /**
- * Returns the percentage of methods that has been tested.
- *
- * @return int|string
- */
- public function getTestedMethodsPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumTestedMethods(),
- $this->getNumMethods(),
- $asString
- );
- }
- /**
- * Returns the percentage of functions and methods that has been tested.
- *
- * @return int|string
- */
- public function getTestedFunctionsAndMethodsPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumTestedFunctionsAndMethods(),
- $this->getNumFunctionsAndMethods(),
- $asString
- );
- }
- /**
- * Returns the percentage of executed lines.
- *
- * @return int|string
- */
- public function getLineExecutedPercent(bool $asString = true)
- {
- return Util::percent(
- $this->getNumExecutedLines(),
- $this->getNumExecutableLines(),
- $asString
- );
- }
- /**
- * Returns the number of classes and traits.
- */
- public function getNumClassesAndTraits(): int
- {
- return $this->getNumClasses() + $this->getNumTraits();
- }
- /**
- * Returns the number of tested classes and traits.
- */
- public function getNumTestedClassesAndTraits(): int
- {
- return $this->getNumTestedClasses() + $this->getNumTestedTraits();
- }
- /**
- * Returns the classes and traits of this node.
- */
- public function getClassesAndTraits(): array
- {
- return \array_merge($this->getClasses(), $this->getTraits());
- }
- /**
- * Returns the number of functions and methods.
- */
- public function getNumFunctionsAndMethods(): int
- {
- return $this->getNumFunctions() + $this->getNumMethods();
- }
- /**
- * Returns the number of tested functions and methods.
- */
- public function getNumTestedFunctionsAndMethods(): int
- {
- return $this->getNumTestedFunctions() + $this->getNumTestedMethods();
- }
- /**
- * Returns the functions and methods of this node.
- */
- public function getFunctionsAndMethods(): array
- {
- return \array_merge($this->getFunctions(), $this->getMethods());
- }
- /**
- * Returns the classes of this node.
- */
- abstract public function getClasses(): array;
- /**
- * Returns the traits of this node.
- */
- abstract public function getTraits(): array;
- /**
- * Returns the functions of this node.
- */
- abstract public function getFunctions(): array;
- /**
- * Returns the LOC/CLOC/NCLOC of this node.
- */
- abstract public function getLinesOfCode(): array;
- /**
- * Returns the number of executable lines.
- */
- abstract public function getNumExecutableLines(): int;
- /**
- * Returns the number of executed lines.
- */
- abstract public function getNumExecutedLines(): int;
- /**
- * Returns the number of classes.
- */
- abstract public function getNumClasses(): int;
- /**
- * Returns the number of tested classes.
- */
- abstract public function getNumTestedClasses(): int;
- /**
- * Returns the number of traits.
- */
- abstract public function getNumTraits(): int;
- /**
- * Returns the number of tested traits.
- */
- abstract public function getNumTestedTraits(): int;
- /**
- * Returns the number of methods.
- */
- abstract public function getNumMethods(): int;
- /**
- * Returns the number of tested methods.
- */
- abstract public function getNumTestedMethods(): int;
- /**
- * Returns the number of functions.
- */
- abstract public function getNumFunctions(): int;
- /**
- * Returns the number of tested functions.
- */
- abstract public function getNumTestedFunctions(): int;
- }
|