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 |
||
| 3 | class Navigation { |
||
| 4 | private $navigation; |
||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 9 | public function __construct() { |
||
| 28 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 33 | public function getNavigation() { |
||
| 36 | |||
| 37 | private function getLienNavigationPage($id_page) { |
||
| 54 | |||
| 55 | private function getLienNavigationModule($id_module) { |
||
| 73 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 78 | private function setNavigation($navigation) { |
||
| 81 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 82 | } |
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.