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 |
||
16 | class Sequence implements SequenceInterface |
||
17 | { |
||
18 | private $values; |
||
19 | private $size; |
||
20 | |||
21 | 92 | public function __construct(...$values) |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 29 | public function size(): int |
|
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | 1 | public function count(): int |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 10 | public function current() |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 12 | public function key() |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 12 | public function next() |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 10 | public function rewind() |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 10 | public function valid(): bool |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 1 | public function offsetExists($offset): bool |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 1 | public function offsetGet($offset) |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 1 | public function offsetSet($offset, $value) |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 1 | public function offsetUnset($offset) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 44 | public function toPrimitive() |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 14 | public function get(int $index) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 2 | public function diff(SequenceInterface $seq): SequenceInterface |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 1 | public function distinct(): SequenceInterface |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 8 | public function drop(int $size): SequenceInterface |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 2 | public function dropEnd(int $size): SequenceInterface |
|
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | 6 | public function equals(SequenceInterface $seq): bool |
|
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 2 | public function filter(\Closure $predicate): SequenceInterface |
|
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | 3 | public function foreach(\Closure $function): SequenceInterface |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 3 | View Code Duplication | public function groupBy(\Closure $discriminator): MapInterface |
214 | { |
||
215 | 3 | if ($this->size() === 0) { |
|
216 | 1 | throw new GroupEmptySequenceException; |
|
217 | } |
||
218 | |||
219 | 2 | $map = null; |
|
220 | |||
221 | 2 | foreach ($this->values as $value) { |
|
222 | 2 | $key = $discriminator($value); |
|
223 | |||
224 | 2 | if ($map === null) { |
|
225 | 2 | $type = gettype($key); |
|
226 | 2 | $map = new Map( |
|
227 | 2 | $type === 'object' ? get_class($key) : $type, |
|
228 | 2 | SequenceInterface::class |
|
229 | ); |
||
230 | } |
||
231 | |||
232 | 2 | if ($map->contains($key)) { |
|
233 | 2 | $map = $map->put( |
|
234 | $key, |
||
235 | 2 | $map->get($key)->add($value) |
|
236 | ); |
||
237 | } else { |
||
238 | 2 | $map = $map->put($key, new self($value)); |
|
239 | } |
||
240 | } |
||
241 | |||
242 | 2 | return $map; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 2 | public function first() |
|
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | 2 | public function last() |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 45 | public function contains($element): bool |
|
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | 16 | public function indexOf($element): int |
|
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | 1 | public function indices(): SequenceInterface |
|
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | 2 | public function map(\Closure $function): SequenceInterface |
|
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | 1 | public function pad(int $size, $element): SequenceInterface |
|
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | 2 | public function partition(\Closure $predicate): SequenceInterface |
|
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | 14 | public function slice(int $from, int $until): SequenceInterface |
|
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | 1 | public function splitAt(int $index): SequenceInterface |
|
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | 8 | public function take(int $size): SequenceInterface |
|
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | 2 | public function takeEnd(int $size): SequenceInterface |
|
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | 9 | public function append(SequenceInterface $seq): SequenceInterface |
|
375 | |||
376 | /** |
||
377 | * {@inheritdoc} |
||
378 | */ |
||
379 | 2 | public function intersect(SequenceInterface $seq): SequenceInterface |
|
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | 3 | public function join(string $separator): StringPrimitive |
|
391 | |||
392 | /** |
||
393 | * {@inheritdoc} |
||
394 | */ |
||
395 | 43 | public function add($element): SequenceInterface |
|
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | */ |
||
406 | 2 | public function sort(\Closure $function): SequenceInterface |
|
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | 2 | public function reduce($carry, \Closure $reducer) |
|
421 | } |
||
422 |