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 |
||
21 | class MappingFactory |
||
22 | { |
||
23 | const CLASS_KEY = 'class'; |
||
24 | const ALIAS_KEY = 'alias'; |
||
25 | const ALIASED_PROPERTIES_KEY = 'aliased_properties'; |
||
26 | const HIDE_PROPERTIES_KEY = 'hide_properties'; |
||
27 | const ID_PROPERTIES_KEY = 'id_properties'; |
||
28 | const URLS_KEY = 'urls'; |
||
29 | const CURIES_KEY = 'curies'; |
||
30 | const RELATIONSHIPS_KEY = 'relationships'; |
||
31 | const SELF_KEY = 'self'; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected static $classProperties = []; |
||
37 | |||
38 | /** |
||
39 | * @param string $className |
||
40 | * |
||
41 | * @throws MappingException |
||
42 | * |
||
43 | * @return Mapping |
||
44 | * |
||
45 | * @since 2.0.0 |
||
46 | */ |
||
47 | public static function fromClass($className) |
||
83 | |||
84 | /** |
||
85 | * @param array $mappedClass |
||
86 | * |
||
87 | * @throws MappingException |
||
88 | * |
||
89 | * @return Mapping |
||
90 | */ |
||
91 | public static function fromArray(array &$mappedClass) |
||
113 | |||
114 | /** |
||
115 | * @param array $mappedClass |
||
116 | * |
||
117 | * @throws MappingException |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | protected static function getClass(array &$mappedClass) |
||
131 | |||
132 | /** |
||
133 | * @param array $mappedClass |
||
134 | * |
||
135 | * @throws MappingException |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | View Code Duplication | protected static function getSelfUrl(array &$mappedClass) |
|
149 | |||
150 | /** |
||
151 | * @param array $mappedClass |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | protected static function getIdProperties(array &$mappedClass) |
||
159 | |||
160 | /** |
||
161 | * @param array $mappedClass |
||
162 | * @param Mapping $mapping |
||
163 | * @param string $className |
||
164 | * |
||
165 | * @throws MappingException |
||
166 | */ |
||
167 | View Code Duplication | protected static function setAliasedProperties(array &$mappedClass, Mapping $mapping, $className) |
|
184 | |||
185 | /** |
||
186 | * Recursive function to get an associative array of class properties by |
||
187 | * property name, including inherited ones from extended classes. |
||
188 | * |
||
189 | * @param string $className Class name |
||
190 | * |
||
191 | * @return array |
||
192 | * |
||
193 | * @link http://php.net/manual/es/reflectionclass.getproperties.php#88405 |
||
194 | */ |
||
195 | protected static function getClassProperties($className) |
||
216 | |||
217 | /** |
||
218 | * @param array $mappedClass |
||
219 | * @param Mapping $mapping |
||
220 | * @param string $className |
||
221 | * |
||
222 | * @throws MappingException |
||
223 | */ |
||
224 | View Code Duplication | protected static function setHideProperties(array &$mappedClass, Mapping $mapping, $className) |
|
241 | |||
242 | /** |
||
243 | * @param array $mappedClass |
||
244 | * @param Mapping $mapping |
||
245 | * @param string $className |
||
246 | * |
||
247 | * @throws MappingException |
||
248 | */ |
||
249 | protected static function setRelationships(array &$mappedClass, Mapping $mapping, $className) |
||
267 | |||
268 | /** |
||
269 | * @param array $mappedClass |
||
270 | * @param Mapping $mapping |
||
271 | */ |
||
272 | protected static function setCuries(array &$mappedClass, Mapping $mapping) |
||
278 | |||
279 | /** |
||
280 | * @param Mapping $mapping |
||
281 | * @param string $className |
||
282 | * |
||
283 | * @throws MappingException |
||
284 | */ |
||
285 | protected static function setProperties(Mapping $mapping, $className) |
||
289 | |||
290 | /** |
||
291 | * @param array $mappedClass |
||
292 | * |
||
293 | * @return mixed |
||
294 | */ |
||
295 | View Code Duplication | protected static function getOtherUrls(array $mappedClass) |
|
303 | } |
||
304 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.