| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?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\InvalidArgumentException;
- /**
- * Represents a directory in the code coverage information tree.
- */
- final class Directory extends AbstractNode implements \IteratorAggregate
- {
- /**
- * @var AbstractNode[]
- */
- private $children = [];
- /**
- * @var Directory[]
- */
- private $directories = [];
- /**
- * @var File[]
- */
- private $files = [];
- /**
- * @var array
- */
- private $classes;
- /**
- * @var array
- */
- private $traits;
- /**
- * @var array
- */
- private $functions;
- /**
- * @var array
- */
- private $linesOfCode;
- /**
- * @var int
- */
- private $numFiles = -1;
- /**
- * @var int
- */
- private $numExecutableLines = -1;
- /**
- * @var int
- */
- private $numExecutedLines = -1;
- /**
- * @var int
- */
- private $numClasses = -1;
- /**
- * @var int
- */
- private $numTestedClasses = -1;
- /**
- * @var int
- */
- private $numTraits = -1;
- /**
- * @var int
- */
- private $numTestedTraits = -1;
- /**
- * @var int
- */
- private $numMethods = -1;
- /**
- * @var int
- */
- private $numTestedMethods = -1;
- /**
- * @var int
- */
- private $numFunctions = -1;
- /**
- * @var int
- */
- private $numTestedFunctions = -1;
- /**
- * Returns the number of files in/under this node.
- */
- public function count(): int
- {
- if ($this->numFiles === -1) {
- $this->numFiles = 0;
- foreach ($this->children as $child) {
- $this->numFiles += \count($child);
- }
- }
- return $this->numFiles;
- }
- /**
- * Returns an iterator for this node.
- */
- public function getIterator(): \RecursiveIteratorIterator
- {
- return new \RecursiveIteratorIterator(
- new Iterator($this),
- \RecursiveIteratorIterator::SELF_FIRST
- );
- }
- /**
- * Adds a new directory.
- */
- public function addDirectory(string $name): self
- {
- $directory = new self($name, $this);
- $this->children[] = $directory;
- $this->directories[] = &$this->children[\count($this->children) - 1];
- return $directory;
- }
- /**
- * Adds a new file.
- *
- * @throws InvalidArgumentException
- */
- public function addFile(string $name, array $coverageData, array $testData, bool $cacheTokens): File
- {
- $file = new File($name, $this, $coverageData, $testData, $cacheTokens);
- $this->children[] = $file;
- $this->files[] = &$this->children[\count($this->children) - 1];
- $this->numExecutableLines = -1;
- $this->numExecutedLines = -1;
- return $file;
- }
- /**
- * Returns the directories in this directory.
- */
- public function getDirectories(): array
- {
- return $this->directories;
- }
- /**
- * Returns the files in this directory.
- */
- public function getFiles(): array
- {
- return $this->files;
- }
- /**
- * Returns the child nodes of this node.
- */
- public function getChildNodes(): array
- {
- return $this->children;
- }
- /**
- * Returns the classes of this node.
- */
- public function getClasses(): array
- {
- if ($this->classes === null) {
- $this->classes = [];
- foreach ($this->children as $child) {
- $this->classes = \array_merge(
- $this->classes,
- $child->getClasses()
- );
- }
- }
- return $this->classes;
- }
- /**
- * Returns the traits of this node.
- */
- public function getTraits(): array
- {
- if ($this->traits === null) {
- $this->traits = [];
- foreach ($this->children as $child) {
- $this->traits = \array_merge(
- $this->traits,
- $child->getTraits()
- );
- }
- }
- return $this->traits;
- }
- /**
- * Returns the functions of this node.
- */
- public function getFunctions(): array
- {
- if ($this->functions === null) {
- $this->functions = [];
- foreach ($this->children as $child) {
- $this->functions = \array_merge(
- $this->functions,
- $child->getFunctions()
- );
- }
- }
- return $this->functions;
- }
- /**
- * Returns the LOC/CLOC/NCLOC of this node.
- */
- public function getLinesOfCode(): array
- {
- if ($this->linesOfCode === null) {
- $this->linesOfCode = ['loc' => 0, 'cloc' => 0, 'ncloc' => 0];
- foreach ($this->children as $child) {
- $linesOfCode = $child->getLinesOfCode();
- $this->linesOfCode['loc'] += $linesOfCode['loc'];
- $this->linesOfCode['cloc'] += $linesOfCode['cloc'];
- $this->linesOfCode['ncloc'] += $linesOfCode['ncloc'];
- }
- }
- return $this->linesOfCode;
- }
- /**
- * Returns the number of executable lines.
- */
- public function getNumExecutableLines(): int
- {
- if ($this->numExecutableLines === -1) {
- $this->numExecutableLines = 0;
- foreach ($this->children as $child) {
- $this->numExecutableLines += $child->getNumExecutableLines();
- }
- }
- return $this->numExecutableLines;
- }
- /**
- * Returns the number of executed lines.
- */
- public function getNumExecutedLines(): int
- {
- if ($this->numExecutedLines === -1) {
- $this->numExecutedLines = 0;
- foreach ($this->children as $child) {
- $this->numExecutedLines += $child->getNumExecutedLines();
- }
- }
- return $this->numExecutedLines;
- }
- /**
- * Returns the number of classes.
- */
- public function getNumClasses(): int
- {
- if ($this->numClasses === -1) {
- $this->numClasses = 0;
- foreach ($this->children as $child) {
- $this->numClasses += $child->getNumClasses();
- }
- }
- return $this->numClasses;
- }
- /**
- * Returns the number of tested classes.
- */
- public function getNumTestedClasses(): int
- {
- if ($this->numTestedClasses === -1) {
- $this->numTestedClasses = 0;
- foreach ($this->children as $child) {
- $this->numTestedClasses += $child->getNumTestedClasses();
- }
- }
- return $this->numTestedClasses;
- }
- /**
- * Returns the number of traits.
- */
- public function getNumTraits(): int
- {
- if ($this->numTraits === -1) {
- $this->numTraits = 0;
- foreach ($this->children as $child) {
- $this->numTraits += $child->getNumTraits();
- }
- }
- return $this->numTraits;
- }
- /**
- * Returns the number of tested traits.
- */
- public function getNumTestedTraits(): int
- {
- if ($this->numTestedTraits === -1) {
- $this->numTestedTraits = 0;
- foreach ($this->children as $child) {
- $this->numTestedTraits += $child->getNumTestedTraits();
- }
- }
- return $this->numTestedTraits;
- }
- /**
- * Returns the number of methods.
- */
- public function getNumMethods(): int
- {
- if ($this->numMethods === -1) {
- $this->numMethods = 0;
- foreach ($this->children as $child) {
- $this->numMethods += $child->getNumMethods();
- }
- }
- return $this->numMethods;
- }
- /**
- * Returns the number of tested methods.
- */
- public function getNumTestedMethods(): int
- {
- if ($this->numTestedMethods === -1) {
- $this->numTestedMethods = 0;
- foreach ($this->children as $child) {
- $this->numTestedMethods += $child->getNumTestedMethods();
- }
- }
- return $this->numTestedMethods;
- }
- /**
- * Returns the number of functions.
- */
- public function getNumFunctions(): int
- {
- if ($this->numFunctions === -1) {
- $this->numFunctions = 0;
- foreach ($this->children as $child) {
- $this->numFunctions += $child->getNumFunctions();
- }
- }
- return $this->numFunctions;
- }
- /**
- * Returns the number of tested functions.
- */
- public function getNumTestedFunctions(): int
- {
- if ($this->numTestedFunctions === -1) {
- $this->numTestedFunctions = 0;
- foreach ($this->children as $child) {
- $this->numTestedFunctions += $child->getNumTestedFunctions();
- }
- }
- return $this->numTestedFunctions;
- }
- }
|