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