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 | 24 | public function __construct($elements = NULL) { |
|
30 | |||
31 | /** |
||
32 | * {@inheritDoc} |
||
33 | */ |
||
34 | 23 | public function add($element) { |
|
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | */ |
||
41 | 22 | 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) { |
|
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | 2 | public function set($index, $element) { |
|
97 | |||
98 | /** |
||
99 | * {@inheritDoc} |
||
100 | */ |
||
101 | 9 | public function contains($element) { |
|
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | 3 | public function containsAt($index) { |
|
111 | |||
112 | /** |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | 5 | View Code Duplication | public function containsAll($elements) { |
130 | |||
131 | /** |
||
132 | * {@inheritDoc} |
||
133 | */ |
||
134 | 2 | View Code Duplication | public function containsAny($elements) { |
145 | |||
146 | /** |
||
147 | * {@inheritDoc} |
||
148 | */ |
||
149 | 5 | public function indexOf($element) { |
|
152 | |||
153 | /** |
||
154 | * {@inheritDoc} |
||
155 | */ |
||
156 | 1 | public function equals(ListInterface $collection) { |
|
173 | |||
174 | /** |
||
175 | * {@inheritDoc} |
||
176 | */ |
||
177 | 3 | public function removeAt($index) { |
|
182 | |||
183 | /** |
||
184 | * {@inheritDoc} |
||
185 | */ |
||
186 | 2 | public function removeAll($elements) { |
|
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | 1 | public function subList($offset, $length) { |
|
203 | |||
204 | /** |
||
205 | * {@inheritDoc} |
||
206 | */ |
||
207 | 2 | public function retainAll($elements) { |
|
218 | |||
219 | } |
||
220 |
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.