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 |
||
| 7 | class Contenus { |
||
| 8 | //pour la table page |
||
| 9 | protected $id_page; |
||
| 10 | protected $titre; |
||
| 11 | protected $contenu; |
||
| 12 | protected $url; |
||
| 13 | protected $meta_description; |
||
| 14 | protected $balise_title; |
||
| 15 | protected $parent; |
||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | //-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
| 20 | View Code Duplication | public function __construct($init_all = 0) { |
|
|
|
|||
| 21 | $dbc = \core\App::getDb(); |
||
| 22 | |||
| 23 | if ($init_all == 1) { |
||
| 24 | //on construit le menu |
||
| 25 | $query = $dbc->query("SELECT ID_page, titre, balise_title, parent, url FROM page WHERE affiche=1 ORDER BY ordre"); |
||
| 26 | |||
| 27 | if ((is_array($query)) && (count($query) > 0)) { |
||
| 28 | $id_page = []; |
||
| 29 | $titre = []; |
||
| 30 | $balise_title = []; |
||
| 31 | $url = []; |
||
| 32 | $parent = []; |
||
| 33 | |||
| 34 | foreach ($query as $obj) { |
||
| 35 | $id_page[] = $obj->ID_page; |
||
| 36 | $titre[] = $obj->titre; |
||
| 37 | $balise_title[] = $obj->balise_title; |
||
| 38 | $url[] = $obj->url; |
||
| 39 | $parent[] = $obj->parent; |
||
| 40 | } |
||
| 41 | |||
| 42 | $this->setMenu($id_page, $titre, $balise_title, $url, $parent); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | //-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 51 | //pour la table page |
||
| 52 | public function getIdPage() { |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * pour récupérer l'en tete d'une page (balise title ++ meta description) |
||
| 77 | * @param $id_page |
||
| 78 | */ |
||
| 79 | public function getHeadPage($id_page, $url = null) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * pour récupérer une page en particulier |
||
| 102 | * @param $id_page |
||
| 103 | */ |
||
| 104 | public function getContenuPage($id_page = null) { |
||
| 125 | //-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 130 | protected function setMenu($id_page, $titre, $balise_title, $url, $parent) { |
||
| 137 | //-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
||
| 138 | } |
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.