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 | trait SetLikeTrait |
||
9 | { |
||
10 | use ConstSetLikeTrait, |
||
11 | CommonMutableContainerTrait; |
||
12 | |||
13 | public function offsetSet($offset, $value) |
||
21 | |||
22 | public function offsetUnset($offset) |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function add($item) |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function removeKey($key) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | View Code Duplication | public function remove($element) |
|
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | public function removeAll($traversable) |
||
82 | |||
83 | /** |
||
84 | * {@inheritDoc} |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function each(callable $callable) |
||
95 | } |
||
96 |
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.