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 Bataille { |
||
| 7 | private static $ressource; |
||
| 8 | private static $base; |
||
| 9 | private static $batiment; |
||
| 10 | private static $database; |
||
| 11 | |||
| 12 | private static $id_base; |
||
| 13 | |||
| 14 | public static $values = []; |
||
| 15 | |||
| 16 | |||
| 17 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 18 | public function __construct() { |
||
| 21 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 26 | /** |
||
| 27 | * @return array |
||
| 28 | * get array of all values wich will be used in the page |
||
| 29 | */ |
||
| 30 | public static function getValues() { |
||
| 33 | |||
| 34 | //initilisation of all classes of battle |
||
| 35 | //initialisation of Ressource class |
||
| 36 | public static function getRessource() { |
||
| 43 | |||
| 44 | //initialisation of Base class |
||
| 45 | public static function getBase() { |
||
| 52 | |||
| 53 | //initialisation of Batiment class |
||
| 54 | public static function getBatiment() { |
||
| 61 | |||
| 62 | //initialisation of Database Core connexion |
||
| 63 | public static function getDb() { |
||
| 69 | |||
| 70 | public static function getIdIdentite() { |
||
| 73 | |||
| 74 | public static function getIdBase() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return mixed |
||
| 86 | * recupere la date de la derniere connexion |
||
| 87 | */ |
||
| 88 | View Code Duplication | public static function getLastConnexion() { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return mixed |
||
| 103 | * recupere le nombre maximum d'emplacement dans la base |
||
| 104 | */ |
||
| 105 | View Code Duplication | public static function getNombreEmplacementBase() { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param $nom_ressource |
||
| 119 | * @param $ressource |
||
| 120 | * @return array |
||
| 121 | * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
||
| 122 | * ou pour creer une unité... |
||
| 123 | */ |
||
| 124 | public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $id_base |
||
| 143 | * @return number |
||
| 144 | * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base |
||
| 145 | */ |
||
| 146 | public static function getDureeTrajet($id_base) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param $id_base |
||
| 163 | * @return array |
||
| 164 | * fonction qui renvoi les posisitons en x et y d'une base |
||
| 165 | */ |
||
| 166 | private static function getPosistionBase($id_base) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param null $id_identite |
||
| 185 | * get nation of a player |
||
| 186 | */ |
||
| 187 | public static function getNationBase($id_identite = null) { |
||
| 207 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 208 | |||
| 209 | |||
| 210 | |||
| 211 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 212 | /** |
||
| 213 | * set la date de derniere connexion a now |
||
| 214 | */ |
||
| 215 | public static function setLastConnexion() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param $values |
||
| 226 | * can set values while keep older infos |
||
| 227 | */ |
||
| 228 | public static function setValues($values) { |
||
| 231 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 232 | } |
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: