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 | /** |
||
23 | * ArrayList constructor. |
||
24 | * |
||
25 | * @param array|NULL $elements |
||
26 | */ |
||
27 | 38 | public function __construct($elements = NULL) { |
|
28 | 38 | if(is_array($elements)) { |
|
29 | 9 | $this->addAll($elements); |
|
30 | 9 | } |
|
31 | 38 | } |
|
32 | |||
33 | /** |
||
34 | * {@inheritDoc} |
||
35 | */ |
||
36 | 29 | public function add($element) { |
|
37 | 29 | $this->data[] = $element; |
|
38 | 29 | } |
|
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | 28 | public function addAll($elements) { |
|
44 | 28 | foreach($elements as $element) { |
|
45 | 28 | $this->add($element); |
|
46 | 28 | } |
|
47 | 28 | } |
|
48 | |||
49 | /** |
||
50 | * {@inheritDoc} |
||
51 | */ |
||
52 | 1 | public function addTo($index, $element) { |
|
53 | 1 | $index = $this->checkIndex($index); |
|
54 | |||
55 | 1 | array_splice($this->data, $index, 0, $element); |
|
56 | 1 | } |
|
57 | |||
58 | /** |
||
59 | * {@inheritDoc} |
||
60 | */ |
||
61 | 15 | public function get($index) { |
|
62 | 15 | $index = $this->checkIndex($index); |
|
63 | |||
64 | 15 | return (isset($this->data[$index])) ? $this->data[$index] : NULL; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritDoc} |
||
69 | */ |
||
70 | 2 | public function filter(callable $filter) { |
|
71 | 2 | $result = ArrayFilter::execute($this->data, $filter); |
|
72 | |||
73 | 2 | return new static($result); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | 2 | public function set($index, $element) { |
|
80 | 2 | $index = $this->checkIndex($index); |
|
81 | 2 | $returns = NULL; |
|
82 | |||
83 | 2 | if(isset($this->data[$index])) { |
|
84 | 2 | $returns = $this->data[$index]; |
|
85 | 2 | } |
|
86 | |||
87 | 2 | $this->data[$index] = $element; |
|
88 | |||
89 | 2 | return $returns; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | */ |
||
95 | 9 | public function contains($element) { |
|
96 | 9 | return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | 3 | public function containsAt($index) { |
|
103 | 3 | $index = $this->checkIndex($index); |
|
104 | |||
105 | 3 | return isset($this->data[$index]); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | 5 | View Code Duplication | public function containsAll($elements) { |
|
|||
112 | 5 | $elements = $this->collectionToArray($elements); |
|
113 | |||
114 | 5 | $result = TRUE; |
|
115 | |||
116 | 5 | foreach($elements as $item) { |
|
117 | 5 | if($this->contains($item) === FALSE) { |
|
118 | 2 | $result = FALSE; |
|
119 | |||
120 | 2 | break; |
|
121 | } |
||
122 | 5 | } |
|
123 | |||
124 | 5 | return $result; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | 2 | View Code Duplication | public function containsAny($elements) { |
131 | 2 | $elements = $this->collectionToArray($elements); |
|
132 | |||
133 | 2 | foreach($elements as $item) { |
|
134 | 2 | if($this->contains($item) === TRUE) { |
|
135 | 2 | return TRUE; |
|
136 | } |
||
137 | 2 | } |
|
138 | |||
139 | 2 | return FALSE; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritDoc} |
||
144 | */ |
||
145 | 5 | public function indexOf($element) { |
|
146 | 5 | return array_search($element, $this->data, TRUE); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritDoc} |
||
151 | */ |
||
152 | 1 | public function equals(ListInterface $collection) { |
|
153 | 1 | if($collection->size() !== $this->size()) { |
|
154 | 1 | return FALSE; |
|
155 | } |
||
156 | |||
157 | 1 | $elements = $collection->toArray(); |
|
158 | |||
159 | 1 | foreach($elements as $key => $value) { |
|
160 | 1 | if(isset($this->data[$key]) && $this->data[$key] === $value) { |
|
161 | 1 | continue; |
|
162 | } |
||
163 | |||
164 | 1 | return FALSE; |
|
165 | 1 | } |
|
166 | |||
167 | 1 | return TRUE; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * {@inheritDoc} |
||
172 | */ |
||
173 | 3 | public function removeAt($index) { |
|
174 | 3 | $index = $this->checkIndex($index); |
|
175 | |||
176 | 3 | if(isset($this->data[$index])) { |
|
177 | 3 | unset($this->data[$index]); |
|
178 | 3 | } |
|
179 | 3 | } |
|
180 | |||
181 | /** |
||
182 | * {@inheritDoc} |
||
183 | */ |
||
184 | 2 | public function removeAll($elements) { |
|
192 | |||
193 | /** |
||
194 | * {@inheritDoc} |
||
195 | */ |
||
196 | 1 | public function subList($offset, $length) { |
|
201 | |||
202 | /** |
||
203 | * {@inheritDoc} |
||
204 | */ |
||
205 | 2 | public function retainAll($elements) { |
|
206 | 2 | $elements = $this->collectionToArray($elements); |
|
207 | |||
208 | 2 | View Code Duplication | foreach($this->data as $index => $item) { |
209 | 2 | if(array_search($item, $elements, TRUE) !== FALSE) { |
|
210 | 2 | continue; |
|
211 | } |
||
212 | |||
213 | 2 | unset($this->data[$index]); |
|
214 | 2 | } |
|
215 | 2 | } |
|
216 | |||
217 | /** |
||
218 | * Validates the given index. Check if this a numeric value |
||
219 | * and throws an exception if not. If the index is numeric |
||
220 | * the value is casted to array and returned. |
||
221 | * |
||
222 | * @param mixed $index |
||
223 | * |
||
224 | * @return int |
||
225 | * |
||
226 | * @throws \BuildR\Collection\Exception\ListException |
||
227 | */ |
||
228 | 25 | protected function checkIndex($index) { |
|
235 | |||
236 | } |
||
237 |
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.