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() { |
||
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 | $query = $dbc->select("pseudo")->from("identite")->where("ID_identite", "=", $id_identite)->get(); |
||
115 | |||
116 | if ((is_array($query)) && (count($query) == 1)) { |
||
117 | foreach ($query as $obj) { |
||
118 | Bataille::setValues(["pseudo" => $obj->pseudo]); |
||
119 | } |
||
120 | |||
121 | $query = $dbc->select() |
||
122 | ->from("_bataille_base") |
||
123 | ->where("ID_identite", "=", $id_identite, "AND") |
||
124 | ->where("ID_base", "!=", $exclude) |
||
125 | ->orderBy("ID_base", "DESC") |
||
126 | ->get(); |
||
127 | |||
128 | View Code Duplication | if ((is_array($query)) && (count($query) > 0)) { |
|
1 ignored issue
–
show
|
|||
129 | foreach ($query as $obj) { |
||
130 | $une_base[] = [ |
||
131 | "id_base" => $obj->ID_base, |
||
132 | "nom_base" => $obj->nom_base, |
||
133 | "points_base" => $obj->points, |
||
134 | "posx" => $obj->posx, |
||
135 | "posy" => $obj->posy |
||
136 | ]; |
||
137 | } |
||
138 | |||
139 | Bataille::setValues(["base_joueur" => $une_base]); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
144 | |||
145 | |||
146 | |||
147 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
148 | private function setBatimentsBase($batiments) { |
||
151 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
152 | } |
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.