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 $centre_recherche; |
||
12 | private static $missions_aleatoire; |
||
13 | private static $database; |
||
14 | private static $nation; |
||
15 | |||
16 | private static $id_base; |
||
17 | |||
18 | public static $values = []; |
||
19 | |||
20 | |||
21 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
22 | public function __construct() { |
||
25 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
26 | |||
27 | |||
28 | |||
29 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
30 | /** |
||
31 | * @return array |
||
32 | * get array of all values wich will be used in the page |
||
33 | */ |
||
34 | public static function getValues() { |
||
37 | |||
38 | //initilisation of all classes of battle |
||
39 | //initialisation of Ressource class |
||
40 | public static function getRessource() { |
||
47 | |||
48 | //initialisation of Base class |
||
49 | public static function getBase() { |
||
56 | |||
57 | //initialisation of Batiment class |
||
58 | public static function getBatiment() { |
||
65 | |||
66 | //initialisation of Batiment class |
||
67 | public static function getUnite() { |
||
74 | |||
75 | //initialisation of CentreRecherche class |
||
76 | public static function getCentreRecherche() { |
||
83 | |||
84 | //initialisation of MissionsAleatoire class |
||
85 | public static function getMissionsAleatoire() { |
||
92 | |||
93 | //initialisation of Database Core connexion |
||
94 | public static function getDb() { |
||
102 | |||
103 | /** |
||
104 | * @return mixe |
||
105 | * récupère l'ID_identité du joueur |
||
106 | */ |
||
107 | public static function getIdIdentite() { |
||
110 | |||
111 | /** |
||
112 | * @return mixed |
||
113 | * renvoi l'id_base du joueur |
||
114 | */ |
||
115 | public static function getIdBase() { |
||
124 | |||
125 | /** |
||
126 | * @return mixed |
||
127 | * renvoi le premier ID_base du joueur (première base et base princ du joueur) |
||
128 | */ |
||
129 | public static function getFirstBase() { |
||
142 | |||
143 | /** |
||
144 | * @param $id_base |
||
145 | * @return array |
||
146 | * fonction qui renvoi les posisitons en x et y d'une base |
||
147 | */ |
||
148 | private static function getPosistionBase($id_base) { |
||
167 | |||
168 | /** |
||
169 | * @return int |
||
170 | * return now timestamp |
||
171 | */ |
||
172 | public static function getToday() { |
||
176 | |||
177 | /** |
||
178 | * @param string $nom_ressource |
||
179 | * @param $ressource |
||
180 | * @return array |
||
181 | * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
||
182 | * ou pour creer une unité... |
||
183 | */ |
||
184 | public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
||
199 | |||
200 | /** |
||
201 | * @param $id_base |
||
202 | * @param integer $vitesse = vitesse de l'unité en question |
||
203 | * @return number |
||
204 | * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes |
||
205 | */ |
||
206 | public static function getDureeTrajet($id_base, $vitesse = 1) { |
||
220 | |||
221 | /** |
||
222 | * @param null $id_identite |
||
223 | * get nation of a player |
||
224 | */ |
||
225 | public static function getNation($id_identite = null) { |
||
245 | |||
246 | /** |
||
247 | * @param $posx |
||
248 | * @param $posy |
||
249 | * @return int |
||
250 | * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante |
||
251 | */ |
||
252 | public static function getBaseExistPosition($posx, $posy) { |
||
266 | |||
267 | /** |
||
268 | * @param string $param |
||
269 | * @return mixed |
||
270 | * fonction qui sert à récupérer un parametre spécifique pour un batiment |
||
271 | * par exemple la vitesse d'un marchand ou le nombred'emplacment de la base |
||
272 | */ |
||
273 | public static function getParam($param) { |
||
282 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
283 | |||
284 | |||
285 | |||
286 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
287 | /** |
||
288 | * @param $values |
||
289 | * can set values while keep older infos |
||
290 | */ |
||
291 | public static function setValues($values) { |
||
294 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
295 | } |
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.