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 |
||
8 | class Points { |
||
9 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
10 | public function __construct($start = null) { |
||
42 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
43 | |||
44 | |||
45 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
46 | /** |
||
47 | * @param $id_base |
||
48 | * @return int |
||
49 | * renvoi les points de la base |
||
50 | */ |
||
51 | public static function getPointsBase($id_base) { |
||
69 | |||
70 | /** |
||
71 | * @param null $id_identite |
||
72 | * @return int |
||
73 | * renvoi les points totaux d'un joueur |
||
74 | */ |
||
75 | public static function getPointsJoueur($id_identite=null) { |
||
92 | |||
93 | /** |
||
94 | * @return mixed |
||
95 | * fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment |
||
96 | */ |
||
97 | private static function getPointAjoutBatiment() { |
||
100 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
101 | |||
102 | |||
103 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
104 | /** |
||
105 | * @param $id_base |
||
106 | * @param null $type |
||
107 | * @param null $points |
||
108 | * @return int|null |
||
109 | * fonction qui ajoute des points à la base en fonction du type |
||
110 | * le type peut etre : batiment, attaque, defense, troupe |
||
111 | */ |
||
112 | public static function setAjouterPoints($id_base, $type=null, $points=null) { |
||
130 | |||
131 | /** |
||
132 | * fonction qui prend les points de toutes les bases et qui les ajoute sur le joueur en lui même |
||
133 | */ |
||
134 | private static function setAjouterPointsTotaux() { |
||
149 | |||
150 | /** |
||
151 | * fonction qui permet d'ajouter tous les points du joueur aux points de la faction |
||
152 | */ |
||
153 | public static function setRejoindreQuitterFaction($del = null) { |
||
178 | |||
179 | /** |
||
180 | * @param $points |
||
181 | * permet d'ajouter des points à la faction |
||
182 | */ |
||
183 | public static function setAjouterPointsFaction($points) { |
||
201 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
202 | |||
203 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.