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 $unite; |
||
11 | private static $groupe_unite; |
||
12 | private static $centre_recherche; |
||
13 | private static $missions_aleatoire; |
||
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 Unite class |
||
68 | public static function getUnite() { |
||
69 | if (self::$unite == null) { |
||
70 | self::$unite = new Unite(); |
||
71 | } |
||
72 | |||
73 | return self::$unite; |
||
74 | } |
||
75 | |||
76 | //initialisation of GroupeUnite class |
||
77 | public static function getGoupeUnite() { |
||
78 | if (self::$groupe_unite == null) { |
||
79 | self::$groupe_unite = new GroupeUnite(); |
||
80 | } |
||
81 | |||
82 | return self::$groupe_unite; |
||
83 | } |
||
84 | |||
85 | //initialisation of CentreRecherche class |
||
86 | public static function getCentreRecherche() { |
||
87 | if (self::$centre_recherche == null) { |
||
88 | self::$centre_recherche = new CentreRecherche(); |
||
89 | } |
||
90 | |||
91 | return self::$centre_recherche; |
||
92 | } |
||
93 | |||
94 | //initialisation of MissionsAleatoire class |
||
95 | public static function getMissionsAleatoire() { |
||
96 | if (self::$missions_aleatoire == null) { |
||
97 | self::$missions_aleatoire = new MissionsAleatoire(); |
||
98 | } |
||
99 | |||
100 | return self::$missions_aleatoire; |
||
101 | } |
||
102 | |||
103 | //initialisation of Database Core connexion |
||
104 | public static function getDb() { |
||
105 | require_once("config.config.php"); |
||
106 | |||
107 | if (self::$database == null) { |
||
108 | self::$database = new Database($type_co, $dbname, $dbuser, $dbpass, $dbhost); |
||
109 | } |
||
110 | return self::$database; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return mixe |
||
115 | * récupère l'ID_identité du joueur |
||
116 | */ |
||
117 | public static function getIdIdentite() { |
||
120 | |||
121 | /** |
||
122 | * @return mixed |
||
123 | * renvoi l'id_base du joueur |
||
124 | */ |
||
125 | public static function getIdBase() { |
||
134 | |||
135 | /** |
||
136 | * @return mixed |
||
137 | * renvoi le premier ID_base du joueur (première base et base princ du joueur) |
||
138 | */ |
||
139 | public static function getFirstBase() { |
||
152 | |||
153 | /** |
||
154 | * @param $id_base |
||
155 | * @return array |
||
156 | * fonction qui renvoi les posisitons en x et y d'une base |
||
157 | */ |
||
158 | private static function getPosistionBase($id_base) { |
||
159 | $dbc = App::getDb(); |
||
160 | |||
161 | $posx = 0; |
||
162 | $posy = 0; |
||
163 | |||
164 | $query = $dbc->select("posx") |
||
165 | ->select("posy") |
||
166 | ->from("_bataille_base") |
||
167 | ->where("ID_base", "=", $id_base) |
||
168 | ->get(); |
||
169 | |||
170 | foreach ($query as $obj) { |
||
171 | $posx = $obj->posx; |
||
172 | $posy = $obj->posy; |
||
173 | } |
||
174 | |||
175 | return ["posx" => $posx, "posy" => $posy]; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return int |
||
180 | * return now timestamp |
||
181 | */ |
||
182 | public static function getToday() { |
||
186 | |||
187 | /** |
||
188 | * @param string $nom_ressource |
||
189 | * @param $ressource |
||
190 | * @return array |
||
191 | * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
||
192 | * ou pour creer une unité... |
||
193 | */ |
||
194 | public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
||
209 | |||
210 | /** |
||
211 | * @param $id_base |
||
212 | * @param integer $vitesse = vitesse de l'unité en question |
||
213 | * @return number |
||
214 | * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes |
||
215 | */ |
||
216 | public static function getDureeTrajet($id_base, $vitesse = 1) { |
||
230 | |||
231 | /** |
||
232 | * @param null $id_identite |
||
233 | * get nation of a player |
||
234 | */ |
||
235 | public static function getNation($id_identite = null) { |
||
255 | |||
256 | /** |
||
257 | * @param $posx |
||
258 | * @param $posy |
||
259 | * @return int |
||
260 | * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante |
||
261 | */ |
||
262 | public static function getBaseExistPosition($posx, $posy) { |
||
276 | |||
277 | /** |
||
278 | * @param string $param |
||
279 | * @return mixed |
||
280 | * fonction qui sert à récupérer un parametre spécifique pour un batiment |
||
281 | * par exemple la vitesse d'un marchand ou le nombred'emplacment de la base |
||
282 | */ |
||
283 | public static function getParam($param) { |
||
292 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
293 | |||
294 | |||
295 | |||
296 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
297 | /** |
||
298 | * @param $values |
||
299 | * can set values while keep older infos |
||
300 | */ |
||
301 | public static function setValues($values) { |
||
304 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
305 | } |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.