ComplexityCollection.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 array_filter;
  12. use function array_merge;
  13. use function array_reverse;
  14. use function array_values;
  15. use function count;
  16. use function usort;
  17. use Countable;
  18. use IteratorAggregate;
  19. /**
  20. * @psalm-immutable
  21. */
  22. final class ComplexityCollection implements Countable, IteratorAggregate
  23. {
  24. /**
  25. * @psalm-var list<Complexity>
  26. */
  27. private readonly array $items;
  28. public static function fromList(Complexity ...$items): self
  29. {
  30. return new self($items);
  31. }
  32. /**
  33. * @psalm-param list<Complexity> $items
  34. */
  35. private function __construct(array $items)
  36. {
  37. $this->items = $items;
  38. }
  39. /**
  40. * @psalm-return list<Complexity>
  41. */
  42. public function asArray(): array
  43. {
  44. return $this->items;
  45. }
  46. public function getIterator(): ComplexityCollectionIterator
  47. {
  48. return new ComplexityCollectionIterator($this);
  49. }
  50. /**
  51. * @psalm-return non-negative-int
  52. */
  53. public function count(): int
  54. {
  55. return count($this->items);
  56. }
  57. public function isEmpty(): bool
  58. {
  59. return empty($this->items);
  60. }
  61. /**
  62. * @psalm-return non-negative-int
  63. */
  64. public function cyclomaticComplexity(): int
  65. {
  66. $cyclomaticComplexity = 0;
  67. foreach ($this as $item) {
  68. $cyclomaticComplexity += $item->cyclomaticComplexity();
  69. }
  70. return $cyclomaticComplexity;
  71. }
  72. public function isFunction(): self
  73. {
  74. return new self(
  75. array_values(
  76. array_filter(
  77. $this->items,
  78. static fn (Complexity $complexity): bool => $complexity->isFunction(),
  79. ),
  80. ),
  81. );
  82. }
  83. public function isMethod(): self
  84. {
  85. return new self(
  86. array_values(
  87. array_filter(
  88. $this->items,
  89. static fn (Complexity $complexity): bool => $complexity->isMethod(),
  90. ),
  91. ),
  92. );
  93. }
  94. public function mergeWith(self $other): self
  95. {
  96. return new self(
  97. array_merge(
  98. $this->asArray(),
  99. $other->asArray(),
  100. ),
  101. );
  102. }
  103. public function sortByDescendingCyclomaticComplexity(): self
  104. {
  105. $items = $this->items;
  106. usort(
  107. $items,
  108. static function (Complexity $a, Complexity $b): int
  109. {
  110. return $a->cyclomaticComplexity() <=> $b->cyclomaticComplexity();
  111. },
  112. );
  113. return new self(array_reverse($items));
  114. }
  115. }