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 |
||
| 17 | class JsonMapper |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Maps data into object class |
||
| 21 | * |
||
| 22 | * @param object $json to be casted |
||
| 23 | * @param object $object Class to receive data |
||
| 24 | * @throws InvalidArgumentException |
||
| 25 | * @return object |
||
| 26 | */ |
||
| 27 | public function map($json, $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 | 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 | protected function getSafeName($name) |
||
| 83 | } |
||
| 84 | ?> |
||
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.