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 |
||
13 | trait Sequence |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $internal = []; |
||
19 | |||
20 | /** |
||
21 | * @inheritDoc |
||
22 | */ |
||
23 | 1661 | public function __construct($values = null) |
|
29 | |||
30 | /** |
||
31 | * @inheritdoc |
||
32 | */ |
||
33 | 898 | public function toArray(): array |
|
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | 14 | public function apply(callable $callback) |
|
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | 20 | public function merge($values): \Ds\Sequence |
|
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | 1374 | public function count(): int |
|
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | 24 | public function contains(...$values): bool |
|
81 | |||
82 | /** |
||
83 | * @inheritDoc |
||
84 | */ |
||
85 | 16 | public function filter(callable $callback = null): \Ds\Sequence |
|
89 | |||
90 | /** |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | 40 | public function find($value) |
|
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | 13 | public function first() |
|
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | 146 | public function get(int $index) |
|
119 | |||
120 | /** |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | 46 | public function insert(int $index, ...$values) |
|
131 | |||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | 300 | public function join(string $glue = null): string |
|
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | 11 | public function last() |
|
151 | |||
152 | /** |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | 14 | public function map(callable $callback): \Ds\Sequence |
|
159 | |||
160 | /** |
||
161 | * @inheritDoc |
||
162 | */ |
||
163 | 20 | View Code Duplication | public function pop() |
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | 1655 | public function push(...$values) |
|
185 | |||
186 | /** |
||
187 | * @inheritDoc |
||
188 | */ |
||
189 | 16 | public function reduce(callable $callback, $initial = null) |
|
193 | |||
194 | /** |
||
195 | * @inheritDoc |
||
196 | */ |
||
197 | 35 | public function remove(int $index) |
|
206 | |||
207 | /** |
||
208 | * @inheritDoc |
||
209 | */ |
||
210 | 10 | public function reverse() |
|
214 | |||
215 | /** |
||
216 | * @inheritDoc |
||
217 | */ |
||
218 | 10 | public function reversed(): \Ds\Sequence |
|
222 | |||
223 | 18 | private function normalizeRotations(int $rotations, int $count) |
|
231 | |||
232 | /** |
||
233 | * @inheritDoc |
||
234 | */ |
||
235 | 18 | public function rotate(int $rotations) |
|
247 | |||
248 | /** |
||
249 | * @inheritDoc |
||
250 | */ |
||
251 | 40 | public function set(int $index, $value) |
|
256 | |||
257 | /** |
||
258 | * @inheritDoc |
||
259 | */ |
||
260 | 40 | View Code Duplication | public function shift() |
271 | |||
272 | /** |
||
273 | * @inheritDoc |
||
274 | */ |
||
275 | 397 | public function slice(int $offset, int $length = null): \Ds\Sequence |
|
283 | |||
284 | /** |
||
285 | * @inheritDoc |
||
286 | */ |
||
287 | 4 | public function sort(callable $comparator = null) |
|
295 | |||
296 | /** |
||
297 | * @inheritDoc |
||
298 | */ |
||
299 | 5 | public function sorted(callable $comparator = null): \Ds\Sequence |
|
311 | |||
312 | /** |
||
313 | * @inheritDoc |
||
314 | */ |
||
315 | 21 | public function sum() |
|
319 | |||
320 | /** |
||
321 | * @inheritDoc |
||
322 | */ |
||
323 | 28 | public function unshift(...$values) |
|
330 | |||
331 | /** |
||
332 | * |
||
333 | * |
||
334 | * @param int $index |
||
335 | */ |
||
336 | 251 | private function checkRange(int $index) |
|
342 | |||
343 | /** |
||
344 | * |
||
345 | */ |
||
346 | 52 | public function getIterator() |
|
352 | |||
353 | /** |
||
354 | * @inheritdoc |
||
355 | */ |
||
356 | 8 | public function clear() |
|
361 | |||
362 | /** |
||
363 | * @inheritdoc |
||
364 | */ |
||
365 | 35 | public function offsetSet($offset, $value) |
|
373 | |||
374 | /** |
||
375 | * @inheritdoc |
||
376 | */ |
||
377 | 118 | public function &offsetGet($offset) |
|
382 | |||
383 | /** |
||
384 | * @inheritdoc |
||
385 | */ |
||
386 | 22 | public function offsetUnset($offset) |
|
393 | |||
394 | /** |
||
395 | * @inheritdoc |
||
396 | */ |
||
397 | 156 | public function offsetExists($offset) |
|
405 | } |
||
406 |
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.