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 $url_telechargement; |
||
| 11 | |||
| 12 | |||
| 13 | //-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
| 14 | //-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
||
| 15 | |||
| 16 | |||
| 17 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 18 | public function getIdModule() { |
||
| 19 | return $this->id_module; |
||
| 20 | } |
||
| 21 | public function getUrl() { |
||
| 22 | return $this->url; |
||
| 23 | } |
||
| 24 | public function getNom() { |
||
| 25 | return $this->nom; |
||
| 26 | } |
||
| 27 | public function getVersion() { |
||
| 28 | return $this->version; |
||
| 29 | } |
||
| 30 | public function getOnlineVersion() { |
||
| 31 | return $this->online_version; |
||
| 32 | } |
||
| 33 | public function getIcone() { |
||
| 34 | return $this->icone; |
||
| 35 | } |
||
| 36 | public function getUrlTelechargement() { |
||
| 37 | return $this->url_telechargement; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * récupere la liste des modules activé utilisé pour toutes les pages |
||
| 42 | */ |
||
| 43 | public function getListeModuleActiver() { |
||
| 44 | $dbc = App::getDb(); |
||
| 45 | $query = $dbc->select()->from("module")->where("activer", "=", 1, "AND")->where("installer", "=", 1)->get(); |
||
| 46 | |||
| 47 | if ((is_array($query)) && (count($query) > 0)) { |
||
| 48 | $id_module = []; |
||
| 49 | $url = []; |
||
| 50 | $nom = []; |
||
| 51 | $version = []; |
||
| 52 | $icone = []; |
||
| 53 | |||
| 54 | foreach ($query as $obj) { |
||
| 55 | $id_module[] = $obj->ID_module; |
||
| 56 | $url[] = $obj->url; |
||
| 57 | $nom[] = $obj->nom_module; |
||
| 58 | $version[] = $obj->version; |
||
| 59 | $icone[] = $obj->icone; |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->setListeModuleActiver($id_module, $url, $version, $nom, $icone); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | */ |
||
| 68 | public function getListeModule() { |
||
| 69 | $dbc = App::getDb(); |
||
| 70 | |||
| 71 | $query = $dbc->select()->from("module")->get(); |
||
| 72 | |||
| 73 | if ((is_array($query)) && (count($query) > 0)) { |
||
| 74 | $id_module = []; |
||
| 75 | $url = []; |
||
| 76 | $nom = []; |
||
| 77 | $version = []; |
||
| 78 | $url_telechargement = []; |
||
| 79 | |||
| 80 | foreach ($query as $obj) { |
||
| 81 | $id_module[] = $obj->ID_module; |
||
| 82 | $url[] = $obj->url; |
||
| 83 | $nom[] = $obj->nom_module; |
||
| 84 | $version[] = $obj->version; |
||
| 85 | $url_telechargement[] = $obj->url_telechargement; |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->setListeModuleActiver($id_module, $url, $version, $nom, null, $url_telechargement); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param $nom_module |
||
| 94 | * @return bool |
||
| 95 | * permets de savoir si un module est installé ou non |
||
| 96 | */ |
||
| 97 | public static function getModuleInstaller($nom_module) { |
||
| 98 | $dbc = App::getDb(); |
||
| 99 | |||
| 100 | $query = $dbc->select("installer")->from("module")->where("nom_module", "=", $dbc->quote($nom_module), "", true)->get(); |
||
| 101 | |||
| 102 | if ((is_array($query)) && (count($query) > 0)) { |
||
| 103 | $installer = 0; |
||
| 104 | |||
| 105 | foreach ($query as $obj) { |
||
| 106 | $installer = $obj->installer; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $installer; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $nom_module |
||
| 115 | * @return boolean|null |
||
| 116 | * return true si le module est activer sinon false |
||
| 117 | */ |
||
| 118 | public static function getModuleActiver($nom_module) { |
||
| 119 | $dbc = App::getDb(); |
||
| 120 | |||
| 121 | $query = $dbc->select("activer")->from("module")->where("nom_module", "=", $dbc->quote($nom_module), "", true)->get(); |
||
| 122 | |||
| 123 | if ((is_array($query)) && (count($query) > 0)) { |
||
| 124 | foreach ($query as $obj) { |
||
| 125 | if ($obj->activer == 1) { |
||
| 126 | return true; |
||
| 127 | } |
||
| 128 | else { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | private static function getUrlToId($url) { |
||
| 136 | $dbc = App::getDb(); |
||
| 137 | |||
| 138 | $query = $dbc->select("ID_module")->from("module")->where("url", "=", $dbc->quote($url))->get(); |
||
| 139 | |||
| 140 | if ((is_array($query)) && (count($query) > 0)) { |
||
| 141 | foreach ($query as $obj) { |
||
| 142 | return $obj->ID_module; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | //-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
||
| 149 | |||
| 150 | |||
| 151 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 152 | private function setListeModuleActiver($id_module, $url, $version, $nom, $icone = null, $url_telechargement = null) { |
||
| 153 | $this->id_module = $id_module; |
||
| 154 | $this->url = $url; |
||
| 155 | $this->nom = $nom; |
||
| 156 | $this->version = $version; |
||
| 157 | $this->icone = $icone; |
||
| 158 | $this->url_telechargement = $url_telechargement; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $activer |
||
| 163 | * @param $url |
||
| 164 | * fonction qui permet d'activer || désactiver un module |
||
| 165 | */ |
||
| 166 | public static function setActiverDesactiverModule($activer, $url) { |
||
| 167 | $dbc = App::getDb(); |
||
| 168 | |||
| 169 | $dbc->update("activer", $activer)->from("module")->where("url", "=", $url)->set(); |
||
| 170 | |||
| 171 | $nav = new Navigation(); |
||
| 172 | |||
| 173 | if ($activer == 1) { |
||
| 174 | $nav->setAjoutLien("ID_module", self::getUrlToId($url)); |
||
| 175 | } |
||
| 176 | else { |
||
| 177 | $nav->setSupprimerLien("ID_module", self::getUrlToId($url)); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | //-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
||
| 181 | } |