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 GestionModule { |
||
7 | private $id_module; |
||
8 | private $url; |
||
9 | private $icone; |
||
10 | private $nom; |
||
11 | private $version; |
||
12 | private $url_telechargement; |
||
13 | |||
14 | |||
15 | //-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
16 | //-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
17 | |||
18 | |||
19 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
20 | public function getIdModule() { |
||
29 | public function getVersion() { |
||
30 | return $this->version; |
||
31 | } |
||
32 | public function getIcone() { |
||
33 | return $this->icone; |
||
34 | } |
||
35 | public function getUrlTelechargement() { |
||
38 | |||
39 | /** |
||
40 | * récupere la liste des modules activé utilisé pour toutes les pages |
||
41 | */ |
||
42 | public function getListeModuleActiver() { |
||
43 | $dbc = App::getDb(); |
||
44 | $query = $dbc->select()->from("module")->where("activer", "=", 1, "AND")->where("installer", "=", 1)->get(); |
||
45 | |||
46 | if ((is_array($query)) && (count($query) > 0)) { |
||
47 | $id_module = []; $url = []; $nom = []; $version = []; $icone = []; |
||
48 | |||
49 | foreach ($query as $obj) { |
||
50 | $id_module[] = $obj->ID_module; |
||
51 | $url[] = $obj->url; |
||
52 | $nom[] = $obj->nom_module; |
||
53 | $version[] = $obj->version; |
||
54 | $icone[] = $obj->icone; |
||
55 | } |
||
56 | $this->setListeModuleActiver($id_module, $url, $version, $nom, $icone); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * fonction qui renvoi la liste de tous les modules |
||
62 | */ |
||
63 | public function getListeModule() { |
||
64 | $dbc = App::getDb(); |
||
65 | |||
66 | $query = $dbc->select()->from("module")->get(); |
||
67 | |||
68 | if ((is_array($query)) && (count($query) > 0)) { |
||
69 | $values = []; |
||
70 | foreach ($query as $obj) { |
||
71 | $values[] = [ |
||
72 | "id_module" => $obj->ID_module, |
||
73 | "url" => $obj->url, |
||
74 | "nom" => $obj->nom_module, |
||
75 | "version" => $obj->version, |
||
76 | "icone" => $obj->icone, |
||
77 | "activer" => $obj->activer, |
||
78 | "installer" => $obj->installer |
||
79 | ]; |
||
80 | } |
||
81 | App::setValues(["active_modules" => $values]); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $nom_module |
||
87 | * @return boolean|null |
||
88 | * return true si le module est activer sinon false |
||
89 | */ |
||
90 | View Code Duplication | public static function getModuleActiver($nom_module) { |
|
|
|||
91 | $dbc = App::getDb(); |
||
92 | |||
93 | $query = $dbc->select("activer")->from("module")->where("nom_module", "=", $dbc->quote($nom_module), "", true)->get(); |
||
94 | |||
95 | if (count($query) > 0) { |
||
96 | foreach ($query as $obj) { |
||
97 | if ($obj->activer == 1) { |
||
98 | return true; |
||
99 | } |
||
100 | else { |
||
101 | return false; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | private static function getUrlToId($url) { |
||
118 | |||
119 | |||
120 | //-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
||
121 | |||
122 | |||
123 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
124 | private function setListeModuleActiver($id_module, $url, $version, $nom, $icone = null, $url_telechargement = null) { |
||
132 | |||
133 | /** |
||
134 | * @param $activer |
||
135 | * @param $url |
||
136 | * fonction qui permet d'activer || désactiver un module |
||
137 | */ |
||
138 | public static function setActiverDesactiverModule($activer, $url) { |
||
152 | //-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
||
153 | } |
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.