ComplexityCollection.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/complexity.
  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\Complexity;
  11. use function count;
  12. use Countable;
  13. use IteratorAggregate;
  14. /**
  15. * @psalm-immutable
  16. */
  17. final class ComplexityCollection implements Countable, IteratorAggregate
  18. {
  19. /**
  20. * @psalm-var list<Complexity>
  21. */
  22. private $items = [];
  23. public static function fromList(Complexity ...$items): self
  24. {
  25. return new self($items);
  26. }
  27. /**
  28. * @psalm-param list<Complexity> $items
  29. */
  30. private function __construct(array $items)
  31. {
  32. $this->items = $items;
  33. }
  34. /**
  35. * @psalm-return list<Complexity>
  36. */
  37. public function asArray(): array
  38. {
  39. return $this->items;
  40. }
  41. public function getIterator(): ComplexityCollectionIterator
  42. {
  43. return new ComplexityCollectionIterator($this);
  44. }
  45. public function count(): int
  46. {
  47. return count($this->items);
  48. }
  49. public function isEmpty(): bool
  50. {
  51. return empty($this->items);
  52. }
  53. public function cyclomaticComplexity(): int
  54. {
  55. $cyclomaticComplexity = 0;
  56. foreach ($this as $item) {
  57. $cyclomaticComplexity += $item->cyclomaticComplexity();
  58. }
  59. return $cyclomaticComplexity;
  60. }
  61. }