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 | abstract class Collection implements \Iterator, \ArrayAccess, \Countable |
||
16 | { |
||
17 | |||
18 | abstract protected function buildEntity(): HasId; |
||
19 | |||
20 | private $pool = []; |
||
21 | private $indexed = []; |
||
22 | |||
23 | private $map = []; |
||
24 | |||
25 | private $position = 0; |
||
26 | |||
27 | private $total = 0; |
||
28 | private $offset = 0; |
||
29 | private $limit = 10; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Add new domain entity, that is constructed using array as values. Each array key |
||
34 | * will be attempted top match with entity's setter method and provided with |
||
35 | * the respective array value. It returns the newly created entity. |
||
36 | * |
||
37 | * @param array $parameters |
||
38 | * |
||
39 | * @return HasId |
||
40 | */ |
||
41 | 1 | public function addBlueprint(array $parameters) |
|
50 | |||
51 | |||
52 | /** code that does the actual population of data from the given array in blueprint */ |
||
53 | 1 | View Code Duplication | private function populateEntity($instance, array $parameters) |
62 | |||
63 | |||
64 | /** |
||
65 | * Method for adding already existing domain entity to the collection. |
||
66 | * |
||
67 | * @param HasId $entity |
||
68 | */ |
||
69 | 13 | public function addEntity(HasId $entity, $key = null) |
|
84 | |||
85 | |||
86 | 1 | public function removeEntityById($key) |
|
87 | { |
||
88 | 1 | unset($this->pool[$this->map[$key]]); |
|
89 | 1 | $this->removeIndexEntry($key); |
|
90 | 1 | $this->pool = array_values($this->pool); |
|
91 | 1 | } |
|
92 | |||
93 | |||
94 | 2 | private function replaceEntity(HasId $entity, $key) |
|
106 | |||
107 | |||
108 | 5 | private function removeIndexEntry($key) |
|
113 | |||
114 | |||
115 | 12 | private function retrieveLastPoolKey() |
|
120 | |||
121 | |||
122 | /** |
||
123 | * Method for getting an ordered list of IDs for items in the collection. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 5 | public function getIds() |
|
133 | |||
134 | |||
135 | /** |
||
136 | * Replaces all of the domain entities with a content of some other collection |
||
137 | * |
||
138 | * @param Collection $replacement |
||
139 | */ |
||
140 | 1 | public function replaceWith(Collection $replacement) |
|
150 | |||
151 | |||
152 | /** |
||
153 | * Removes an entity from collection. |
||
154 | * |
||
155 | * @param HasId $entity |
||
156 | */ |
||
157 | 3 | public function removeEntity(HasId $entity) |
|
166 | |||
167 | |||
168 | /** |
||
169 | * Removes all of the content of the collection and resets it to pristine state. |
||
170 | */ |
||
171 | 1 | public function purge() |
|
178 | |||
179 | |||
180 | // implementing Countable |
||
181 | 2 | public function count() |
|
185 | |||
186 | |||
187 | // implementing Iterator |
||
188 | 2 | public function rewind() |
|
192 | |||
193 | |||
194 | 2 | public function current() |
|
198 | |||
199 | |||
200 | /** |
||
201 | * @codeCoverageIgnore |
||
202 | */ |
||
203 | public function key() |
||
207 | |||
208 | |||
209 | 2 | public function next() |
|
213 | |||
214 | |||
215 | 2 | public function valid() |
|
219 | |||
220 | |||
221 | // implementing ArrayAccess |
||
222 | 10 | public function offsetSet($offset, $value) |
|
226 | |||
227 | |||
228 | 1 | public function offsetExists($offset) |
|
232 | |||
233 | |||
234 | 3 | public function offsetUnset($offset) |
|
238 | |||
239 | |||
240 | 5 | public function offsetGet($offset) |
|
248 | |||
249 | |||
250 | // pagination |
||
251 | |||
252 | 1 | public function setOffset($offset) |
|
262 | |||
263 | |||
264 | /** |
||
265 | * @codeCoverageIgnore |
||
266 | */ |
||
267 | public function getOffset() |
||
271 | |||
272 | |||
273 | 1 | public function setLimit($limit) |
|
283 | |||
284 | |||
285 | /** |
||
286 | * @codeCoverageIgnore |
||
287 | */ |
||
288 | public function getLimit() |
||
292 | |||
293 | |||
294 | 1 | public function setTotal($total) |
|
304 | |||
305 | |||
306 | /** |
||
307 | * @codeCoverageIgnore |
||
308 | */ |
||
309 | public function getTotal() |
||
313 | } |
||
314 |
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.