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 Sequence 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 Sequence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait Sequence |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $internal = []; |
||
20 | |||
21 | /** |
||
22 | * @inheritDoc |
||
23 | */ |
||
24 | public function __construct($values = null) |
||
25 | { |
||
26 | if (func_num_args()) { |
||
27 | $this->pushAll($values); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @inheritdoc |
||
33 | */ |
||
34 | public function toArray(): array |
||
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | public function apply(callable $callback) |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | public function merge($values): \Ds\Sequence |
||
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | public function count(): int |
||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | View Code Duplication | public function contains(...$values): bool |
|
85 | |||
86 | /** |
||
87 | * @inheritDoc |
||
88 | */ |
||
89 | public function filter(callable $callback = null): \Ds\Sequence |
||
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | public function find($value) |
||
105 | |||
106 | /** |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | public function first() |
||
117 | |||
118 | /** |
||
119 | * @inheritDoc |
||
120 | */ |
||
121 | public function get(int $index) |
||
127 | |||
128 | /** |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | public function insert(int $index, ...$values) |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function join(string $glue = null): string |
||
147 | |||
148 | /** |
||
149 | * @inheritDoc |
||
150 | */ |
||
151 | public function last() |
||
159 | |||
160 | /** |
||
161 | * @inheritDoc |
||
162 | */ |
||
163 | public function map(callable $callback): \Ds\Sequence |
||
167 | |||
168 | /** |
||
169 | * @inheritDoc |
||
170 | */ |
||
171 | View Code Duplication | public function pop() |
|
182 | |||
183 | private function pushAll($values) |
||
191 | |||
192 | /** |
||
193 | * @inheritDoc |
||
194 | */ |
||
195 | public function push(...$values) |
||
199 | |||
200 | /** |
||
201 | * @inheritDoc |
||
202 | */ |
||
203 | public function reduce(callable $callback, $initial = null) |
||
207 | |||
208 | /** |
||
209 | * @inheritDoc |
||
210 | */ |
||
211 | public function remove(int $index) |
||
220 | |||
221 | /** |
||
222 | * @inheritDoc |
||
223 | */ |
||
224 | public function reverse() |
||
228 | |||
229 | /** |
||
230 | * @inheritDoc |
||
231 | */ |
||
232 | public function reversed(): \Ds\Sequence |
||
236 | |||
237 | private function reverseRange(int $a, int $b) |
||
252 | |||
253 | private function normalizeRotations(int $rotations, int $count) |
||
261 | |||
262 | /** |
||
263 | * @inheritDoc |
||
264 | */ |
||
265 | public function rotate(int $rotations) |
||
280 | |||
281 | /** |
||
282 | * @inheritDoc |
||
283 | */ |
||
284 | public function set(int $index, $value) |
||
289 | |||
290 | /** |
||
291 | * @inheritDoc |
||
292 | */ |
||
293 | View Code Duplication | public function shift() |
|
304 | |||
305 | /** |
||
306 | * @inheritDoc |
||
307 | */ |
||
308 | public function slice(int $offset, int $length = null): \Ds\Sequence |
||
316 | |||
317 | /** |
||
318 | * @inheritDoc |
||
319 | */ |
||
320 | public function sort(callable $comparator = null) |
||
328 | |||
329 | /** |
||
330 | * @inheritDoc |
||
331 | */ |
||
332 | public function sorted(callable $comparator = null): \Ds\Sequence |
||
344 | |||
345 | /** |
||
346 | * @inheritDoc |
||
347 | */ |
||
348 | public function sum() |
||
352 | |||
353 | /** |
||
354 | * @inheritDoc |
||
355 | */ |
||
356 | public function unshift(...$values) |
||
363 | |||
364 | /** |
||
365 | * |
||
366 | * |
||
367 | * @param int $index |
||
368 | */ |
||
369 | private function checkRange(int $index) |
||
375 | |||
376 | /** |
||
377 | * |
||
378 | */ |
||
379 | public function getIterator() |
||
385 | |||
386 | /** |
||
387 | * @inheritdoc |
||
388 | */ |
||
389 | public function clear() |
||
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | public function offsetSet($offset, $value) |
||
406 | |||
407 | /** |
||
408 | * @inheritdoc |
||
409 | */ |
||
410 | public function &offsetGet($offset) |
||
415 | |||
416 | /** |
||
417 | * @inheritdoc |
||
418 | */ |
||
419 | public function offsetUnset($offset) |
||
426 | |||
427 | /** |
||
428 | * @inheritdoc |
||
429 | */ |
||
430 | public function offsetExists($offset) |
||
438 | } |
||
439 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.