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 GenericSequence 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 GenericSequence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait GenericSequence |
||
16 | { |
||
17 | /** |
||
18 | * @var array internal array used to store the values of the sequence. |
||
19 | */ |
||
20 | private $array = []; |
||
21 | |||
22 | /** |
||
23 | * @inheritDoc |
||
24 | */ |
||
25 | public function __construct($values = null) |
||
31 | |||
32 | /** |
||
33 | * @inheritdoc |
||
34 | */ |
||
35 | public function toArray(): array |
||
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | public function apply(callable $callback) |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | public function merge($values): Sequence |
||
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | public function count(): int |
||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | public function contains(...$values): bool |
||
81 | |||
82 | /** |
||
83 | * @inheritDoc |
||
84 | */ |
||
85 | public function filter(callable $callback = null): Sequence |
||
89 | |||
90 | /** |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function find($value) |
||
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | public function first() |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function get(int $index) |
||
121 | |||
122 | /** |
||
123 | * @inheritDoc |
||
124 | */ |
||
125 | View Code Duplication | public function insert(int $index, ...$values) |
|
134 | |||
135 | /** |
||
136 | * @inheritDoc |
||
137 | */ |
||
138 | public function join(string $glue = null): string |
||
142 | |||
143 | /** |
||
144 | * @inheritDoc |
||
145 | */ |
||
146 | public function last() |
||
154 | |||
155 | /** |
||
156 | * @inheritDoc |
||
157 | */ |
||
158 | public function map(callable $callback): Sequence |
||
162 | |||
163 | /** |
||
164 | * @inheritDoc |
||
165 | */ |
||
166 | View Code Duplication | public function pop() |
|
177 | |||
178 | /** |
||
179 | * @inheritDoc |
||
180 | */ |
||
181 | public function push(...$values) |
||
189 | |||
190 | /** |
||
191 | * @inheritDoc |
||
192 | */ |
||
193 | public function reduce(callable $callback, $initial = null) |
||
197 | |||
198 | /** |
||
199 | * @inheritDoc |
||
200 | */ |
||
201 | View Code Duplication | public function remove(int $index) |
|
212 | |||
213 | /** |
||
214 | * @inheritDoc |
||
215 | */ |
||
216 | public function reverse() |
||
220 | |||
221 | /** |
||
222 | * @inheritDoc |
||
223 | */ |
||
224 | public function reversed(): Sequence |
||
228 | |||
229 | /** |
||
230 | * Converts negative or large rotations into the minimum positive number |
||
231 | * of rotations required to rotate the sequence by a given $r. |
||
232 | */ |
||
233 | private function normalizeRotations(int $r) |
||
242 | |||
243 | /** |
||
244 | * @inheritDoc |
||
245 | */ |
||
246 | public function rotate(int $rotations) |
||
252 | |||
253 | /** |
||
254 | * @inheritDoc |
||
255 | */ |
||
256 | public function set(int $index, $value) |
||
264 | |||
265 | /** |
||
266 | * @inheritDoc |
||
267 | */ |
||
268 | View Code Duplication | public function shift() |
|
279 | |||
280 | /** |
||
281 | * @inheritDoc |
||
282 | */ |
||
283 | public function slice(int $offset, int $length = null): Sequence |
||
291 | |||
292 | /** |
||
293 | * @inheritDoc |
||
294 | */ |
||
295 | public function sort(callable $comparator = null) |
||
303 | |||
304 | /** |
||
305 | * @inheritDoc |
||
306 | */ |
||
307 | public function sorted(callable $comparator = null): Sequence |
||
313 | |||
314 | /** |
||
315 | * @inheritDoc |
||
316 | */ |
||
317 | public function sum() |
||
321 | |||
322 | /** |
||
323 | * @inheritDoc |
||
324 | */ |
||
325 | public function unshift(...$values) |
||
332 | |||
333 | /** |
||
334 | * |
||
335 | */ |
||
336 | private function validIndex(int $index) |
||
340 | |||
341 | /** |
||
342 | * |
||
343 | */ |
||
344 | public function getIterator() |
||
350 | |||
351 | /** |
||
352 | * @inheritdoc |
||
353 | */ |
||
354 | public function clear() |
||
359 | |||
360 | /** |
||
361 | * @inheritdoc |
||
362 | */ |
||
363 | public function offsetSet($offset, $value) |
||
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | public function &offsetGet($offset) |
||
383 | |||
384 | /** |
||
385 | * @inheritdoc |
||
386 | */ |
||
387 | public function offsetUnset($offset) |
||
393 | |||
394 | /** |
||
395 | * @inheritdoc |
||
396 | */ |
||
397 | public function offsetExists($offset) |
||
403 | } |
||
404 |
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.