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) |
||
32 | |||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | public function toArray(): array |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | public function merge($values): \Ds\Sequence |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function count(): int |
||
59 | |||
60 | /** |
||
61 | * @inheritDoc |
||
62 | */ |
||
63 | View Code Duplication | public function contains(...$values): bool |
|
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | public function filter(callable $callback = null): \Ds\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) |
||
119 | |||
120 | /** |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | public function insert(int $index, ...$values) |
||
131 | |||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | public function join(string $glue = null): string |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function last() |
||
151 | |||
152 | /** |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | public function map(callable $callback): \Ds\Sequence |
||
159 | |||
160 | /** |
||
161 | * @inheritDoc |
||
162 | */ |
||
163 | View Code Duplication | public function pop() |
|
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | public function push(...$values) |
||
185 | |||
186 | /** |
||
187 | * @inheritDoc |
||
188 | */ |
||
189 | public function reduce(callable $callback, $initial = null) |
||
193 | |||
194 | /** |
||
195 | * @inheritDoc |
||
196 | */ |
||
197 | public function remove(int $index) |
||
206 | |||
207 | /** |
||
208 | * @inheritDoc |
||
209 | */ |
||
210 | public function reverse(): \Ds\Sequence |
||
215 | |||
216 | private function reverseRange(int $a, int $b) |
||
228 | |||
229 | private function normalizeRotations(int $rotations, int $count) |
||
237 | |||
238 | /** |
||
239 | * @inheritDoc |
||
240 | */ |
||
241 | public function rotate(int $rotations) |
||
256 | |||
257 | /** |
||
258 | * @inheritDoc |
||
259 | */ |
||
260 | public function set(int $index, $value) |
||
265 | |||
266 | /** |
||
267 | * @inheritDoc |
||
268 | */ |
||
269 | View Code Duplication | public function shift() |
|
280 | |||
281 | /** |
||
282 | * @inheritDoc |
||
283 | */ |
||
284 | public function slice(int $offset, int $length = null): \Ds\Sequence |
||
292 | |||
293 | /** |
||
294 | * @inheritDoc |
||
295 | */ |
||
296 | public function sort(callable $comparator = null): \Ds\Sequence |
||
308 | |||
309 | /** |
||
310 | * @inheritDoc |
||
311 | */ |
||
312 | public function unshift(...$values) |
||
319 | |||
320 | /** |
||
321 | * Check Range |
||
322 | * |
||
323 | * @param int $index |
||
324 | */ |
||
325 | private function checkRange(int $index) |
||
331 | |||
332 | /** |
||
333 | * Get Iterator |
||
334 | */ |
||
335 | public function getIterator() |
||
341 | |||
342 | /** |
||
343 | * |
||
344 | */ |
||
345 | public function clear() |
||
350 | |||
351 | /** |
||
352 | * @inheritdoc |
||
353 | */ |
||
354 | public function offsetSet($offset, $value) |
||
362 | |||
363 | /** |
||
364 | * @inheritdoc |
||
365 | */ |
||
366 | public function &offsetGet($offset) |
||
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | public function offsetUnset($offset) |
||
382 | |||
383 | /** |
||
384 | * @inheritdoc |
||
385 | */ |
||
386 | public function offsetExists($offset) |
||
394 | } |
||
395 |
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.