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:
1 | <?php |
||
8 | class Set implements SetInterface |
||
9 | { |
||
10 | use Type; |
||
11 | |||
12 | private $type; |
||
13 | private $spec; |
||
14 | private $values; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | 64 | public function __construct(string $type) |
|
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 34 | public function type(): Str |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 4 | public function size(): int |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 2 | public function count(): int |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 40 | public function toPrimitive() |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 2 | public function current() |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 2 | public function key() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 2 | public function next() |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 2 | public function rewind() |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function valid() |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 4 | View Code Duplication | public function intersect(SetInterface $set): SetInterface |
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 54 | public function add($element): SetInterface |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 52 | public function contains($element): bool |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 2 | public function remove($element): SetInterface |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 4 | View Code Duplication | public function diff(SetInterface $set): SetInterface |
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 12 | public function equals(SetInterface $set): bool |
|
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | 2 | public function filter(callable $predicate): SetInterface |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function foreach(callable $function): SetInterface |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | 2 | public function groupBy(callable $discriminator): MapInterface |
|
225 | { |
||
226 | 2 | $map = $this->values->groupBy($discriminator); |
|
227 | |||
228 | 2 | return $map->reduce( |
|
229 | 2 | new Map((string) $map->keyType(), SetInterface::class), |
|
230 | function(Map $carry, $key, StreamInterface $values): Map { |
||
231 | 2 | return $carry->put( |
|
232 | 2 | $key, |
|
233 | 2 | $values->reduce( |
|
234 | 2 | $this->clear(), |
|
235 | function(Set $carry, $value): Set { |
||
236 | 2 | return $carry->add($value); |
|
237 | 2 | } |
|
238 | ) |
||
239 | ); |
||
240 | 2 | } |
|
241 | ); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 4 | public function map(callable $function): SetInterface |
|
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | 2 | public function partition(callable $predicate): MapInterface |
|
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | 2 | public function join(string $separator): Str |
|
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | 2 | public function sort(callable $function): StreamInterface |
|
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | 4 | public function merge(SetInterface $set): SetInterface |
|
293 | { |
||
294 | 4 | $this->validate($set); |
|
295 | |||
296 | 2 | return $set->reduce( |
|
297 | 2 | $this, |
|
298 | 2 | function(self $carry, $value): self { |
|
299 | 2 | return $carry->add($value); |
|
300 | 2 | } |
|
301 | ); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | 20 | public function reduce($carry, callable $reducer) |
|
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | 8 | public function clear(): SetInterface |
|
322 | |||
323 | /** |
||
324 | * Make sure the set is compatible with the current one |
||
325 | * |
||
326 | * @param SetInterface $set |
||
327 | * |
||
328 | * @throws InvalidArgumentException |
||
329 | * |
||
330 | * @return void |
||
331 | */ |
||
332 | 22 | private function validate(SetInterface $set) |
|
340 | } |
||
341 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.