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 | use Traits\SortTrait; |
||
7 | use Traits\SetsTrait; |
||
8 | use Traits\TerminateTrait; |
||
9 | use Traits\LimitTrait; |
||
10 | |||
11 | const TYPE_FOR = 'for'; |
||
12 | const TYPE_FOREACH = 'foreach'; |
||
13 | |||
14 | private $type = self::TYPE_FOREACH; |
||
15 | private $debug = false; |
||
16 | |||
17 | private $ops = []; |
||
18 | private $seed; |
||
19 | private $is_array; |
||
20 | private $vars = []; |
||
21 | private $fn_cnt = 0; |
||
22 | |||
23 | /** |
||
24 | * @return \Spindle\Collection |
||
25 | */ |
||
26 | 7 | public static function from($iterable, $debug = null) |
|
30 | |||
31 | /** |
||
32 | * Generator-based range() |
||
33 | * @param int|string $start |
||
34 | * @param int|string $end |
||
35 | * @param int $step |
||
36 | * @param bool $debug |
||
37 | * @return \Spindle\Collection |
||
38 | */ |
||
39 | 13 | public static function range($start, $end, $step = 1, $debug = null) |
|
59 | |||
60 | /** |
||
61 | * Generator-based repeat() |
||
62 | * @param mixed $elem |
||
63 | * @param int $count |
||
64 | * @param bool $debug |
||
65 | */ |
||
66 | 2 | public static function repeat($elem, $count, $debug = null) |
|
80 | |||
81 | /** |
||
82 | * @param iterable $seed |
||
83 | */ |
||
84 | 21 | public function __construct($seed, $debug = null, $type = null) |
|
100 | |||
101 | 8 | private function step() |
|
109 | |||
110 | /** |
||
111 | * @param string|callable $fn '$_ * 2' |
||
112 | * @return $this |
||
113 | */ |
||
114 | 4 | View Code Duplication | public function map($fn) |
126 | |||
127 | /** |
||
128 | * @param string|string[] columns |
||
129 | * @return $this |
||
130 | */ |
||
131 | 1 | public function column($columns) |
|
147 | |||
148 | /** |
||
149 | * @return \Spindle\Collection (new instance) |
||
150 | */ |
||
151 | 1 | public function flip() |
|
157 | |||
158 | /** |
||
159 | * @return \Generator|\ArrayIterator |
||
160 | */ |
||
161 | 1 | public function getIterator() |
|
177 | |||
178 | /** |
||
179 | * @return array |
||
180 | */ |
||
181 | 12 | public function toArray() |
|
203 | |||
204 | /** |
||
205 | * @return $this |
||
206 | */ |
||
207 | 1 | public function dump() |
|
212 | |||
213 | /** |
||
214 | * @return $this |
||
215 | */ |
||
216 | 1 | public function assignTo(&$var = null) |
|
221 | |||
222 | /** |
||
223 | * @return $this |
||
224 | */ |
||
225 | 1 | public function assignArrayTo(&$var = null) |
|
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | 1 | public function __toString() |
|
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | 1 | public function __debugInfo() |
|
264 | |||
265 | private static function evaluate($_seed, $_vars, $_code, $_before, $_after) |
||
281 | |||
282 | 15 | private function compile($ops) |
|
290 | |||
291 | 12 | private static function compileFor($ops, $seed) |
|
305 | |||
306 | 3 | private static function compileForeach($ops) |
|
318 | } |
||
319 |
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: