Directory.php 9.5 KB

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