| Total Complexity | 45 |
| Total Lines | 382 |
| 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 |
||
| 27 | class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * The collection data. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $items = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Create a new collection instance if the value isn't one already. |
||
| 38 | * |
||
| 39 | * @param mixed $items |
||
| 40 | * @return Collection |
||
| 41 | */ |
||
| 42 | public static function make($items = []): Collection |
||
| 43 | { |
||
| 44 | return new static($items); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Wrap the given value in a collection if applicable. |
||
| 49 | * |
||
| 50 | * @param mixed $value |
||
| 51 | * @return static |
||
| 52 | */ |
||
| 53 | public static function wrap($value) |
||
| 54 | { |
||
| 55 | return $value instanceof self |
||
| 56 | ? new static($value) |
||
| 57 | : new static(ArrayHelper::wrap($value)); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * set data. |
||
| 62 | * |
||
| 63 | * @param mixed $items |
||
| 64 | */ |
||
| 65 | public function __construct($items = []) |
||
| 66 | { |
||
| 67 | $this->items = $this->getArrayableItems($items); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return all items. |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function all(): array |
||
| 76 | { |
||
| 77 | return $this->items; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Return specific items. |
||
| 82 | * |
||
| 83 | * @param array $keys |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function only(array $keys): array |
||
| 87 | { |
||
| 88 | $return = []; |
||
| 89 | foreach ($keys as $key) { |
||
| 90 | $value = $this->get($key); |
||
| 91 | if (!is_null($value)) { |
||
| 92 | $return[$key] = $value; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | return $return; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get all items except for those with the specified keys. |
||
| 100 | * |
||
| 101 | * @param mixed $keys |
||
| 102 | * @return Collection |
||
| 103 | */ |
||
| 104 | public function except($keys): Collection |
||
| 105 | { |
||
| 106 | $keys = is_array($keys) ? $keys : func_get_args(); |
||
| 107 | return new static(ArrayHelper::except($this->items, $keys)); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Merge data. |
||
| 112 | * |
||
| 113 | * @param Collection|array $items |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function merge($items): array |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * To determine Whether the specified element exists. |
||
| 127 | * |
||
| 128 | * @param string $key |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function has($key): bool |
||
| 132 | { |
||
| 133 | return array_key_exists($key, $this->items); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Retrieve the first item. |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function first() |
||
| 142 | { |
||
| 143 | return reset($this->items); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Retrieve the last item. |
||
| 148 | * |
||
| 149 | * @return mixed |
||
| 150 | */ |
||
| 151 | public function last() |
||
| 152 | { |
||
| 153 | $end = end($this->items); |
||
| 154 | reset($this->items); |
||
| 155 | return $end; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * add the item value. |
||
| 160 | * |
||
| 161 | * @param string $key |
||
| 162 | * @param mixed $value |
||
| 163 | */ |
||
| 164 | public function add($key, $value) |
||
| 165 | { |
||
| 166 | ArrayHelper::set($this->items, $key, $value); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set the item value. |
||
| 171 | * |
||
| 172 | * @param string $key |
||
| 173 | * @param mixed $value |
||
| 174 | */ |
||
| 175 | public function set($key, $value) |
||
| 176 | { |
||
| 177 | if (is_null($key)) { |
||
|
|
|||
| 178 | $this->items[] = $value; |
||
| 179 | } else { |
||
| 180 | $this->items[$key] = $value; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieve item from Collection. |
||
| 186 | * |
||
| 187 | * @param string $key |
||
| 188 | * @param mixed $default |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | public function get($key, $default = null) |
||
| 192 | { |
||
| 193 | return ArrayHelper::get($this->items, $key, $default); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Remove item form Collection. |
||
| 198 | * |
||
| 199 | * @param string $key |
||
| 200 | */ |
||
| 201 | public function forget($key) |
||
| 202 | { |
||
| 203 | ArrayHelper::forget($this->items, $key); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Build to array. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function toArray(): array |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Build to json. |
||
| 218 | * |
||
| 219 | * @param int $option |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function toJson($option = JSON_UNESCAPED_UNICODE): string |
||
| 223 | { |
||
| 224 | return Json::encode($this->all(), $option); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * To string. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function __toString(): string |
||
| 233 | { |
||
| 234 | return $this->toJson(); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Specify data which should be serialized to JSON. |
||
| 239 | * |
||
| 240 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
| 241 | * which is a value of any type other than a resource |
||
| 242 | */ |
||
| 243 | public function jsonSerialize() |
||
| 244 | { |
||
| 245 | return $this->items; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * String representation of object. |
||
| 250 | * |
||
| 251 | * @return string the string representation of the object or null |
||
| 252 | */ |
||
| 253 | public function serialize(): string |
||
| 254 | { |
||
| 255 | return serialize($this->items); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Retrieve an external iterator. |
||
| 260 | * |
||
| 261 | * @return ArrayIterator|Traversable An instance of an object implementing Iterator or Traversable |
||
| 262 | */ |
||
| 263 | public function getIterator() |
||
| 264 | { |
||
| 265 | return new ArrayIterator($this->items); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Count elements of an object. |
||
| 270 | * |
||
| 271 | * @return int The custom count as an integer. |
||
| 272 | * The return value is cast to an integer |
||
| 273 | */ |
||
| 274 | public function count(): int |
||
| 275 | { |
||
| 276 | return count($this->items); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Constructs the object. |
||
| 281 | * |
||
| 282 | * @param string $serialized The string representation of the object. |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function unserialize($serialized) |
||
| 286 | { |
||
| 287 | $this->items = unserialize($serialized); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get a data by key. |
||
| 292 | * |
||
| 293 | * @param string $key |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function __get($key) |
||
| 297 | { |
||
| 298 | return $this->get($key); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Assigns a value to the specified data. |
||
| 303 | * |
||
| 304 | * @param string $key |
||
| 305 | * @param mixed $value |
||
| 306 | */ |
||
| 307 | public function __set($key, $value) |
||
| 308 | { |
||
| 309 | $this->set($key, $value); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Whether or not an data exists by key. |
||
| 314 | * |
||
| 315 | * @param string $key |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function __isset($key): bool |
||
| 319 | { |
||
| 320 | return $this->has($key); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Unsets an data by key. |
||
| 325 | * |
||
| 326 | * @param string $key |
||
| 327 | */ |
||
| 328 | public function __unset($key) |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * var_export. |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function __set_state(): array |
||
| 339 | { |
||
| 340 | return $this->all(); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Whether a offset exists. |
||
| 345 | * |
||
| 346 | * @param mixed $offset An offset to check for. |
||
| 347 | * @return bool true on success or false on failure. |
||
| 348 | * The return value will be casted to boolean if non-boolean was returned |
||
| 349 | */ |
||
| 350 | public function offsetExists($offset): bool |
||
| 351 | { |
||
| 352 | return $this->has($offset); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Offset to unset. |
||
| 357 | * |
||
| 358 | * @param mixed $offset The offset to unset. |
||
| 359 | */ |
||
| 360 | public function offsetUnset($offset) |
||
| 361 | { |
||
| 362 | if ($this->offsetExists($offset)) { |
||
| 363 | $this->forget($offset); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Offset to retrieve. |
||
| 369 | * |
||
| 370 | * @param mixed $offset The offset to retrieve. |
||
| 371 | * @return mixed Can return all value types |
||
| 372 | */ |
||
| 373 | public function offsetGet($offset) |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Offset to set. |
||
| 380 | * |
||
| 381 | * @param mixed $offset The offset to assign the value to. |
||
| 382 | * @param mixed $value The value to set. |
||
| 383 | */ |
||
| 384 | public function offsetSet($offset, $value) |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Results array of items from Collection or Arrayable. |
||
| 391 | * |
||
| 392 | * @param mixed $items |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | protected function getArrayableItems($items) |
||
| 396 | { |
||
| 397 | if (is_array($items)) { |
||
| 398 | return $items; |
||
| 409 | } |
||
| 410 | } |