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 ArrayDefinitionMapper |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Maps data into object class |
||
| 22 | * |
||
| 23 | * @param object $array to be casted |
||
| 24 | * @param object $object Class to receive data |
||
| 25 | * @throws ProxyExceptionListener |
||
| 26 | * @return object |
||
|
|
|||
| 27 | */ |
||
| 28 | 2 | public function map($array, $object) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Removes - and _ and makes the next letter uppercase |
||
| 56 | * |
||
| 57 | * @param string $name Property name |
||
| 58 | * |
||
| 59 | * @return string CamelCasedVariableName |
||
| 60 | */ |
||
| 61 | 2 | protected function getCamelCaseName($name) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Since hyphens cannot be used in variables we have to uppercase them. |
||
| 70 | * |
||
| 71 | * @param string $name Property name |
||
| 72 | * |
||
| 73 | * @return string Name without hyphen |
||
| 74 | */ |
||
| 75 | 2 | protected function getSafeName($name) |
|
| 83 | } |
||
| 84 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.