Directory.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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\InvalidArgumentException;
  12. /**
  13. * Represents a directory in the code coverage information tree.
  14. */
  15. final class Directory extends AbstractNode implements \IteratorAggregate
  16. {
  17. /**
  18. * @var AbstractNode[]
  19. */
  20. private $children = [];
  21. /**
  22. * @var Directory[]
  23. */
  24. private $directories = [];
  25. /**
  26. * @var File[]
  27. */
  28. private $files = [];
  29. /**
  30. * @var array
  31. */
  32. private $classes;
  33. /**
  34. * @var array
  35. */
  36. private $traits;
  37. /**
  38. * @var array
  39. */
  40. private $functions;
  41. /**
  42. * @var array
  43. */
  44. private $linesOfCode;
  45. /**
  46. * @var int
  47. */
  48. private $numFiles = -1;
  49. /**
  50. * @var int
  51. */
  52. private $numExecutableLines = -1;
  53. /**
  54. * @var int
  55. */
  56. private $numExecutedLines = -1;
  57. /**
  58. * @var int
  59. */
  60. private $numClasses = -1;
  61. /**
  62. * @var int
  63. */
  64. private $numTestedClasses = -1;
  65. /**
  66. * @var int
  67. */
  68. private $numTraits = -1;
  69. /**
  70. * @var int
  71. */
  72. private $numTestedTraits = -1;
  73. /**
  74. * @var int
  75. */
  76. private $numMethods = -1;
  77. /**
  78. * @var int
  79. */
  80. private $numTestedMethods = -1;
  81. /**
  82. * @var int
  83. */
  84. private $numFunctions = -1;
  85. /**
  86. * @var int
  87. */
  88. private $numTestedFunctions = -1;
  89. /**
  90. * Returns the number of files in/under this node.
  91. */
  92. public function count(): int
  93. {
  94. if ($this->numFiles === -1) {
  95. $this->numFiles = 0;
  96. foreach ($this->children as $child) {
  97. $this->numFiles += \count($child);
  98. }
  99. }
  100. return $this->numFiles;
  101. }
  102. /**
  103. * Returns an iterator for this node.
  104. */
  105. public function getIterator(): \RecursiveIteratorIterator
  106. {
  107. return new \RecursiveIteratorIterator(
  108. new Iterator($this),
  109. \RecursiveIteratorIterator::SELF_FIRST
  110. );
  111. }
  112. /**
  113. * Adds a new directory.
  114. */
  115. public function addDirectory(string $name): self
  116. {
  117. $directory = new self($name, $this);
  118. $this->children[] = $directory;
  119. $this->directories[] = &$this->children[\count($this->children) - 1];
  120. return $directory;
  121. }
  122. /**
  123. * Adds a new file.
  124. *
  125. * @throws InvalidArgumentException
  126. */
  127. public function addFile(string $name, array $coverageData, array $testData, bool $cacheTokens): File
  128. {
  129. $file = new File($name, $this, $coverageData, $testData, $cacheTokens);
  130. $this->children[] = $file;
  131. $this->files[] = &$this->children[\count($this->children) - 1];
  132. $this->numExecutableLines = -1;
  133. $this->numExecutedLines = -1;
  134. return $file;
  135. }
  136. /**
  137. * Returns the directories in this directory.
  138. */
  139. public function getDirectories(): array
  140. {
  141. return $this->directories;
  142. }
  143. /**
  144. * Returns the files in this directory.
  145. */
  146. public function getFiles(): array
  147. {
  148. return $this->files;
  149. }
  150. /**
  151. * Returns the child nodes of this node.
  152. */
  153. public function getChildNodes(): array
  154. {
  155. return $this->children;
  156. }
  157. /**
  158. * Returns the classes of this node.
  159. */
  160. public function getClasses(): array
  161. {
  162. if ($this->classes === null) {
  163. $this->classes = [];
  164. foreach ($this->children as $child) {
  165. $this->classes = \array_merge(
  166. $this->classes,
  167. $child->getClasses()
  168. );
  169. }
  170. }
  171. return $this->classes;
  172. }
  173. /**
  174. * Returns the traits of this node.
  175. */
  176. public function getTraits(): array
  177. {
  178. if ($this->traits === null) {
  179. $this->traits = [];
  180. foreach ($this->children as $child) {
  181. $this->traits = \array_merge(
  182. $this->traits,
  183. $child->getTraits()
  184. );
  185. }
  186. }
  187. return $this->traits;
  188. }
  189. /**
  190. * Returns the functions of this node.
  191. */
  192. public function getFunctions(): array
  193. {
  194. if ($this->functions === null) {
  195. $this->functions = [];
  196. foreach ($this->children as $child) {
  197. $this->functions = \array_merge(
  198. $this->functions,
  199. $child->getFunctions()
  200. );
  201. }
  202. }
  203. return $this->functions;
  204. }
  205. /**
  206. * Returns the LOC/CLOC/NCLOC of this node.
  207. */
  208. public function getLinesOfCode(): array
  209. {
  210. if ($this->linesOfCode === null) {
  211. $this->linesOfCode = ['loc' => 0, 'cloc' => 0, 'ncloc' => 0];
  212. foreach ($this->children as $child) {
  213. $linesOfCode = $child->getLinesOfCode();
  214. $this->linesOfCode['loc'] += $linesOfCode['loc'];
  215. $this->linesOfCode['cloc'] += $linesOfCode['cloc'];
  216. $this->linesOfCode['ncloc'] += $linesOfCode['ncloc'];
  217. }
  218. }
  219. return $this->linesOfCode;
  220. }
  221. /**
  222. * Returns the number of executable lines.
  223. */
  224. public function getNumExecutableLines(): int
  225. {
  226. if ($this->numExecutableLines === -1) {
  227. $this->numExecutableLines = 0;
  228. foreach ($this->children as $child) {
  229. $this->numExecutableLines += $child->getNumExecutableLines();
  230. }
  231. }
  232. return $this->numExecutableLines;
  233. }
  234. /**
  235. * Returns the number of executed lines.
  236. */
  237. public function getNumExecutedLines(): int
  238. {
  239. if ($this->numExecutedLines === -1) {
  240. $this->numExecutedLines = 0;
  241. foreach ($this->children as $child) {
  242. $this->numExecutedLines += $child->getNumExecutedLines();
  243. }
  244. }
  245. return $this->numExecutedLines;
  246. }
  247. /**
  248. * Returns the number of classes.
  249. */
  250. public function getNumClasses(): int
  251. {
  252. if ($this->numClasses === -1) {
  253. $this->numClasses = 0;
  254. foreach ($this->children as $child) {
  255. $this->numClasses += $child->getNumClasses();
  256. }
  257. }
  258. return $this->numClasses;
  259. }
  260. /**
  261. * Returns the number of tested classes.
  262. */
  263. public function getNumTestedClasses(): int
  264. {
  265. if ($this->numTestedClasses === -1) {
  266. $this->numTestedClasses = 0;
  267. foreach ($this->children as $child) {
  268. $this->numTestedClasses += $child->getNumTestedClasses();
  269. }
  270. }
  271. return $this->numTestedClasses;
  272. }
  273. /**
  274. * Returns the number of traits.
  275. */
  276. public function getNumTraits(): int
  277. {
  278. if ($this->numTraits === -1) {
  279. $this->numTraits = 0;
  280. foreach ($this->children as $child) {
  281. $this->numTraits += $child->getNumTraits();
  282. }
  283. }
  284. return $this->numTraits;
  285. }
  286. /**
  287. * Returns the number of tested traits.
  288. */
  289. public function getNumTestedTraits(): int
  290. {
  291. if ($this->numTestedTraits === -1) {
  292. $this->numTestedTraits = 0;
  293. foreach ($this->children as $child) {
  294. $this->numTestedTraits += $child->getNumTestedTraits();
  295. }
  296. }
  297. return $this->numTestedTraits;
  298. }
  299. /**
  300. * Returns the number of methods.
  301. */
  302. public function getNumMethods(): int
  303. {
  304. if ($this->numMethods === -1) {
  305. $this->numMethods = 0;
  306. foreach ($this->children as $child) {
  307. $this->numMethods += $child->getNumMethods();
  308. }
  309. }
  310. return $this->numMethods;
  311. }
  312. /**
  313. * Returns the number of tested methods.
  314. */
  315. public function getNumTestedMethods(): int
  316. {
  317. if ($this->numTestedMethods === -1) {
  318. $this->numTestedMethods = 0;
  319. foreach ($this->children as $child) {
  320. $this->numTestedMethods += $child->getNumTestedMethods();
  321. }
  322. }
  323. return $this->numTestedMethods;
  324. }
  325. /**
  326. * Returns the number of functions.
  327. */
  328. public function getNumFunctions(): int
  329. {
  330. if ($this->numFunctions === -1) {
  331. $this->numFunctions = 0;
  332. foreach ($this->children as $child) {
  333. $this->numFunctions += $child->getNumFunctions();
  334. }
  335. }
  336. return $this->numFunctions;
  337. }
  338. /**
  339. * Returns the number of tested functions.
  340. */
  341. public function getNumTestedFunctions(): int
  342. {
  343. if ($this->numTestedFunctions === -1) {
  344. $this->numTestedFunctions = 0;
  345. foreach ($this->children as $child) {
  346. $this->numTestedFunctions += $child->getNumTestedFunctions();
  347. }
  348. }
  349. return $this->numTestedFunctions;
  350. }
  351. }