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 |
||
| 6 | class Unite { |
||
| 7 | private $coef_unite; |
||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 12 | View Code Duplication | public function __construct() { |
|
| 21 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 22 | |||
| 23 | |||
| 24 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param $unite |
||
| 28 | * @param $niveau |
||
| 29 | * @param $type |
||
| 30 | * @return array |
||
| 31 | * récupère les caractéristiques de l'unité en fonction de son niveau |
||
| 32 | */ |
||
| 33 | private function getCaracteristiqueUnite($unite, $niveau, $type) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param $type |
||
| 65 | * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction |
||
| 66 | * du type (batiment sur lequel on a cliqué) |
||
| 67 | */ |
||
| 68 | public function getUnitePossibleRecruter($type) { |
||
| 80 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 81 | |||
| 82 | |||
| 83 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 84 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 85 | } |
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.