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 $unite; |
||
| 12 | private static $centre_recherche; |
||
| 13 | private static $map; |
||
| 14 | private static $database; |
||
| 15 | private static $nation; |
||
| 16 | |||
| 17 | private static $id_base; |
||
| 18 | |||
| 19 | public static $values = []; |
||
| 20 | |||
| 21 | |||
| 22 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 23 | public function __construct() { |
||
| 26 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 31 | /** |
||
| 32 | * @return array |
||
| 33 | * get array of all values wich will be used in the page |
||
| 34 | */ |
||
| 35 | public static function getValues() { |
||
| 38 | |||
| 39 | //initilisation of all classes of battle |
||
| 40 | //initialisation of Ressource class |
||
| 41 | public static function getRessource() { |
||
| 48 | |||
| 49 | //initialisation of Base class |
||
| 50 | public static function getBase() { |
||
| 57 | |||
| 58 | //initialisation of Batiment class |
||
| 59 | public static function getBatiment() { |
||
| 66 | |||
| 67 | //initialisation of Batiment class |
||
| 68 | public static function getPoints() { |
||
| 75 | |||
| 76 | //initialisation of Batiment class |
||
| 77 | public static function getMap() { |
||
| 84 | |||
| 85 | //initialisation of Batiment class |
||
| 86 | public static function getUnite() { |
||
| 93 | |||
| 94 | //initialisation of Batiment class |
||
| 95 | public static function getCentreRecherche() { |
||
| 102 | |||
| 103 | //initialisation of Database Core connexion |
||
| 104 | public static function getDb() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return mixe |
||
| 113 | * récupère l'ID_identité du joueur |
||
| 114 | */ |
||
| 115 | public static function getIdIdentite() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return mixed |
||
| 121 | * renvoi l'id_base du joueur |
||
| 122 | */ |
||
| 123 | public static function getIdBase() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return mixed |
||
| 135 | * renvoi le premier ID_base du joueur (première base et base princ du joueur) |
||
| 136 | */ |
||
| 137 | View Code Duplication | public static function getFirstBase() { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param $id_base |
||
| 153 | * @return array |
||
| 154 | * fonction qui renvoi les posisitons en x et y d'une base |
||
| 155 | */ |
||
| 156 | private static function getPosistionBase($id_base) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return int |
||
| 175 | * return now timestamp |
||
| 176 | */ |
||
| 177 | public static function getToday() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param null $id_base -> sert si definit a recuperer l'id identite de la abse en question |
||
| 184 | * @return mixed |
||
| 185 | * recupere la date de la derniere connexion |
||
| 186 | */ |
||
| 187 | public static function getLastConnexion($id_base = null) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param $nom_ressource |
||
| 212 | * @param $ressource |
||
| 213 | * @return array |
||
| 214 | * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
||
| 215 | * ou pour creer une unité... |
||
| 216 | */ |
||
| 217 | public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param $id_base |
||
| 236 | * @param null $vitesse = vitesse de l'unité en question |
||
| 237 | * @return number |
||
| 238 | * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes |
||
| 239 | */ |
||
| 240 | public static function getDureeTrajet($id_base, $vitesse = 1) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param null $id_identite |
||
| 257 | * get nation of a player |
||
| 258 | */ |
||
| 259 | public static function getNation($id_identite = null) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param $posx |
||
| 282 | * @param $posy |
||
| 283 | * @return int |
||
| 284 | * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante |
||
| 285 | */ |
||
| 286 | View Code Duplication | public static function getBaseExistPosition($posx, $posy) { |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @return int |
||
| 303 | * fonction qui permet de récupérer le nombre de joueurs sur le serveur |
||
| 304 | */ |
||
| 305 | public static function getNombreJoueur() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param $param |
||
| 319 | * @return mixed |
||
| 320 | * fonction qui sert à récupérer un parametre spécifique pour un batiment |
||
| 321 | * par exemple la vitesse d'un marchand ou le nombred'emplacment de la base |
||
| 322 | */ |
||
| 323 | public static function getParam($param) { |
||
| 332 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 337 | /** |
||
| 338 | * set la date de derniere connexion a now |
||
| 339 | */ |
||
| 340 | public static function setLastConnexion($id_base = null) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param $values |
||
| 360 | * can set values while keep older infos |
||
| 361 | */ |
||
| 362 | public static function setValues($values) { |
||
| 365 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 366 | } |
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: