Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 4 | class Collection implements \IteratorAggregate |
||
| 5 | { |
||
| 6 | private $ops = []; |
||
| 7 | private $seed; |
||
| 8 | private $is_array; |
||
| 9 | private $vars = []; |
||
| 10 | private $fn_cnt = 0; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @return \Spindle\Collection |
||
| 14 | */ |
||
| 15 | public static function from($iterable) |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Generator-based range() |
||
| 22 | * @param int|string $start |
||
| 23 | * @param int|string $end |
||
| 24 | * @param int $step |
||
| 25 | * @return \Spindle\Collection |
||
| 26 | */ |
||
| 27 | public static function range($start, $end, $step = 1) |
||
| 31 | |||
| 32 | private static function xrange($start, $end, $step = 1) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Generator-based repeat() |
||
| 45 | * @param mixed $elem |
||
| 46 | * @param int $count |
||
| 47 | */ |
||
| 48 | public static function repeat($elem, $count) |
||
| 55 | |||
| 56 | private static function xrepeat($elem, $count) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param iterable $seed |
||
| 65 | */ |
||
| 66 | public function __construct($seed) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string|callable $fn '$_ > 100' |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function filter($fn) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param string|callable $fn '$_ * 2' |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function map($fn) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string[] column |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function column(array $columns) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param int $offset |
||
| 127 | * @param ?int $length |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function slice($offset, $length = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param int $size |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function chunk($size) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return \Spindle\Collection (new instance) |
||
| 153 | */ |
||
| 154 | public function unique() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return \Spindle\Collection (new instance) |
||
| 161 | */ |
||
| 162 | public function flip() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string|callable $fn '$_carry + $_' |
||
| 171 | * @param mixed $initial |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function reduce($fn, $initial = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return int|float |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function sum() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return int|float |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function product() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @return \Spindle\Collection (new instance) |
||
| 215 | */ |
||
| 216 | public function usort(callable $cmp) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return \Spindle\Collection (new instance) |
||
| 225 | */ |
||
| 226 | public function rsort($sort_flags = \SORT_REGULAR) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return \Spindle\Collection (new instance) |
||
| 235 | */ |
||
| 236 | public function sort($sort_flags = \SORT_REGULAR) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return \Generator |
||
| 245 | */ |
||
| 246 | View Code Duplication | public function getIterator() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function toArray() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function dump() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function assignTo(&$var = null) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function assignArrayTo(&$var = null) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function __toString() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function __debugInfo() |
||
| 339 | |||
| 340 | private static function evaluate($_seed, $_vars, $_code, $_before, $_after) |
||
| 347 | |||
| 348 | private static function compile($ops) |
||
| 355 | } |
||
| 356 |
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: