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 |
||
7 | trait ConstMapLikeTrait |
||
8 | { |
||
9 | use CommonContainerMethodsTrait; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $container; |
||
15 | |||
16 | View Code Duplication | protected function init($it = null) |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function isEmpty() |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function count() |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function at($key) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function get($index) |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function containsKey($key) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function contains($element) |
||
87 | |||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function offsetExists($offset) |
||
96 | |||
97 | /** |
||
98 | * Returns an array containing the values from this VectorLike. |
||
99 | */ |
||
100 | public function toArray() |
||
101 | { |
||
102 | $arr = []; |
||
103 | foreach ($this as $key => $value) { |
||
104 | if ($value instanceof Enumerable) { |
||
105 | $arr[$key] = $value->toArray(); |
||
106 | } else { |
||
107 | $arr[$key] = $value; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | return $arr; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function each(callable $callable) |
||
126 | } |
||
127 |
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.