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 | const TYPE_FOR = 'for'; |
||
7 | const TYPE_FOREACH = 'foreach'; |
||
8 | |||
9 | private $type = self::TYPE_FOREACH; |
||
10 | private $debug = false; |
||
11 | |||
12 | private $ops = []; |
||
13 | private $seed; |
||
14 | private $is_array; |
||
15 | private $vars = []; |
||
16 | private $fn_cnt = 0; |
||
17 | |||
18 | /** |
||
19 | * @return \Spindle\Collection |
||
20 | */ |
||
21 | 7 | public static function from($iterable, $debug = null) |
|
25 | |||
26 | /** |
||
27 | * Generator-based range() |
||
28 | * @param int|string $start |
||
29 | * @param int|string $end |
||
30 | * @param int $step |
||
31 | * @param bool $debug |
||
32 | * @return \Spindle\Collection |
||
33 | */ |
||
34 | 13 | public static function range($start, $end, $step = 1, $debug = null) |
|
51 | |||
52 | /** |
||
53 | * Generator-based repeat() |
||
54 | * @param mixed $elem |
||
55 | * @param int $count |
||
56 | * @param bool $debug |
||
57 | */ |
||
58 | 2 | public static function repeat($elem, $count, $debug = null) |
|
72 | |||
73 | /** |
||
74 | * @param iterable $seed |
||
75 | */ |
||
76 | 21 | public function __construct($seed, $debug = null, $type = null) |
|
92 | |||
93 | 8 | private function step() |
|
101 | |||
102 | /** |
||
103 | * @param string|callable $fn '$_ > 100' |
||
104 | * @return $this |
||
105 | */ |
||
106 | 2 | View Code Duplication | public function filter($fn) |
118 | |||
119 | /** |
||
120 | * @param string|callable $fn '$_ > 100' |
||
121 | * @return $this |
||
122 | */ |
||
123 | View Code Duplication | public function reject($fn) |
|
135 | |||
136 | /** |
||
137 | * @param string|callable $fn '$_ * 2' |
||
138 | * @return $this |
||
139 | */ |
||
140 | 4 | View Code Duplication | public function map($fn) |
152 | |||
153 | /** |
||
154 | * @param string[] column |
||
155 | * @return $this |
||
156 | */ |
||
157 | 1 | public function column(array $columns) |
|
168 | |||
169 | /** |
||
170 | * @param int $offset |
||
171 | * @param ?int $length |
||
172 | * @return $this |
||
173 | */ |
||
174 | 1 | public function slice($offset, $length = null) |
|
185 | |||
186 | /** |
||
187 | * @param int $size |
||
188 | * @return $this |
||
189 | */ |
||
190 | 1 | public function chunk($size) |
|
194 | |||
195 | /** |
||
196 | * @return \Spindle\Collection (new instance) |
||
197 | */ |
||
198 | 1 | public function unique() |
|
202 | |||
203 | /** |
||
204 | * @return \Spindle\Collection (new instance) |
||
205 | */ |
||
206 | 1 | public function flip() |
|
212 | |||
213 | /** |
||
214 | * @param string|callable $fn '$_carry + $_' |
||
215 | * @param mixed $initial |
||
216 | * @return mixed |
||
217 | */ |
||
218 | 1 | public function reduce($fn, $initial = null) |
|
232 | |||
233 | /** |
||
234 | * @return int|float |
||
235 | */ |
||
236 | 3 | View Code Duplication | public function sum() |
244 | |||
245 | /** |
||
246 | * @return int|float |
||
247 | */ |
||
248 | 1 | View Code Duplication | public function product() |
256 | |||
257 | /** |
||
258 | * @return \Spindle\Collection (new instance) |
||
259 | */ |
||
260 | 1 | public function usort(callable $cmp) |
|
266 | |||
267 | /** |
||
268 | * @return \Spindle\Collection (new instance) |
||
269 | */ |
||
270 | 1 | public function rsort($sort_flags = \SORT_REGULAR) |
|
276 | |||
277 | /** |
||
278 | * @return \Spindle\Collection (new instance) |
||
279 | */ |
||
280 | 1 | public function sort($sort_flags = \SORT_REGULAR) |
|
286 | |||
287 | /** |
||
288 | * @return \Generator |
||
289 | */ |
||
290 | 1 | View Code Duplication | public function getIterator() |
303 | |||
304 | /** |
||
305 | * @return array |
||
306 | */ |
||
307 | 12 | public function toArray() |
|
329 | |||
330 | /** |
||
331 | * @return $this |
||
332 | */ |
||
333 | 1 | public function dump() |
|
338 | |||
339 | /** |
||
340 | * @return $this |
||
341 | */ |
||
342 | 1 | public function assignTo(&$var = null) |
|
347 | |||
348 | /** |
||
349 | * @return $this |
||
350 | */ |
||
351 | 1 | public function assignArrayTo(&$var = null) |
|
356 | |||
357 | /** |
||
358 | * @return string |
||
359 | */ |
||
360 | 1 | public function __toString() |
|
368 | |||
369 | /** |
||
370 | * @return array |
||
371 | */ |
||
372 | 1 | public function __debugInfo() |
|
390 | |||
391 | private static function evaluate($_seed, $_vars, $_code, $_before, $_after) |
||
407 | |||
408 | 15 | private function compile($ops) |
|
416 | |||
417 | 12 | private static function compileFor($ops, $seed) |
|
431 | |||
432 | 3 | private static function compileForeach($ops) |
|
444 | } |
||
445 |
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: