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 | use Type; |
||
19 | |||
20 | private $values; |
||
21 | private $size; |
||
22 | |||
23 | 300 | public function __construct(...$values) |
|
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 66 | public function size(): int |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 2 | public function count(): int |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 68 | public function current() |
|
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | 72 | public function key() |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 68 | public function next() |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 68 | public function rewind() |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 68 | public function valid(): bool |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 2 | public function offsetExists($offset): bool |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 2 | public function offsetGet($offset) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 2 | public function offsetSet($offset, $value) |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 2 | public function offsetUnset($offset) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 136 | public function toPrimitive() |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 50 | public function get(int $index) |
|
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 54 | public function has(int $index): bool |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 6 | public function diff(SequenceInterface $seq): SequenceInterface |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 4 | public function distinct(): SequenceInterface |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 18 | public function drop(int $size): SequenceInterface |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 4 | public function dropEnd(int $size): SequenceInterface |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 16 | public function equals(SequenceInterface $seq): bool |
|
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | 6 | public function filter(callable $predicate): SequenceInterface |
|
194 | { |
||
195 | 6 | return new self(...array_filter( |
|
196 | 6 | $this->values, |
|
197 | 6 | $predicate |
|
198 | )); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 6 | public function foreach(callable $function): SequenceInterface |
|
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | 4 | View Code Duplication | public function groupBy(callable $discriminator): MapInterface |
217 | { |
||
218 | 4 | if ($this->size() === 0) { |
|
219 | 2 | throw new GroupEmptySequenceException; |
|
220 | } |
||
221 | |||
222 | 2 | $map = null; |
|
223 | |||
224 | 2 | foreach ($this->values as $value) { |
|
225 | 2 | $key = $discriminator($value); |
|
226 | |||
227 | 2 | if ($map === null) { |
|
228 | 2 | $map = new Map( |
|
229 | 2 | $this->determineType($key), |
|
230 | 2 | SequenceInterface::class |
|
231 | ); |
||
232 | } |
||
233 | |||
234 | 2 | if ($map->contains($key)) { |
|
235 | 2 | $map = $map->put( |
|
236 | $key, |
||
237 | 2 | $map->get($key)->add($value) |
|
238 | ); |
||
239 | } else { |
||
240 | 2 | $map = $map->put($key, new self($value)); |
|
241 | } |
||
242 | } |
||
243 | |||
244 | 2 | return $map; |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 8 | public function first() |
|
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | 8 | public function last() |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | 100 | public function contains($element): bool |
|
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | 42 | public function indexOf($element): int |
|
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | 4 | public function indices(): StreamInterface |
|
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | 2 | public function map(callable $function): SequenceInterface |
|
314 | |||
315 | /** |
||
316 | * {@inheritdoc} |
||
317 | */ |
||
318 | 4 | public function pad(int $size, $element): SequenceInterface |
|
322 | |||
323 | /** |
||
324 | * {@inheritdoc} |
||
325 | */ |
||
326 | 2 | public function partition(callable $predicate): MapInterface |
|
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | 34 | public function slice(int $from, int $until): SequenceInterface |
|
348 | { |
||
349 | 34 | return new self(...array_slice( |
|
350 | 34 | $this->values, |
|
351 | $from, |
||
352 | 34 | $until - $from |
|
353 | )); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * {@inheritdoc} |
||
358 | */ |
||
359 | 4 | public function splitAt(int $index): StreamInterface |
|
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | 18 | public function take(int $size): SequenceInterface |
|
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | 4 | public function takeEnd(int $size): SequenceInterface |
|
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | */ |
||
385 | 22 | public function append(SequenceInterface $seq): SequenceInterface |
|
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | 6 | public function intersect(SequenceInterface $seq): SequenceInterface |
|
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | 10 | public function join(string $separator): Str |
|
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | 176 | public function add($element): SequenceInterface |
|
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | 6 | public function sort(callable $function): SequenceInterface |
|
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | 44 | public function reduce($carry, callable $reducer) |
|
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | 4 | public function reverse(): SequenceInterface |
|
443 | } |
||
444 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.