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\Map; |
||
22 | class HashMap extends AbstractCollection implements MapInterface { |
||
23 | |||
24 | use FilterableCollectionTrait; |
||
25 | |||
26 | /** |
||
27 | * HashMap constructor. |
||
28 | * |
||
29 | * @param array|null $content |
||
30 | */ |
||
31 | 58 | public function __construct($content = NULL) { |
|
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | 54 | public function containsKey($key) { |
|
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | 4 | public function containsValue($value) { |
|
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | 24 | public function contains($key, $value) { |
|
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | 12 | public function equals(MapInterface $map) { |
|
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | */ |
||
84 | 8 | public function each(callable $callback) { |
|
89 | |||
90 | /** |
||
91 | * {@inheritDoc} |
||
92 | */ |
||
93 | 28 | View Code Duplication | public function get($key, $defaultValue = NULL) { |
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | 4 | public function keySet() { |
|
109 | |||
110 | /** |
||
111 | * {@inheritDoc} |
||
112 | */ |
||
113 | 4 | public function valueList() { |
|
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | */ |
||
120 | 4 | public function merge(MapInterface $map) { |
|
129 | |||
130 | /** |
||
131 | * {@inheritDoc} |
||
132 | */ |
||
133 | 54 | View Code Duplication | public function put($key, $value) { |
145 | |||
146 | /** |
||
147 | * {@inheritDoc} |
||
148 | */ |
||
149 | 46 | public function putAll($map) { |
|
158 | |||
159 | /** |
||
160 | * {@inheritDoc} |
||
161 | */ |
||
162 | 4 | View Code Duplication | public function putIfAbsent($key, $value) { |
173 | |||
174 | /** |
||
175 | * {@inheritDoc} |
||
176 | */ |
||
177 | 4 | View Code Duplication | public function remove($key) { |
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | */ |
||
193 | 4 | public function removeIf($key, $value) { |
|
205 | |||
206 | /** |
||
207 | * {@inheritDoc} |
||
208 | */ |
||
209 | 4 | View Code Duplication | public function replace($key, $value) { |
221 | |||
222 | /** |
||
223 | * {@inheritDoc} |
||
224 | */ |
||
225 | 4 | public function replaceIf($key, $oldValue, $newValue) { |
|
237 | |||
238 | /** |
||
239 | * Validate the given key. This map only allows scalar types |
||
240 | * as items key. If the validation is fails, throws a MapException. |
||
241 | * |
||
242 | * @param mixed $key |
||
243 | * |
||
244 | * @return bool |
||
245 | * |
||
246 | * @throws \BuildR\Collection\Exception\MapException |
||
247 | */ |
||
248 | 58 | protected function checkKeyType($key) { |
|
255 | |||
256 | } |
||
257 |
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.