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 |
||
16 | trait GenericSequence |
||
17 | { |
||
18 | /** |
||
19 | * @var array internal array used to store the values of the sequence. |
||
20 | */ |
||
21 | private $array = []; |
||
22 | |||
23 | /** |
||
24 | * @inheritDoc |
||
25 | */ |
||
26 | 1698 | public function __construct($values = null) |
|
32 | |||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | 932 | public function toArray(): array |
|
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 14 | public function apply(callable $callback) |
|
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 20 | public function merge($values): Sequence |
|
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | 1432 | public function count(): int |
|
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | 24 | public function contains(...$values): bool |
|
82 | |||
83 | /** |
||
84 | * @inheritDoc |
||
85 | */ |
||
86 | 16 | public function filter(callable $callback = null): Sequence |
|
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | 40 | public function find($value) |
|
98 | |||
99 | /** |
||
100 | * @inheritDoc |
||
101 | */ |
||
102 | 13 | public function first() |
|
110 | |||
111 | /** |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | 146 | public function get(int $index) |
|
122 | |||
123 | /** |
||
124 | * @inheritDoc |
||
125 | */ |
||
126 | 46 | View Code Duplication | public function insert(int $index, ...$values) |
134 | |||
135 | /** |
||
136 | * @inheritDoc |
||
137 | */ |
||
138 | 300 | public function join(string $glue = null): string |
|
142 | |||
143 | /** |
||
144 | * @inheritDoc |
||
145 | */ |
||
146 | 11 | public function last() |
|
154 | |||
155 | /** |
||
156 | * @inheritDoc |
||
157 | */ |
||
158 | 14 | public function map(callable $callback): Sequence |
|
162 | |||
163 | /** |
||
164 | * @inheritDoc |
||
165 | */ |
||
166 | 20 | View Code Duplication | public function pop() |
176 | |||
177 | /** |
||
178 | * Pushes all values of either an array or traversable object. |
||
179 | */ |
||
180 | 1265 | private function pushAll($values) |
|
187 | |||
188 | /** |
||
189 | * @inheritDoc |
||
190 | */ |
||
191 | 82 | public function push(...$values) |
|
195 | |||
196 | /** |
||
197 | * @inheritDoc |
||
198 | */ |
||
199 | 16 | public function reduce(callable $callback, $initial = null) |
|
203 | |||
204 | /** |
||
205 | * @inheritDoc |
||
206 | */ |
||
207 | 35 | View Code Duplication | public function remove(int $index) |
217 | |||
218 | /** |
||
219 | * @inheritDoc |
||
220 | */ |
||
221 | 10 | public function reverse() |
|
225 | |||
226 | /** |
||
227 | * @inheritDoc |
||
228 | */ |
||
229 | 10 | public function reversed(): Sequence |
|
233 | |||
234 | /** |
||
235 | * Converts negative or large rotations into the minimum positive number |
||
236 | * of rotations required to rotate the sequence by a given $r. |
||
237 | */ |
||
238 | 52 | private function normalizeRotations(int $r) |
|
247 | |||
248 | /** |
||
249 | * @inheritDoc |
||
250 | */ |
||
251 | 52 | public function rotate(int $rotations) |
|
257 | |||
258 | /** |
||
259 | * @inheritDoc |
||
260 | */ |
||
261 | 40 | public function set(int $index, $value) |
|
269 | |||
270 | /** |
||
271 | * @inheritDoc |
||
272 | */ |
||
273 | 26 | View Code Duplication | public function shift() |
283 | |||
284 | /** |
||
285 | * @inheritDoc |
||
286 | */ |
||
287 | 397 | public function slice(int $offset, int $length = null): Sequence |
|
295 | |||
296 | /** |
||
297 | * @inheritDoc |
||
298 | */ |
||
299 | 9 | public function sort(callable $comparator = null) |
|
307 | |||
308 | /** |
||
309 | * @inheritDoc |
||
310 | */ |
||
311 | 5 | public function sorted(callable $comparator = null): Sequence |
|
317 | |||
318 | /** |
||
319 | * @inheritDoc |
||
320 | */ |
||
321 | 21 | public function sum() |
|
325 | |||
326 | /** |
||
327 | * @inheritDoc |
||
328 | */ |
||
329 | 28 | public function unshift(...$values) |
|
336 | |||
337 | /** |
||
338 | * |
||
339 | */ |
||
340 | 333 | private function validIndex(int $index) |
|
344 | |||
345 | /** |
||
346 | * |
||
347 | */ |
||
348 | 67 | public function getIterator() |
|
354 | |||
355 | /** |
||
356 | * @inheritdoc |
||
357 | */ |
||
358 | 8 | public function clear() |
|
363 | |||
364 | /** |
||
365 | * @inheritdoc |
||
366 | */ |
||
367 | 30 | public function offsetSet($offset, $value) |
|
375 | |||
376 | /** |
||
377 | * @inheritdoc |
||
378 | */ |
||
379 | 118 | public function &offsetGet($offset) |
|
387 | |||
388 | /** |
||
389 | * @inheritdoc |
||
390 | */ |
||
391 | 22 | public function offsetUnset($offset) |
|
397 | |||
398 | /** |
||
399 | * @inheritdoc |
||
400 | */ |
||
401 | 156 | public function offsetExists($offset) |
|
407 | } |
||
408 |
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.