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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 13 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 14 | { |
||
| 15 | /** @var array */ |
||
| 16 | protected $items; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Collection constructor. |
||
| 20 | * |
||
| 21 | * @param array $items |
||
| 22 | */ |
||
| 23 | 35 | public function __construct(array $items = []) |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Generate a collection from an array of items. |
||
| 31 | * I created this method so that it's possible to extend a collection more easily. |
||
| 32 | * |
||
| 33 | * @param array $items |
||
| 34 | * |
||
| 35 | * @return Collection |
||
| 36 | */ |
||
| 37 | 10 | public static function factory(array $items = []) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Get collection as an array |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | 13 | public function toArray() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Determine if collection has a given key |
||
| 54 | * |
||
| 55 | * @param mixed $key The key to look for |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 21 | public function has($key) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Does collection have item at position? |
||
| 66 | * |
||
| 67 | * Determine if collection has an item at a particular position (indexed from one). |
||
| 68 | * Position can be positive and start from the beginning or it can be negative and |
||
| 69 | * start from the end. |
||
| 70 | * |
||
| 71 | * @param int $position |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | 2 | public function hasValueAt($position) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Get key at given position |
||
| 87 | * |
||
| 88 | * Returns the key at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 89 | * negative (start from the end). |
||
| 90 | * |
||
| 91 | * If the position does not exist, a RuntimeException is thrown. |
||
| 92 | * |
||
| 93 | * @param int $position |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | * |
||
| 97 | * @throws RuntimeException |
||
| 98 | */ |
||
| 99 | 6 | public function getKeyAt($position) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get value at given position |
||
| 116 | * |
||
| 117 | * Returns the value at the given position, starting from one. Position can be positive (start from beginning) or |
||
| 118 | * negative (start from the end). |
||
| 119 | * |
||
| 120 | * If the position does not exist, a RuntimeException is thrown. |
||
| 121 | * |
||
| 122 | * @param int $position |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | * |
||
| 126 | * @throws RuntimeException |
||
| 127 | */ |
||
| 128 | 2 | public function getValueAt($position) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Get item by key, with an optional default return value |
||
| 135 | * |
||
| 136 | * @param mixed $key |
||
| 137 | * @param mixed $default |
||
| 138 | * |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | 7 | public function get($key, $default = null) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Add an item with no regard to key |
||
| 152 | * |
||
| 153 | * @param mixed $value |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | 3 | public function add($value) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Set an item at a given key |
||
| 166 | * |
||
| 167 | * @param mixed $key |
||
| 168 | * @param mixed $value |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 5 | public function set($key, $value) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Delete an item by key |
||
| 181 | * |
||
| 182 | * @param mixed $key |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | 3 | public function delete($key) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Clear the collection of all its items. |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | 1 | public function clear() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Determine if collection contains given value |
||
| 207 | * |
||
| 208 | * @param mixed $val |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | 2 | public function contains($val) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Fetch item from collection by key and remove it from collection |
||
| 219 | * |
||
| 220 | * @param mixed $key |
||
| 221 | * |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | 1 | public function pull($key) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Join collection items using a delimiter |
||
| 235 | * |
||
| 236 | * @param string $delim |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 1 | public function join($delim = '') |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Determine if collection has any items |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 3 | public function isEmpty() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get a collection of only this collection's values (without its keys) |
||
| 257 | * |
||
| 258 | * @return Collection |
||
| 259 | */ |
||
| 260 | 1 | public function values() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get a collection of only this collection's keys |
||
| 267 | * |
||
| 268 | * @return Collection |
||
| 269 | */ |
||
| 270 | 1 | public function keys() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Get a collection with order reversed |
||
| 277 | * |
||
| 278 | * @return Collection |
||
| 279 | */ |
||
| 280 | 3 | public function reverse() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Get a collection with keys and values flipped |
||
| 287 | * |
||
| 288 | * @return Collection |
||
| 289 | */ |
||
| 290 | 1 | public function flip() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Shuffle the order of this collection's values |
||
| 301 | * |
||
| 302 | * @return Collection |
||
| 303 | */ |
||
| 304 | 1 | public function shuffle() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Get a random value from the collection |
||
| 312 | * |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | 1 | public function random() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Sort the collection |
||
| 322 | * |
||
| 323 | * @param mixed $algo |
||
| 324 | * |
||
| 325 | * @return Collection |
||
| 326 | */ |
||
| 327 | public function sort($algo = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get a new collection with only distinct values |
||
| 334 | * |
||
| 335 | * @return Collection |
||
| 336 | */ |
||
| 337 | 1 | public function distinct() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Remove all duplicate values from collection in-place |
||
| 350 | * |
||
| 351 | * @return Collection |
||
| 352 | */ |
||
| 353 | 1 | public function deduplicate() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Return a new collection with only filtered keys/values |
||
| 362 | * |
||
| 363 | * The callback accepts value, key, index and should return true if the item should be added to the returned |
||
| 364 | * collection |
||
| 365 | * |
||
| 366 | * @param callable $callback |
||
| 367 | * |
||
| 368 | * @return Collection |
||
| 369 | */ |
||
| 370 | 1 | public function filter(callable $callback) |
|
| 382 | |||
| 383 | 1 | public function fold($callback, $initial = null) |
|
| 392 | |||
| 393 | /** ++++ ++++ **/ |
||
| 394 | /** ++ Interface Compliance ++ **/ |
||
| 395 | /** ++++ ++++ **/ |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | 1 | public function jsonSerialize() |
|
| 404 | |||
| 405 | /** ++++ ++++ **/ |
||
| 406 | /** ++ Array Access Methods ++ **/ |
||
| 407 | /** ++++ ++++ **/ |
||
| 408 | |||
| 409 | /** |
||
| 410 | * {@inheritDoc} |
||
| 411 | */ |
||
| 412 | 1 | public function offsetExists($offset) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * {@inheritDoc} |
||
| 419 | */ |
||
| 420 | 2 | public function offsetGet($offset) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritDoc} |
||
| 431 | */ |
||
| 432 | 1 | public function offsetUnset($offset) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * {@inheritDoc} |
||
| 439 | */ |
||
| 440 | 1 | public function offsetSet($offset, $value) |
|
| 448 | |||
| 449 | /** ++++ ++++ **/ |
||
| 450 | /** ++ Iterator Methods ++ **/ |
||
| 451 | /** ++++ ++++ **/ |
||
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritDoc} |
||
| 455 | */ |
||
| 456 | 11 | public function current() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritDoc} |
||
| 463 | */ |
||
| 464 | 11 | public function key() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritDoc} |
||
| 471 | */ |
||
| 472 | 11 | public function next() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritDoc} |
||
| 479 | */ |
||
| 480 | 35 | public function rewind() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * {@inheritDoc} |
||
| 487 | */ |
||
| 488 | 11 | public function valid() |
|
| 492 | |||
| 493 | /** ++++ ++++ **/ |
||
| 494 | /** ++ Countable Method ++ **/ |
||
| 495 | /** ++++ ++++ **/ |
||
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritDoc} |
||
| 499 | */ |
||
| 500 | 5 | public function count() |
|
| 504 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.