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) |
||
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 | /** |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | public function push(...$values) |
||
193 | |||
194 | /** |
||
195 | * @inheritDoc |
||
196 | */ |
||
197 | public function reduce(callable $callback, $initial = null) |
||
201 | |||
202 | /** |
||
203 | * @inheritDoc |
||
204 | */ |
||
205 | public function remove(int $index) |
||
214 | |||
215 | /** |
||
216 | * @inheritDoc |
||
217 | */ |
||
218 | public function reverse() |
||
222 | |||
223 | /** |
||
224 | * @inheritDoc |
||
225 | */ |
||
226 | public function reversed(): \Ds\Sequence |
||
230 | |||
231 | private function reverseRange(int $a, int $b) |
||
246 | |||
247 | private function normalizeRotations(int $rotations, int $count) |
||
255 | |||
256 | /** |
||
257 | * @inheritDoc |
||
258 | */ |
||
259 | public function rotate(int $rotations) |
||
274 | |||
275 | /** |
||
276 | * @inheritDoc |
||
277 | */ |
||
278 | public function set(int $index, $value) |
||
283 | |||
284 | /** |
||
285 | * @inheritDoc |
||
286 | */ |
||
287 | View Code Duplication | public function shift() |
|
298 | |||
299 | /** |
||
300 | * @inheritDoc |
||
301 | */ |
||
302 | public function slice(int $offset, int $length = null): \Ds\Sequence |
||
310 | |||
311 | /** |
||
312 | * @inheritDoc |
||
313 | */ |
||
314 | public function sort(callable $comparator = null) |
||
322 | |||
323 | /** |
||
324 | * @inheritDoc |
||
325 | */ |
||
326 | public function sorted(callable $comparator = null): \Ds\Sequence |
||
338 | |||
339 | /** |
||
340 | * @inheritDoc |
||
341 | */ |
||
342 | public function sum() |
||
346 | |||
347 | /** |
||
348 | * @inheritDoc |
||
349 | */ |
||
350 | public function unshift(...$values) |
||
357 | |||
358 | /** |
||
359 | * |
||
360 | * |
||
361 | * @param int $index |
||
362 | */ |
||
363 | private function checkRange(int $index) |
||
369 | |||
370 | /** |
||
371 | * |
||
372 | */ |
||
373 | public function getIterator() |
||
379 | |||
380 | /** |
||
381 | * @inheritdoc |
||
382 | */ |
||
383 | public function clear() |
||
388 | |||
389 | /** |
||
390 | * @inheritdoc |
||
391 | */ |
||
392 | public function offsetSet($offset, $value) |
||
400 | |||
401 | /** |
||
402 | * @inheritdoc |
||
403 | */ |
||
404 | public function &offsetGet($offset) |
||
409 | |||
410 | /** |
||
411 | * @inheritdoc |
||
412 | */ |
||
413 | public function offsetUnset($offset) |
||
420 | |||
421 | /** |
||
422 | * @inheritdoc |
||
423 | */ |
||
424 | public function offsetExists($offset) |
||
432 | } |
||
433 |
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.