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; |
||
18 | class ArrayList extends AbstractCollection implements ListInterface { |
||
19 | |||
20 | /** |
||
21 | * ArrayList constructor. |
||
22 | * |
||
23 | * @param array|NULL $elements |
||
24 | */ |
||
25 | 22 | public function __construct($elements = NULL) { |
|
30 | |||
31 | /** |
||
32 | * {@inheritDoc} |
||
33 | */ |
||
34 | 21 | public function add($element) { |
|
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | */ |
||
41 | 20 | public function addAll($elements) { |
|
46 | |||
47 | /** |
||
48 | * {@inheritDoc} |
||
49 | */ |
||
50 | 1 | public function addTo($index, $element) { |
|
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | 13 | public function get($index) { |
|
60 | |||
61 | /** |
||
62 | * {@inheritDoc} |
||
63 | */ |
||
64 | 2 | public function filter(callable $filter) { |
|
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | 2 | public function set($index, $element) { |
|
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | 9 | public function contains($element) { |
|
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | 3 | public function containsAt($index) { |
|
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | */ |
||
105 | 5 | View Code Duplication | public function containsAll($elements) { |
120 | |||
121 | /** |
||
122 | * {@inheritDoc} |
||
123 | */ |
||
124 | 2 | View Code Duplication | public function containsAny($elements) { |
135 | |||
136 | /** |
||
137 | * {@inheritDoc} |
||
138 | */ |
||
139 | 5 | public function indexOf($element) { |
|
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | 1 | public function equals(ListInterface $collection) { |
|
163 | |||
164 | /** |
||
165 | * {@inheritDoc} |
||
166 | */ |
||
167 | 3 | public function removeAt($index) { |
|
172 | |||
173 | /** |
||
174 | * {@inheritDoc} |
||
175 | */ |
||
176 | 2 | public function removeAll($elements) { |
|
184 | |||
185 | /** |
||
186 | * {@inheritDoc} |
||
187 | */ |
||
188 | 1 | public function subList($offset, $length) { |
|
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | 2 | public function retainAll($elements) { |
|
208 | |||
209 | /** |
||
210 | * The HHVM is not compatible with the $flag attribute that introduced in PHP 5.6 |
||
211 | * This is a fallback method that provides same functionality on HHVM that |
||
212 | * ARRAY_FILTER_BOTH argument on PHP 5.6 |
||
213 | * |
||
214 | * @param callable $filter |
||
215 | * |
||
216 | * @return static |
||
217 | */ |
||
218 | protected function executeHhvmArrayFilter(callable $filter) { |
||
229 | |||
230 | } |
||
231 |
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.