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 |
||
18 | class HashMap extends AbstractMap |
||
19 | { |
||
20 | /** |
||
21 | * The mapping keys |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $keys = []; |
||
26 | |||
27 | /** |
||
28 | * The mapping values |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $values = []; |
||
33 | |||
34 | /** |
||
35 | * Removes all mappings from map |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | 1 | public function clear() |
|
44 | |||
45 | /** |
||
46 | * Returns true if the key exists in the lookup table |
||
47 | * |
||
48 | * @param mixed $key |
||
49 | * @return bool |
||
50 | */ |
||
51 | 23 | public function containsKey($key): bool |
|
55 | |||
56 | /** |
||
57 | * Returns true if the value exists in the map |
||
58 | * |
||
59 | * By default this method will use strict comparison checking, passing false |
||
60 | * in will use a double equals (==) instead. |
||
61 | * |
||
62 | * @param mixed $value |
||
63 | * @param bool $strict |
||
64 | * @return bool |
||
65 | */ |
||
66 | 3 | public function containsValue($value, bool $strict = true): bool |
|
70 | |||
71 | /** |
||
72 | * Return a set representation of map |
||
73 | * |
||
74 | * If a set is passed in, that set will be populated, otherwise |
||
75 | * a default set will be used. |
||
76 | * |
||
77 | * @param SetInterface $set |
||
78 | * @return SetInterface |
||
79 | */ |
||
80 | 5 | public function entrySet(SetInterface $set = null): SetInterface |
|
89 | |||
90 | /** |
||
91 | * Get the value at the specified key |
||
92 | * |
||
93 | * @param mixed $key |
||
94 | * @return mixed |
||
95 | * @throws \OutOfRangeException if the key doesn't exist |
||
96 | */ |
||
97 | 4 | public function get($key) |
|
106 | |||
107 | /** |
||
108 | * Returns true if the map is empty |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 2 | public function isEmpty(): bool |
|
116 | |||
117 | /** |
||
118 | * Returns a set of they keys in the map |
||
119 | * |
||
120 | * If a set is passed in, that set will be populated, otherwise |
||
121 | * a default set will be used. |
||
122 | * |
||
123 | * @param SetInterface $set |
||
124 | * @return SetInterface |
||
125 | */ |
||
126 | 2 | public function keySet(SetInterface $set = null): SetInterface |
|
136 | |||
137 | /** |
||
138 | * Returns the previous value or null if there was no value |
||
139 | * |
||
140 | * @param mixed $key |
||
141 | * @param mixed $value |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 23 | public function put($key, $value) |
|
158 | |||
159 | /** |
||
160 | * Remove the mapping for the key and returns the previous value |
||
161 | * or null |
||
162 | * |
||
163 | * @param mixed $key |
||
164 | * @return mixed |
||
165 | */ |
||
166 | 3 | public function remove($key) |
|
178 | |||
179 | /** |
||
180 | * Returns the number of mappings in the map |
||
181 | * |
||
182 | * @return int |
||
183 | */ |
||
184 | 6 | public function count(): int |
|
188 | |||
189 | /** |
||
190 | * Returns the keys as a collection |
||
191 | * |
||
192 | * @param CollectionInterface $collection |
||
193 | * @return CollectionInterface |
||
194 | */ |
||
195 | 9 | View Code Duplication | public function keys(CollectionInterface $collection = null): CollectionInterface |
205 | |||
206 | /** |
||
207 | * Returns the values as a collection |
||
208 | * |
||
209 | * @param CollectionInterface $collection |
||
210 | * @return CollectionInterface |
||
211 | */ |
||
212 | 2 | View Code Duplication | public function values(CollectionInterface $collection = null): CollectionInterface |
222 | |||
223 | /** |
||
224 | * Filter the collection using closure |
||
225 | * |
||
226 | * The closure will get passed a [@see MapEntry]. Returning true from the |
||
227 | * closure will include that entry in the new map. |
||
228 | * |
||
229 | * @param callable $filter |
||
230 | * @return MapInterface |
||
231 | */ |
||
232 | 1 | public function filter(callable $filter): MapInterface |
|
245 | |||
246 | /** |
||
247 | * Generate a hashcode for a php value |
||
248 | * |
||
249 | * @param mixed $value |
||
250 | * @return string |
||
251 | */ |
||
252 | 23 | protected function hashCode($value): string |
|
264 | } |
||
265 |
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.