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 |
||
| 5 | class Base { |
||
| 6 | private $batiments; |
||
| 7 | |||
| 8 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 9 | public function __construct() { |
||
| 12 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 17 | public function getBatiments() { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * fonction qui va initaliser la base |
||
| 23 | */ |
||
| 24 | public function getMaBase() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param null $id_base |
||
| 43 | * fonction qui va récupérer les infos de la base |
||
| 44 | */ |
||
| 45 | public function getInfoBase($id_base = null) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * fonction qui recupere tous les batiments de la base |
||
| 66 | */ |
||
| 67 | public function getBatimentsBase() { |
||
| 68 | $dbc = App::getDb(); |
||
| 69 | |||
| 70 | $query = $dbc->select()->from("_bataille_batiment")->where("ID_base", "=", Bataille::getIdBase())->get(); |
||
| 71 | |||
| 72 | if (count($query) > 0) { |
||
| 73 | $batiments = []; |
||
| 74 | |||
| 75 | foreach ($query as $obj) { |
||
| 76 | $taille_batiment = Bataille::getBatiment()->getTailleBatiment($obj->nom_batiment_sql); |
||
| 77 | $construction = ""; |
||
| 78 | |||
| 79 | if ($obj->construction) { |
||
| 80 | $construction = "construction"; |
||
| 81 | } |
||
| 82 | |||
| 83 | $batiments[] = [ |
||
| 84 | "nom_batiment" => $obj->nom_batiment, |
||
| 85 | "nom_batiment_sql" => $obj->nom_batiment_sql, |
||
| 86 | "niveau" => $obj->niveau, |
||
| 87 | "posx" => $obj->posx, |
||
| 88 | "posy" => $obj->posy, |
||
| 89 | "width" => $taille_batiment[0], |
||
| 90 | "height" => $taille_batiment[1], |
||
| 91 | "construction" => $construction |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | Bataille::setValues(["batiments" => $batiments]); |
||
| 96 | |||
| 97 | $this->setBatimentsBase($batiments); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param null $id_identite |
||
| 103 | * fonction qui récupère les bases d'un joueur |
||
| 104 | */ |
||
| 105 | public function getBasesJoueur($id_identite = null) { |
||
| 106 | $dbc = App::getDb(); |
||
| 107 | $exclude = 0; |
||
| 108 | |||
| 109 | if ($id_identite === null) { |
||
| 110 | $id_identite = Bataille::getIdIdentite(); |
||
| 111 | $exclude = Bataille::getIdBase(); |
||
| 112 | } |
||
| 113 | |||
| 114 | $this->getPseudoJoueur($id_identite); |
||
| 115 | |||
| 116 | $query = $dbc->select() |
||
| 117 | ->from("_bataille_base") |
||
| 118 | ->where("ID_identite", "=", $id_identite, "AND") |
||
| 119 | ->where("ID_base", "!=", $exclude) |
||
| 120 | ->orderBy("ID_base", "DESC") |
||
| 121 | ->get(); |
||
| 122 | |||
| 123 | View Code Duplication | if ((is_array($query)) && (count($query) > 0)) { |
|
| 124 | $une_base = []; |
||
| 125 | foreach ($query as $obj) { |
||
| 126 | $une_base[] = [ |
||
| 127 | "id_base" => $obj->ID_base, |
||
| 128 | "nom_base" => $obj->nom_base, |
||
| 129 | "points_base" => $obj->points, |
||
| 130 | "posx" => $obj->posx, |
||
| 131 | "posy" => $obj->posy |
||
| 132 | ]; |
||
| 133 | } |
||
| 134 | |||
| 135 | Bataille::setValues(["base_joueur" => $une_base]); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param $id_identite |
||
| 141 | * fonction qui renvoi le pseudo d'un joueur en fonction de son ID |
||
| 142 | */ |
||
| 143 | private function getPseudoJoueur($id_identite) { |
||
| 152 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 157 | private function setBatimentsBase($batiments) { |
||
| 160 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 161 | } |