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) { |
||
43 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
44 | |||
45 | |||
46 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
47 | /** |
||
48 | * @param $id_base |
||
49 | * @return int |
||
50 | * renvoi les points de la base |
||
51 | */ |
||
52 | public static function getPointsBase($id_base) { |
||
70 | |||
71 | /** |
||
72 | * @param null $id_identite |
||
73 | * @return int |
||
74 | * renvoi les points totaux d'un joueur |
||
75 | */ |
||
76 | public static function getPointsJoueur($id_identite=null) { |
||
93 | |||
94 | /** |
||
95 | * @return mixed |
||
96 | * fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment |
||
97 | */ |
||
98 | private static function getPointAjoutBatiment() { |
||
101 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
102 | |||
103 | |||
104 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
105 | /** |
||
106 | * @param $id_base |
||
107 | * @param null $type |
||
108 | * @param null $points |
||
109 | * @return int|null |
||
110 | * fonction qui ajoute des points à la base en fonction du type |
||
111 | * le type peut etre : batiment, attaque, defense, troupe |
||
112 | */ |
||
113 | public static function setAjouterPoints($id_base, $type=null, $points=null) { |
||
131 | |||
132 | /** |
||
133 | * fonction qui prend les points de toutes les bases et qui les ajoute sur le joueur en lui même |
||
134 | */ |
||
135 | private static function setAjouterPointsTotaux() { |
||
150 | |||
151 | /** |
||
152 | * fonction qui permet d'ajouter tous les points du joueur aux points de la faction |
||
153 | */ |
||
154 | public static function setRejoindreQuitterFaction($del = null) { |
||
179 | |||
180 | /** |
||
181 | * @param $points |
||
182 | * permet d'ajouter des points à la faction |
||
183 | */ |
||
184 | public static function setAjouterPointsFaction($points) { |
||
202 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
203 | |||
204 | } |
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.