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:
Complex classes like Bataille often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Bataille, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Bataille { |
||
7 | private static $ressource; |
||
8 | private static $base; |
||
9 | private static $batiment; |
||
10 | private static $points; |
||
11 | private static $map; |
||
12 | private static $database; |
||
13 | private static $nation; |
||
14 | |||
15 | private static $id_base; |
||
16 | |||
17 | public static $values = []; |
||
18 | |||
19 | |||
20 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
21 | public function __construct() { |
||
24 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
25 | |||
26 | |||
27 | |||
28 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
29 | /** |
||
30 | * @return array |
||
31 | * get array of all values wich will be used in the page |
||
32 | */ |
||
33 | public static function getValues() { |
||
36 | |||
37 | //initilisation of all classes of battle |
||
38 | //initialisation of Ressource class |
||
39 | public static function getRessource() { |
||
46 | |||
47 | //initialisation of Base class |
||
48 | public static function getBase() { |
||
55 | |||
56 | //initialisation of Batiment class |
||
57 | public static function getBatiment() { |
||
64 | |||
65 | //initialisation of Batiment class |
||
66 | public static function getPoints() { |
||
73 | |||
74 | //initialisation of Batiment class |
||
75 | public static function getMap() { |
||
82 | |||
83 | //initialisation of Database Core connexion |
||
84 | public static function getDb() { |
||
90 | |||
91 | /** |
||
92 | * @return mixe |
||
93 | * récupère l'ID_identité du joueur |
||
94 | */ |
||
95 | public static function getIdIdentite() { |
||
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | * renvoi l'id_base du joueur |
||
102 | */ |
||
103 | public static function getIdBase() { |
||
112 | |||
113 | /** |
||
114 | * @return mixed |
||
115 | * renvoi le premier ID_base du joueur (première base et base princ du joueur) |
||
116 | */ |
||
117 | View Code Duplication | public static function getFirstBase() { |
|
130 | |||
131 | /** |
||
132 | * @param $id_base |
||
133 | * @return array |
||
134 | * fonction qui renvoi les posisitons en x et y d'une base |
||
135 | */ |
||
136 | private static function getPosistionBase($id_base) { |
||
152 | |||
153 | /** |
||
154 | * @return int |
||
155 | * return now timestamp |
||
156 | */ |
||
157 | public static function getToday() { |
||
161 | |||
162 | /** |
||
163 | * @param null $id_base -> sert si definit a recuperer l'id identite de la abse en question |
||
164 | * @return mixed |
||
165 | * recupere la date de la derniere connexion |
||
166 | */ |
||
167 | public static function getLastConnexion($id_base = null) { |
||
189 | |||
190 | /** |
||
191 | * @param $nom_ressource |
||
192 | * @param $ressource |
||
193 | * @return array |
||
194 | * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
||
195 | * ou pour creer une unité... |
||
196 | */ |
||
197 | public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
||
213 | |||
214 | /** |
||
215 | * @param $id_base |
||
216 | * @param null $vitesse = vitesse de l'unité en question |
||
217 | * @return number |
||
218 | * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes |
||
219 | */ |
||
220 | public static function getDureeTrajet($id_base, $vitesse = 1) { |
||
234 | |||
235 | /** |
||
236 | * @param null $id_identite |
||
237 | * get nation of a player |
||
238 | */ |
||
239 | public static function getNation($id_identite = null) { |
||
259 | |||
260 | /** |
||
261 | * @param $posx |
||
262 | * @param $posy |
||
263 | * @return int |
||
264 | * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante |
||
265 | */ |
||
266 | View Code Duplication | public static function getBaseExistPosition($posx, $posy) { |
|
280 | |||
281 | /** |
||
282 | * @return int |
||
283 | * fonction qui permet de récupérer le nombre de joueurs sur le serveur |
||
284 | */ |
||
285 | public static function getNombreJoueur() { |
||
296 | |||
297 | /** |
||
298 | * @param $param |
||
299 | * @return mixed |
||
300 | * fonction qui sert à récupérer un parametre spécifique pour un batiment |
||
301 | * par exemple la vitesse d'un marchand ou le nombred'emplacment de la base |
||
302 | */ |
||
303 | public static function getParam($param) { |
||
312 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
313 | |||
314 | |||
315 | |||
316 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
317 | /** |
||
318 | * set la date de derniere connexion a now |
||
319 | */ |
||
320 | public static function setLastConnexion($id_base = null) { |
||
337 | |||
338 | /** |
||
339 | * @param $values |
||
340 | * can set values while keep older infos |
||
341 | */ |
||
342 | public static function setValues($values) { |
||
345 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
346 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: