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 Unite 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 Unite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Unite { |
||
11 | private $coef_unite; |
||
12 | private $pour_recruter; |
||
13 | private $temps_recrutement; |
||
14 | |||
15 | |||
16 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
17 | public function __construct() { |
||
20 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
21 | |||
22 | |||
23 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
24 | |||
25 | /** |
||
26 | * @param $unite |
||
27 | * @param $niveau |
||
28 | * @param $type |
||
29 | * @return array |
||
30 | * récupère les caractéristiques de l'unité en fonction de son niveau |
||
31 | */ |
||
32 | public function getCaracteristiqueUnite($unite, $niveau, $type) { |
||
72 | |||
73 | /** |
||
74 | * @return array |
||
75 | * fonction qui renvoit tous les types d'unités qu'il est possible de recruter |
||
76 | */ |
||
77 | private function getAllType() { |
||
80 | |||
81 | /** |
||
82 | * @param $type |
||
83 | * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction |
||
84 | * du type (batiment sur lequel on a cliqué) |
||
85 | */ |
||
86 | public function getUnitePossibleRecruter($type) { |
||
98 | |||
99 | /** |
||
100 | * fonction qui renvoi les unité en cours de recrutement |
||
101 | */ |
||
102 | public function getRecrutement() { |
||
128 | |||
129 | /** |
||
130 | * @param null $id_base |
||
131 | * @param null $id_groupe |
||
132 | * fonction qui récupère toutes les unités qui sont dans la base |
||
133 | */ |
||
134 | public function getAllUnites($id_base = null, $id_groupe = null) { |
||
156 | |||
157 | /** |
||
158 | * @param $type |
||
159 | * @param $id_base |
||
160 | * @param null $id_groupe |
||
161 | * @return array |
||
162 | * fonction qui récupère toutes les unités en fonction d'un type précis |
||
163 | */ |
||
164 | private function getAllUniteType($type, $id_base, $id_groupe = null) { |
||
200 | |||
201 | /** |
||
202 | * @param $type |
||
203 | * @param $nom |
||
204 | * @return int |
||
205 | * renvoi le nombre d'unite en fonction d'un type et d'un nom qui ne sont ni dans un groupe ni |
||
206 | * en mission |
||
207 | */ |
||
208 | protected function getNombreUniteNom($type, $nom) { |
||
222 | |||
223 | /** |
||
224 | * @return int |
||
225 | * fonction qui renvoi le nombre d'unité vivante dans la base qui consomme de la nourriture |
||
226 | */ |
||
227 | View Code Duplication | public function getNombreUniteHumaine() { |
|
237 | |||
238 | /** |
||
239 | * @param $id_mission |
||
240 | * @return int |
||
241 | * fonction qui renvoi le nombre d'unités envoyées sur une mission en particulier |
||
242 | */ |
||
243 | View Code Duplication | public function getUnitesMission($id_mission) { |
|
253 | |||
254 | /** |
||
255 | * @param $type |
||
256 | * @param $nom |
||
257 | * récupération du temmp de recrutement + les ressources nécéssaires |
||
258 | */ |
||
259 | private function getInfosRecrutementUnite($type, $nom) { |
||
276 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
277 | |||
278 | |||
279 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
280 | /** |
||
281 | * @param $nom -> nom de l'unité à recruter |
||
282 | * @param $type -> type de l'unité à recruter |
||
283 | * @param $nombre -> nombre d'unité à recruter |
||
284 | * fonction qui permet d'initialiser le début du recrutement d'unités |
||
285 | */ |
||
286 | public function setCommencerRecruter($nom, $type, $nombre) { |
||
324 | |||
325 | /** |
||
326 | * @param $id_recrutement |
||
327 | * fonction appellée dans celle qui récupère les recrutement uniquement quand celui ci est finit |
||
328 | * fonction qui sert à terminer un rcrutement et ajouter les unités dans la base |
||
329 | */ |
||
330 | private function setTerminerRecrutement($id_recrutement) { |
||
353 | |||
354 | /** |
||
355 | * @param $nombre_unite |
||
356 | * @param $nom_unite |
||
357 | * @param $type_unite |
||
358 | * @param $id_mission |
||
359 | * @return bool |
||
360 | * permet de lancer des unites en expédition en ajoutant à chaque unité un id_mission |
||
361 | */ |
||
362 | public function setCommencerExpedition($nombre_unite, $nom_unite, $type_unite, $id_mission) { |
||
382 | |||
383 | /** |
||
384 | * @param $id_mission |
||
385 | * @param $pourcentage_perte |
||
386 | * @return int |
||
387 | * fonction qui termine une expdedition au niveau des troupes, cette fonction s'occupe d'en |
||
388 | * supprimer de la bdd en fonction du nombre de troupe envoyé et du cpourcentage de perte |
||
389 | */ |
||
390 | public function setTerminerExpedition($id_mission, $pourcentage_perte) { |
||
423 | |||
424 | /** |
||
425 | * @param $nombre |
||
426 | * fonction qui permet de tuer des unites |
||
427 | */ |
||
428 | public function setTuerUnites($nombre) { |
||
441 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
442 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: