AbstractNode.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage 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. namespace SebastianBergmann\CodeCoverage\Node;
  11. use SebastianBergmann\CodeCoverage\Util;
  12. /**
  13. * Base class for nodes in the code coverage information tree.
  14. */
  15. abstract class AbstractNode implements \Countable
  16. {
  17. /**
  18. * @var string
  19. */
  20. private $name;
  21. /**
  22. * @var string
  23. */
  24. private $path;
  25. /**
  26. * @var array
  27. */
  28. private $pathArray;
  29. /**
  30. * @var AbstractNode
  31. */
  32. private $parent;
  33. /**
  34. * @var string
  35. */
  36. private $id;
  37. public function __construct(string $name, self $parent = null)
  38. {
  39. if (\substr($name, -1) == \DIRECTORY_SEPARATOR) {
  40. $name = \substr($name, 0, -1);
  41. }
  42. $this->name = $name;
  43. $this->parent = $parent;
  44. }
  45. public function getName(): string
  46. {
  47. return $this->name;
  48. }
  49. public function getId(): string
  50. {
  51. if ($this->id === null) {
  52. $parent = $this->getParent();
  53. if ($parent === null) {
  54. $this->id = 'index';
  55. } else {
  56. $parentId = $parent->getId();
  57. if ($parentId === 'index') {
  58. $this->id = \str_replace(':', '_', $this->name);
  59. } else {
  60. $this->id = $parentId . '/' . $this->name;
  61. }
  62. }
  63. }
  64. return $this->id;
  65. }
  66. public function getPath(): string
  67. {
  68. if ($this->path === null) {
  69. if ($this->parent === null || $this->parent->getPath() === null || $this->parent->getPath() === false) {
  70. $this->path = $this->name;
  71. } else {
  72. $this->path = $this->parent->getPath() . \DIRECTORY_SEPARATOR . $this->name;
  73. }
  74. }
  75. return $this->path;
  76. }
  77. public function getPathAsArray(): array
  78. {
  79. if ($this->pathArray === null) {
  80. if ($this->parent === null) {
  81. $this->pathArray = [];
  82. } else {
  83. $this->pathArray = $this->parent->getPathAsArray();
  84. }
  85. $this->pathArray[] = $this;
  86. }
  87. return $this->pathArray;
  88. }
  89. public function getParent(): ?self
  90. {
  91. return $this->parent;
  92. }
  93. /**
  94. * Returns the percentage of classes that has been tested.
  95. *
  96. * @return int|string
  97. */
  98. public function getTestedClassesPercent(bool $asString = true)
  99. {
  100. return Util::percent(
  101. $this->getNumTestedClasses(),
  102. $this->getNumClasses(),
  103. $asString
  104. );
  105. }
  106. /**
  107. * Returns the percentage of traits that has been tested.
  108. *
  109. * @return int|string
  110. */
  111. public function getTestedTraitsPercent(bool $asString = true)
  112. {
  113. return Util::percent(
  114. $this->getNumTestedTraits(),
  115. $this->getNumTraits(),
  116. $asString
  117. );
  118. }
  119. /**
  120. * Returns the percentage of classes and traits that has been tested.
  121. *
  122. * @return int|string
  123. */
  124. public function getTestedClassesAndTraitsPercent(bool $asString = true)
  125. {
  126. return Util::percent(
  127. $this->getNumTestedClassesAndTraits(),
  128. $this->getNumClassesAndTraits(),
  129. $asString
  130. );
  131. }
  132. /**
  133. * Returns the percentage of functions that has been tested.
  134. *
  135. * @return int|string
  136. */
  137. public function getTestedFunctionsPercent(bool $asString = true)
  138. {
  139. return Util::percent(
  140. $this->getNumTestedFunctions(),
  141. $this->getNumFunctions(),
  142. $asString
  143. );
  144. }
  145. /**
  146. * Returns the percentage of methods that has been tested.
  147. *
  148. * @return int|string
  149. */
  150. public function getTestedMethodsPercent(bool $asString = true)
  151. {
  152. return Util::percent(
  153. $this->getNumTestedMethods(),
  154. $this->getNumMethods(),
  155. $asString
  156. );
  157. }
  158. /**
  159. * Returns the percentage of functions and methods that has been tested.
  160. *
  161. * @return int|string
  162. */
  163. public function getTestedFunctionsAndMethodsPercent(bool $asString = true)
  164. {
  165. return Util::percent(
  166. $this->getNumTestedFunctionsAndMethods(),
  167. $this->getNumFunctionsAndMethods(),
  168. $asString
  169. );
  170. }
  171. /**
  172. * Returns the percentage of executed lines.
  173. *
  174. * @return int|string
  175. */
  176. public function getLineExecutedPercent(bool $asString = true)
  177. {
  178. return Util::percent(
  179. $this->getNumExecutedLines(),
  180. $this->getNumExecutableLines(),
  181. $asString
  182. );
  183. }
  184. /**
  185. * Returns the number of classes and traits.
  186. */
  187. public function getNumClassesAndTraits(): int
  188. {
  189. return $this->getNumClasses() + $this->getNumTraits();
  190. }
  191. /**
  192. * Returns the number of tested classes and traits.
  193. */
  194. public function getNumTestedClassesAndTraits(): int
  195. {
  196. return $this->getNumTestedClasses() + $this->getNumTestedTraits();
  197. }
  198. /**
  199. * Returns the classes and traits of this node.
  200. */
  201. public function getClassesAndTraits(): array
  202. {
  203. return \array_merge($this->getClasses(), $this->getTraits());
  204. }
  205. /**
  206. * Returns the number of functions and methods.
  207. */
  208. public function getNumFunctionsAndMethods(): int
  209. {
  210. return $this->getNumFunctions() + $this->getNumMethods();
  211. }
  212. /**
  213. * Returns the number of tested functions and methods.
  214. */
  215. public function getNumTestedFunctionsAndMethods(): int
  216. {
  217. return $this->getNumTestedFunctions() + $this->getNumTestedMethods();
  218. }
  219. /**
  220. * Returns the functions and methods of this node.
  221. */
  222. public function getFunctionsAndMethods(): array
  223. {
  224. return \array_merge($this->getFunctions(), $this->getMethods());
  225. }
  226. /**
  227. * Returns the classes of this node.
  228. */
  229. abstract public function getClasses(): array;
  230. /**
  231. * Returns the traits of this node.
  232. */
  233. abstract public function getTraits(): array;
  234. /**
  235. * Returns the functions of this node.
  236. */
  237. abstract public function getFunctions(): array;
  238. /**
  239. * Returns the LOC/CLOC/NCLOC of this node.
  240. */
  241. abstract public function getLinesOfCode(): array;
  242. /**
  243. * Returns the number of executable lines.
  244. */
  245. abstract public function getNumExecutableLines(): int;
  246. /**
  247. * Returns the number of executed lines.
  248. */
  249. abstract public function getNumExecutedLines(): int;
  250. /**
  251. * Returns the number of classes.
  252. */
  253. abstract public function getNumClasses(): int;
  254. /**
  255. * Returns the number of tested classes.
  256. */
  257. abstract public function getNumTestedClasses(): int;
  258. /**
  259. * Returns the number of traits.
  260. */
  261. abstract public function getNumTraits(): int;
  262. /**
  263. * Returns the number of tested traits.
  264. */
  265. abstract public function getNumTestedTraits(): int;
  266. /**
  267. * Returns the number of methods.
  268. */
  269. abstract public function getNumMethods(): int;
  270. /**
  271. * Returns the number of tested methods.
  272. */
  273. abstract public function getNumTestedMethods(): int;
  274. /**
  275. * Returns the number of functions.
  276. */
  277. abstract public function getNumFunctions(): int;
  278. /**
  279. * Returns the number of tested functions.
  280. */
  281. abstract public function getNumTestedFunctions(): int;
  282. }