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 |
||
15 | class ArrayList extends AbstractConstCollectionArray implements VectorInterface, \ArrayAccess |
||
16 | { |
||
17 | use StrictIterableTrait, |
||
18 | GuardTrait; |
||
19 | |||
20 | public function at($key) |
||
27 | |||
28 | public function set($key, $value) |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function get($index) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function tryGet($index, $default = null) |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | 3 | */ |
|
61 | public function add($item) |
||
67 | 2 | ||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | 2 | */ |
|
71 | 2 | View Code Duplication | public function addAll($items) |
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | View Code Duplication | public function setAll($items) |
|
101 | |||
102 | |||
103 | 3 | /** |
|
104 | * {@inheritdoc} |
||
105 | 3 | */ |
|
106 | public function containsKey($key) |
||
112 | |||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function removeKey($key) |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function indexOf($item) |
||
134 | 2 | ||
135 | /** |
||
136 | 1 | * {@inheritdoc} |
|
137 | */ |
||
138 | public function insert($index, $item) |
||
155 | 17 | ||
156 | /** |
||
157 | 18 | * {@inheritdoc} |
|
158 | */ |
||
159 | public function offsetExists($offset) |
||
165 | 1 | ||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function offsetGet($offset) |
||
173 | 1 | ||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function offsetSet($offset, $value) |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function offsetUnset($offset) |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function toMap() |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function reverse() |
||
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | 18 | */ |
|
214 | public function splice($offset, $length = null) |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public static function fromArray(array $arr) |
||
235 | |||
236 | /** |
||
237 | * Gets the collection's iterator |
||
238 | * @return VectorIterator |
||
239 | */ |
||
240 | public function getIterator() |
||
244 | |||
245 | /** |
||
246 | * {@inheritDoc} |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function concat($iterable) |
||
257 | } |
||
258 |
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.