| Total Complexity | 43 |
| Total Lines | 241 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 11 | class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable |
||
| 12 | { |
||
| 13 | use Method\Append, |
||
| 14 | Method\Chunk, |
||
| 15 | Method\Column; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | private $type = null; |
||
| 19 | |||
| 20 | /** @var array */ |
||
| 21 | protected $container = []; |
||
| 22 | |||
| 23 | public function __construct(string $type, iterable ...$iterables) |
||
| 24 | { |
||
| 25 | static::validateObject($type); |
||
| 26 | |||
| 27 | $this->type = $type; |
||
| 28 | |||
| 29 | foreach ($iterables as $iterable) { |
||
| 30 | foreach ($iterable as $index => $item) { |
||
| 31 | $this->add($item, $index); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | final public function type(): string |
||
| 37 | { |
||
| 38 | return $this->type; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function offsetExists($offset): bool |
||
| 42 | { |
||
| 43 | return isset($this->container[$offset]); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function offsetGet($offset): object |
||
| 47 | { |
||
| 48 | return $this->container[$offset]; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function offsetUnset($offset): void |
||
| 52 | { |
||
| 53 | if ($this->offsetExists($offset)) { |
||
| 54 | unset($this->container[$offset]); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getIterator(): \ArrayIterator |
||
| 59 | { |
||
| 60 | return new \ArrayIterator($this->container); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param mixed $index |
||
| 65 | * @param mixed $value |
||
| 66 | * |
||
| 67 | * @throws Exception\InvalidElement |
||
| 68 | */ |
||
| 69 | public function offsetSet($index, $value): void |
||
| 70 | { |
||
| 71 | $this->add($value, $index); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function jsonSerialize(): array |
||
| 77 | } |
||
| 78 | |||
| 79 | public function isCompatible(iterable $collection): bool |
||
| 80 | { |
||
| 81 | try { |
||
| 82 | foreach ($collection as $item) { |
||
| 83 | $this->validate($item); |
||
| 84 | } |
||
| 85 | } catch (Exception\InvalidElement $exception) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | return true; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function first(): object |
||
| 93 | { |
||
| 94 | reset($this->container); |
||
| 95 | |||
| 96 | return $this->current(); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function last(): object |
||
| 100 | { |
||
| 101 | end($this->container); |
||
| 102 | |||
| 103 | return $this->current(); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param object $item |
||
| 108 | * |
||
| 109 | * @throws Exception\InvalidElement |
||
| 110 | */ |
||
| 111 | public function validate(object $item): void |
||
| 112 | { |
||
| 113 | $type = $this->type(); |
||
| 114 | |||
| 115 | if (!$item instanceof $type) { |
||
| 116 | throw new Exception\InvalidElement($item, $type); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | public function map(\Closure $closure, iterable ...$collections): array |
||
| 121 | { |
||
| 122 | $result = []; |
||
| 123 | $count = $this->count(); |
||
| 124 | $values[] = array_values($this->container); |
||
|
|
|||
| 125 | |||
| 126 | foreach ($collections as $index => $collection) { |
||
| 127 | if (!$this->isCompatible($collection)) { |
||
| 128 | throw new Exception\IncompatibleIterable($collection, 'Given iterable object contain invalid element'); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($count !== count($collection)) { |
||
| 132 | throw new Exception\IncompatibleIterable( |
||
| 133 | $collection, |
||
| 134 | 'Given iterable object must contain same count elements' |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | foreach ($collection as $item) { |
||
| 139 | $values[$index + 1][] = $item; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | foreach (range(0, $this->count() - 1) as $index) { |
||
| 144 | $result[] = call_user_func( |
||
| 145 | $closure, |
||
| 146 | ...array_map(function (array $collection) use ($index) { |
||
| 147 | return $collection[$index]; |
||
| 148 | }, $values) |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $result; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function reverse(bool $preserveKeys = false): Collection |
||
| 156 | { |
||
| 157 | return new static($this->type(), array_reverse($this->container, $preserveKeys)); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function current() |
||
| 161 | { |
||
| 162 | return current($this->container); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function pop(): object |
||
| 166 | { |
||
| 167 | return array_pop($this->container); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function sum(\Closure $callback) |
||
| 171 | { |
||
| 172 | $sum = 0; |
||
| 173 | |||
| 174 | foreach ($this as $element) { |
||
| 175 | $sum += call_user_func($callback, $element); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $sum; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function count(): int |
||
| 182 | { |
||
| 183 | return count($this->container); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function add($item, $index = null): void |
||
| 187 | { |
||
| 188 | $this->validate($item); |
||
| 189 | $this->container[$index ?? $this->count()] = $item; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $name |
||
| 194 | * @param array $arguments |
||
| 195 | * |
||
| 196 | * @return Collection |
||
| 197 | */ |
||
| 198 | public static function __callStatic(string $name, array $arguments = []) |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | protected static function validateObject(string $type): void |
||
| 216 | { |
||
| 217 | if (!class_exists($type)) { |
||
| 218 | throw new Exception\UnprocessedType($type); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 |