| Total Complexity | 40 |
| Total Lines | 181 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
Complex classes like TCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | 1 | trait TCollection { |
|
| 13 | protected $items = []; |
||
| 14 | /** @var string Type of items in the collection */ |
||
| 15 | protected $class; |
||
| 16 | /** @var string|NULL */ |
||
| 17 | protected $uniqueProperty = NULL; |
||
| 18 | /** @var int */ |
||
| 19 | protected $maxSize = 0; |
||
| 20 | /** @var bool */ |
||
| 21 | protected $locked = false; |
||
| 22 | /** @var callable[] */ |
||
| 23 | protected $checkers = []; |
||
| 24 | |||
| 25 | public function __construct() { |
||
| 26 | 1 | $this->addChecker([$this, "checkLock"]); |
|
| 27 | 1 | $this->addChecker([$this, "checkType"]); |
|
| 28 | 1 | $this->addChecker([$this, "checkUniqueness"]); |
|
| 29 | 1 | $this->addChecker([$this, "checkSize"]); |
|
| 30 | 1 | } |
|
| 31 | |||
| 32 | public function isLocked(): bool { |
||
| 33 | 1 | return $this->locked; |
|
| 34 | } |
||
| 35 | |||
| 36 | public function lock(): void { |
||
| 37 | 1 | $this->locked = true; |
|
| 38 | 1 | } |
|
| 39 | |||
| 40 | public function count(): int { |
||
| 41 | 1 | return count($this->items); |
|
| 42 | } |
||
| 43 | |||
| 44 | public function getIterator(): \ArrayIterator { |
||
| 45 | 1 | return new \ArrayIterator($this->items); |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param int $index |
||
| 50 | */ |
||
| 51 | public function offsetExists($index): bool { |
||
| 52 | 1 | return $index >= 0 AND $index < count($this->items); |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param int|NULL $index |
||
| 57 | * @throws \OutOfRangeException |
||
| 58 | */ |
||
| 59 | public function offsetGet($index) { |
||
| 60 | 1 | if($index < 0 OR $index >= count($this->items)) { |
|
| 61 | 1 | throw new \OutOfRangeException("Offset invalid or out of range."); |
|
| 62 | } |
||
| 63 | 1 | return $this->items[$index]; |
|
| 64 | } |
||
| 65 | |||
| 66 | public function addChecker(callable $checker): void { |
||
| 67 | 1 | $this->checkers[] = $checker; |
|
| 68 | 1 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param object $newItem |
||
| 72 | */ |
||
| 73 | protected function checkLock($newItem, self $collection): void { |
||
| 74 | 1 | if($collection->locked) { |
|
| 75 | 1 | throw new \RuntimeException("Cannot add items to locked collection."); |
|
| 76 | } |
||
| 77 | 1 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param object $newItem |
||
| 81 | */ |
||
| 82 | protected function checkType($newItem, self $collection): void { |
||
| 85 | } |
||
| 86 | 1 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param object $newItem |
||
| 90 | */ |
||
| 91 | protected function checkUniqueness($newItem, self $collection): void { |
||
| 92 | 1 | $uniqueProperty = $collection->uniqueProperty; |
|
| 93 | 1 | if(is_null($uniqueProperty)) { |
|
| 94 | 1 | return; |
|
| 95 | } |
||
| 96 | 1 | foreach($collection->items as $item) { |
|
| 97 | 1 | if($newItem->$uniqueProperty === $item->$uniqueProperty) { |
|
| 98 | 1 | throw new \RuntimeException("Duplicate $uniqueProperty {$item->$uniqueProperty}."); |
|
| 99 | } |
||
| 100 | } |
||
| 101 | 1 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param object $newItem |
||
| 105 | */ |
||
| 106 | protected function checkSize($newItem, self $collection): void { |
||
| 112 | } |
||
| 113 | 1 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param object $item |
||
| 117 | */ |
||
| 118 | protected function performChecks($item): void { |
||
| 119 | 1 | foreach($this->checkers as $checker) { |
|
| 120 | 1 | call_user_func($checker, $item, $this); |
|
| 121 | } |
||
| 122 | 1 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param int|NULL $index |
||
| 126 | * @param object $item |
||
| 127 | * @throws \OutOfRangeException |
||
| 128 | * @throws \InvalidArgumentException |
||
| 129 | * @throws \RuntimeException |
||
| 130 | */ |
||
| 131 | public function offsetSet($index, $item): void { |
||
| 132 | 1 | $this->performChecks($item); |
|
| 133 | 1 | if($index === NULL) { |
|
| 134 | 1 | $this->items[] = & $item; |
|
| 135 | 1 | } elseif($index < 0 OR $index >= count($this->items)) { |
|
| 136 | 1 | throw new \OutOfRangeException("Offset invalid or out of range."); |
|
| 137 | } else { |
||
| 138 | 1 | $this->items[$index] = & $item; |
|
| 139 | } |
||
| 140 | 1 | } |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param int $index |
||
| 144 | * @throws \OutOfRangeException |
||
| 145 | */ |
||
| 146 | public function offsetUnset($index): void { |
||
| 147 | 1 | if($this->locked) { |
|
| 148 | 1 | throw new \RuntimeException("Cannot remove items from locked collection."); |
|
| 149 | 1 | } elseif($index < 0 OR $index >= count($this->items)) { |
|
| 150 | 1 | throw new \OutOfRangeException("Offset invalid or out of range."); |
|
| 151 | } |
||
| 152 | 1 | array_splice($this->items, $index, 1); |
|
| 153 | 1 | } |
|
| 154 | |||
| 155 | public function toArray(): array { |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Create new collection from array |
||
| 161 | */ |
||
| 162 | public static function fromArray(array $items, ...$args): self { |
||
| 163 | 1 | $collection = new static(...$args); |
|
|
1 ignored issue
–
show
|
|||
| 164 | 1 | foreach($items as $item) { |
|
| 165 | 1 | $collection[] = $item; |
|
| 166 | } |
||
| 167 | 1 | return $collection; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if the collection has at least 1 item matching the filter |
||
| 172 | */ |
||
| 173 | public function hasItems(array $filter = []): bool { |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get all items matching the filter |
||
| 179 | * |
||
| 180 | * @todo make it possible to use different comparing rules |
||
| 181 | */ |
||
| 182 | public function getItems(array $filter = []): array { |
||
| 193 | 1 | })); |
|
| 194 | } |
||
| 196 | ?> |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.