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 namespace Arcanedev\Support; |
||
| 15 | class Collection extends IlluminateCollection |
||
| 16 | { |
||
| 17 | /* ------------------------------------------------------------------------------------------------ |
||
| 18 | | Constructor |
||
| 19 | | ------------------------------------------------------------------------------------------------ |
||
| 20 | */ |
||
| 21 | /** |
||
| 22 | * Create a new collection. |
||
| 23 | * |
||
| 24 | * @param mixed $items |
||
| 25 | */ |
||
| 26 | 1136 | public function __construct($items = []) |
|
| 30 | |||
| 31 | /* ------------------------------------------------------------------------------------------------ |
||
| 32 | | Main Functions |
||
| 33 | | ------------------------------------------------------------------------------------------------ |
||
| 34 | */ |
||
| 35 | /** |
||
| 36 | * Get the median of a given key. |
||
| 37 | * |
||
| 38 | * @param null $key |
||
| 39 | * @return mixed|null |
||
| 40 | */ |
||
| 41 | 40 | public function median($key = null) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get the mode of a given key. |
||
| 65 | * |
||
| 66 | * @param null $key |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | 32 | public function mode($key = null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get the items in the collection whose keys are not present in the given items. |
||
| 94 | * |
||
| 95 | * @param mixed $items |
||
| 96 | * @return static |
||
| 97 | */ |
||
| 98 | 8 | public function diffKeys($items) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Run a filter over each of the items. |
||
| 105 | * |
||
| 106 | * @param callable|null $callback |
||
| 107 | * @return static |
||
| 108 | */ |
||
| 109 | 80 | public function filter(callable $callback = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get the first item from the collection. |
||
| 128 | * |
||
| 129 | * @param callable|null $callback |
||
| 130 | * @param mixed $default |
||
| 131 | * @return mixed |
||
| 132 | */ |
||
| 133 | 64 | public function first(callable $callback = null, $default = null) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Filter items by the given key value pair. |
||
| 140 | * |
||
| 141 | * @param string $key |
||
| 142 | * @param array $values |
||
| 143 | * @param bool $strict |
||
| 144 | * |
||
| 145 | * @return static |
||
| 146 | */ |
||
| 147 | 27 | public function whereIn($key, array $values, $strict = true) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Group an associative array by a field or using a callback. |
||
| 156 | * |
||
| 157 | * @param callable|string $groupBy |
||
| 158 | * @param bool $preserveKeys |
||
| 159 | * @return static |
||
| 160 | */ |
||
| 161 | 48 | public function groupBy($groupBy, $preserveKeys = false) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Key an associative array by a field or using a callback. |
||
| 188 | * |
||
| 189 | * @param callable|string $keyBy |
||
| 190 | * @return static |
||
| 191 | */ |
||
| 192 | 16 | public function keyBy($keyBy) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get the last item from the collection. |
||
| 207 | * |
||
| 208 | * @param callable|null $callback |
||
| 209 | * @param mixed $default |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | 56 | public function last(callable $callback = null, $default = null) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Create a collection by using this collection for keys and another for its values. |
||
| 219 | * |
||
| 220 | * @param mixed $values |
||
| 221 | * @return static |
||
| 222 | */ |
||
| 223 | 16 | public function combine($values) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Union the collection with the given items. |
||
| 230 | * |
||
| 231 | * @param mixed $items |
||
| 232 | * @return static |
||
| 233 | */ |
||
| 234 | 24 | public function union($items) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Pass the collection to the given callback and return the result. |
||
| 241 | * |
||
| 242 | * @param callable $callback |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | 8 | public function pipe(callable $callback) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Reverse items order. |
||
| 252 | * |
||
| 253 | * @return static |
||
| 254 | */ |
||
| 255 | 8 | public function reverse() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Shuffle the items in the collection. |
||
| 262 | * |
||
| 263 | * @param int $seed |
||
| 264 | * @return static |
||
| 265 | */ |
||
| 266 | public function shuffle($seed = null) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Convert the object into something JSON serializable. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function jsonSerialize() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the collection of items as JSON. |
||
| 305 | * |
||
| 306 | * @param int $options |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 20 | public function toJson($options = 0) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Results array of items from Collection or Arrayable. |
||
| 316 | * |
||
| 317 | * @param mixed $items |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | 1136 | protected function getArrayableItems($items) |
|
| 336 | |||
| 337 | /* ------------------------------------------------------------------------------------------------ |
||
| 338 | | Custom Functions |
||
| 339 | | ------------------------------------------------------------------------------------------------ |
||
| 340 | */ |
||
| 341 | /** |
||
| 342 | * Reset the collection. |
||
| 343 | * |
||
| 344 | * @return self |
||
| 345 | */ |
||
| 346 | 8 | public function reset() |
|
| 352 | } |
||
| 353 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: