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 map |
||
47 | * |
||
48 | * By default this method will use strict comparison checking, passing false |
||
49 | * in will use a double equals (==) instead. |
||
50 | * |
||
51 | * @param mixed $key |
||
52 | * @param bool $strict |
||
53 | * @return bool |
||
54 | */ |
||
55 | 26 | public function containsKey($key, bool $strict = true): bool |
|
59 | |||
60 | /** |
||
61 | * Returns true if the value exists in the map |
||
62 | * |
||
63 | * By default this method will use strict comparison checking, passing false |
||
64 | * in will use a double equals (==) instead. |
||
65 | * |
||
66 | * @param mixed $value |
||
67 | * @param bool $strict |
||
68 | * @return bool |
||
69 | */ |
||
70 | 3 | public function containsValue($value, bool $strict = true): bool |
|
74 | |||
75 | /** |
||
76 | * Return a set representation of map |
||
77 | * |
||
78 | * If a set is passed in, that set will be populated, otherwise |
||
79 | * a default set will be used. |
||
80 | * |
||
81 | * @param SetInterface $set |
||
82 | * @return SetInterface |
||
83 | */ |
||
84 | 5 | public function entrySet(SetInterface $set = null): SetInterface |
|
93 | |||
94 | /** |
||
95 | * Get the value at the specified key |
||
96 | * |
||
97 | * By default this method will use strict comparison checking, passing false |
||
98 | * in will use a double equals (==) instead. |
||
99 | * |
||
100 | * @param mixed $key |
||
101 | * @param bool $strict |
||
102 | * @return mixed |
||
103 | * @throws \OutOfRangeException if the key doesn't exist |
||
104 | */ |
||
105 | 7 | public function get($key, bool $strict = true) |
|
115 | |||
116 | /** |
||
117 | * Returns true if the map is empty |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | 2 | public function isEmpty(): bool |
|
125 | |||
126 | /** |
||
127 | * Returns a set of they keys in the map |
||
128 | * |
||
129 | * If a set is passed in, that set will be populated, otherwise |
||
130 | * a default set will be used. |
||
131 | * |
||
132 | * @param SetInterface $set |
||
133 | * @return SetInterface |
||
134 | */ |
||
135 | 2 | public function keySet(SetInterface $set = null): SetInterface |
|
145 | |||
146 | /** |
||
147 | * Returns the previous value or null if there was no value |
||
148 | * |
||
149 | * By default this method will use strict comparison checking, passing false |
||
150 | * in will use a double equals (==) instead. |
||
151 | * |
||
152 | * @param mixed $key |
||
153 | * @param mixed $value |
||
154 | * @param bool $strict |
||
155 | * @return mixed |
||
156 | */ |
||
157 | 26 | public function put($key, $value, bool $strict = true) |
|
170 | |||
171 | /** |
||
172 | * Remove the mapping for the key and returns the previous value |
||
173 | * or null |
||
174 | * |
||
175 | * By default this method will use strict comparison checking, passing false |
||
176 | * in will use a double equals (==) instead. |
||
177 | * |
||
178 | * @param mixed $key |
||
179 | * @param bool $strict |
||
180 | * @return mixed |
||
181 | */ |
||
182 | 5 | public function remove($key, bool $strict = true) |
|
196 | |||
197 | /** |
||
198 | * Returns the number of mappings in the map |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | 7 | public function count(): int |
|
206 | |||
207 | /** |
||
208 | * Returns the keys as a collection |
||
209 | * |
||
210 | * @param CollectionInterface $collection |
||
211 | * @return CollectionInterface |
||
212 | */ |
||
213 | 9 | View Code Duplication | public function keys(CollectionInterface $collection = null): CollectionInterface |
223 | |||
224 | /** |
||
225 | * Returns the values as a collection |
||
226 | * |
||
227 | * @param CollectionInterface $collection |
||
228 | * @return CollectionInterface |
||
229 | */ |
||
230 | 2 | View Code Duplication | public function values(CollectionInterface $collection = null): CollectionInterface |
240 | |||
241 | /** |
||
242 | * Filter the collection using closure |
||
243 | * |
||
244 | * The closure will get passed a [@see MapEntry]. Returning true from the |
||
245 | * closure will include that entry in the new map. |
||
246 | * |
||
247 | * @param callable $filter |
||
248 | * @return MapInterface |
||
249 | */ |
||
250 | 1 | public function filter(callable $filter): MapInterface |
|
263 | |||
264 | |||
265 | } |
||
266 |
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.