| Total Complexity | 58 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | final class Collection implements CollectionInterface |
||
| 8 | { |
||
| 9 | private array $items; |
||
| 10 | |||
| 11 | private \Generator $deferredValues; |
||
| 12 | |||
| 13 | private function __construct(array $items) |
||
| 16 | } |
||
| 17 | |||
| 18 | public static function collect(array $items = []): self |
||
| 19 | { |
||
| 20 | self::validateItems($items); |
||
| 21 | |||
| 22 | return new static($items); |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function fill(int $startIndex, int $amount, $item = null): self |
||
| 26 | { |
||
| 27 | self::validateItem($item); |
||
| 28 | |||
| 29 | $items = array_fill($startIndex, $amount, $item); |
||
| 30 | |||
| 31 | return new self($items); |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function fromString(string $string, string $delimiter = ','): self |
||
| 35 | { |
||
| 36 | return new self(explode($delimiter, trim($string))); |
||
| 37 | } |
||
| 38 | |||
| 39 | public static function later(\Generator $deferredValues): self |
||
| 40 | { |
||
| 41 | $deferred = new static([]); |
||
| 42 | $deferred->deferredValues = $deferredValues; |
||
| 43 | |||
| 44 | return $deferred; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function count(): int |
||
| 48 | { |
||
| 49 | return count($this->all()); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function map(callable $fn): self |
||
| 53 | { |
||
| 54 | return new static(array_map($fn, $this->all())); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function filter(callable $fn): self |
||
| 58 | { |
||
| 59 | return new static(array_filter($this->all(), $fn, ARRAY_FILTER_USE_BOTH)); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function each(callable $fn): self |
||
| 63 | { |
||
| 64 | $deferredValues = function () use ($fn) { |
||
| 65 | foreach ($this->all() as $key => $item) { |
||
| 66 | yield $key => $fn($item, $key); |
||
| 67 | } |
||
| 68 | }; |
||
| 69 | |||
| 70 | return self::later($deferredValues()); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function reverse(): self |
||
| 74 | { |
||
| 75 | return new static(array_reverse($this->all())); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return mixed|null |
||
| 80 | */ |
||
| 81 | public function first() |
||
| 82 | { |
||
| 83 | if ($this->empty()) { |
||
| 84 | return null; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $this->get(array_key_first($this->all())); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return mixed|null |
||
| 92 | */ |
||
| 93 | public function last() |
||
| 94 | { |
||
| 95 | if ($this->empty()) { |
||
| 96 | return null; |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this->get(array_key_last($this->all())); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param callable $fn |
||
| 104 | * @param null $initial |
||
|
|
|||
| 105 | * |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | public function reduce(callable $fn, $initial = null) |
||
| 109 | { |
||
| 110 | return array_reduce($this->all(), $fn, $initial); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function unique(): self |
||
| 114 | { |
||
| 115 | return new self(array_unique($this->all())); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function diff(Collection $collection, callable $comparator = null): self |
||
| 119 | { |
||
| 120 | return new self(array_udiff($this->all(), $collection->all(), $comparator ?? self::getObjectSafeComparator())); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function merge(Collection $collection): self |
||
| 124 | { |
||
| 125 | return new self(array_merge($this->all(), $collection->all())); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function union(Collection $collection): self |
||
| 129 | { |
||
| 130 | return $this->merge($collection)->unique(); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function intersect(Collection $collection): self |
||
| 134 | { |
||
| 135 | return new self(array_intersect($this->all(), $collection->all())); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function sort(callable $comparator = null): self |
||
| 139 | { |
||
| 140 | $items = $this->all(); |
||
| 141 | uasort($items, $comparator ?? self::getObjectSafeComparator()); |
||
| 142 | |||
| 143 | return new self($items); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function kSort(callable $comparator = null): self |
||
| 152 | } |
||
| 153 | |||
| 154 | public function empty(): bool |
||
| 155 | { |
||
| 156 | return $this->count() === 0; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function all(): array |
||
| 166 | } |
||
| 167 | |||
| 168 | public function reIndex(): self |
||
| 169 | { |
||
| 170 | return new self($this->values()); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function chunk(int $size, callable $fn): self |
||
| 174 | { |
||
| 175 | $deferred = function () use ($size, $fn) { |
||
| 176 | foreach(array_chunk($this->all(), $size, true) as $chunk) { |
||
| 177 | foreach($chunk as $index => $value) { |
||
| 178 | yield $index => $fn($value, $index); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | }; |
||
| 182 | |||
| 183 | // do the work later and revalidate processed values |
||
| 184 | return self::later($deferred()); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function deferred(): \Generator |
||
| 188 | { |
||
| 189 | foreach ($this->all() as $key => $value) { |
||
| 190 | yield $key => $value; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | public function values(): array |
||
| 195 | { |
||
| 196 | return array_values($this->all()); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function equals(Collection $collection): bool |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string|int $offset |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function has($offset): bool |
||
| 210 | { |
||
| 211 | return isset($this->all()[$offset]); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param mixed $item |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public function contains($item): bool |
||
| 220 | { |
||
| 221 | return in_array($item, $this->all(), true); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function some(callable $evaluation): bool |
||
| 225 | { |
||
| 226 | foreach ($this->all() as $key => $value) { |
||
| 227 | if ($evaluation($value, $key) === true) { |
||
| 228 | return true; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | return false; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string|int $offset |
||
| 237 | * @param mixed|null $default |
||
| 238 | * |
||
| 239 | * @return mixed|null |
||
| 240 | */ |
||
| 241 | public function get($offset, $default = null) |
||
| 242 | { |
||
| 243 | return $this->all()[$offset] ?? $default; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | public function rand() |
||
| 250 | { |
||
| 251 | return $this->all()[array_rand($this->all())]; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string|int $offset |
||
| 256 | */ |
||
| 257 | public function remove(...$offsets): self |
||
| 258 | { |
||
| 259 | $withRemoved = new static($this->all()); |
||
| 260 | foreach ($offsets as $offset) { |
||
| 261 | unset($withRemoved->items[$offset]); |
||
| 262 | } |
||
| 263 | |||
| 264 | return $withRemoved; |
||
| 265 | } |
||
| 266 | |||
| 267 | public function getIterator(): \ArrayIterator |
||
| 268 | { |
||
| 269 | return new \ArrayIterator($this->all()); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function serialize(): string |
||
| 273 | { |
||
| 274 | return \serialize(['items' => $this->all()]); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function unserialize($serialized, array $classnames = []) |
||
| 278 | { |
||
| 279 | if (isset($this->items)) { |
||
| 280 | throw new \LogicException('Cannot unserialize instance of collection'); |
||
| 281 | } |
||
| 282 | |||
| 283 | $this->items = \unserialize($serialized, $classnames)['items'] ?? []; |
||
| 284 | } |
||
| 285 | |||
| 286 | public function jsonSerialize(): array |
||
| 287 | { |
||
| 288 | return $this->all(); |
||
| 289 | } |
||
| 290 | |||
| 291 | private function unwrapDeferred(): array |
||
| 292 | { |
||
| 293 | $items = []; |
||
| 294 | foreach ($this->deferredValues as $key => $item) { |
||
| 295 | self::validateItem($item); |
||
| 296 | $items[$key] = $item; |
||
| 297 | } |
||
| 298 | |||
| 299 | return $items; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @throw \InvalidArgumentException |
||
| 304 | */ |
||
| 305 | private static function validateItems(array $items): void |
||
| 306 | { |
||
| 307 | foreach ($items as $item) { |
||
| 308 | self::validateItem($item); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | private static function validateItem($item): void |
||
| 313 | { |
||
| 314 | if (is_iterable($item)) { |
||
| 315 | throw new \InvalidArgumentException('A collection may only contain numbers, strings, or objects'); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | private static function getStringComparator(): callable |
||
| 323 | }; |
||
| 324 | } |
||
| 325 | |||
| 326 | private static function getObjectSafeComparator(): callable |
||
| 327 | { |
||
| 328 | return function ($a, $b): int { |
||
| 329 | return (\gettype($a) === \gettype($b)) ? $a <=> $b : -1; |
||
| 330 | }; |
||
| 331 | } |
||
| 332 | } |
||
| 333 |