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 namespace BuildR\Collection\ArrayList; |
||
20 | class ArrayList extends AbstractCollection implements ListInterface { |
||
21 | |||
22 | use FilterableCollectionTrait; |
||
23 | |||
24 | /** |
||
25 | * ArrayList constructor. |
||
26 | * |
||
27 | * @param array|NULL $elements |
||
28 | */ |
||
29 | 38 | public function __construct($elements = NULL) { |
|
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | */ |
||
38 | 29 | public function add($element) { |
|
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | 28 | public function addAll($elements) { |
|
50 | |||
51 | /** |
||
52 | * {@inheritDoc} |
||
53 | */ |
||
54 | 1 | public function addTo($index, $element) { |
|
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | */ |
||
63 | 15 | public function get($index) { |
|
68 | |||
69 | /** |
||
70 | * {@inheritDoc} |
||
71 | */ |
||
72 | 2 | public function set($index, $element) { |
|
84 | |||
85 | /** |
||
86 | * {@inheritDoc} |
||
87 | */ |
||
88 | 9 | public function contains($element) { |
|
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | */ |
||
95 | 3 | public function containsAt($index) { |
|
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | */ |
||
104 | 5 | View Code Duplication | public function containsAll($elements) { |
119 | |||
120 | /** |
||
121 | * {@inheritDoc} |
||
122 | */ |
||
123 | 2 | View Code Duplication | public function containsAny($elements) { |
134 | |||
135 | /** |
||
136 | * {@inheritDoc} |
||
137 | */ |
||
138 | 5 | public function indexOf($element) { |
|
141 | |||
142 | /** |
||
143 | * {@inheritDoc} |
||
144 | */ |
||
145 | 1 | public function equals(ListInterface $collection) { |
|
162 | |||
163 | /** |
||
164 | * {@inheritDoc} |
||
165 | */ |
||
166 | 3 | public function removeAt($index) { |
|
173 | |||
174 | /** |
||
175 | * {@inheritDoc} |
||
176 | */ |
||
177 | 2 | public function removeAll($elements) { |
|
185 | |||
186 | /** |
||
187 | * {@inheritDoc} |
||
188 | */ |
||
189 | 1 | public function subList($offset, $length) { |
|
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | 2 | public function retainAll($elements) { |
|
209 | |||
210 | /** |
||
211 | * Validates the given index. Check if this a numeric value |
||
212 | * and throws an exception if not. If the index is numeric |
||
213 | * the value is casted to array and returned. |
||
214 | * |
||
215 | * @param mixed $index |
||
216 | * |
||
217 | * @return int |
||
218 | * |
||
219 | * @throws \BuildR\Collection\Exception\ListException |
||
220 | */ |
||
221 | 25 | protected function checkIndex($index) { |
|
228 | |||
229 | } |
||
230 |
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.