Directory.php 9.8 KB

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