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 |
||
| 9 | class PermissionsFaction { |
||
| 10 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 11 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 12 | |||
| 13 | |||
| 14 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 15 | /** |
||
| 16 | * @param $id_identite |
||
| 17 | * @param $id_faction |
||
| 18 | * @return bool |
||
| 19 | * permet de savoir si le joueur en question est le chef de la faction |
||
| 20 | */ |
||
| 21 | View Code Duplication | protected function getTestChefFaction($id_identite, $id_faction) { |
|
| 22 | $dbc = App::getDb(); |
||
| 23 | |||
| 24 | $query = $dbc->select("ID_identite")->from("_bataille_faction") |
||
| 25 | ->where("ID_identite", "=", $id_identite, "AND") |
||
| 26 | ->where("ID_faction", "=", $id_faction) |
||
| 27 | ->get(); |
||
| 28 | |||
| 29 | if (count($query) > 0) { |
||
| 30 | return true; |
||
| 31 | } |
||
| 32 | |||
| 33 | return false; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return int |
||
| 38 | * fonction qui renvoi le nombre de permissions |
||
| 39 | */ |
||
| 40 | private function getNombrePermissions() { |
||
| 41 | $dbc = App::getDb(); |
||
| 42 | |||
| 43 | $query = $dbc->select()->from("_bataille_faction_permissions")->get(); |
||
| 44 | |||
| 45 | return count($query); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return array |
||
| 50 | * fonction qui liste toutes les permissions |
||
| 51 | */ |
||
| 52 | public function getListePermissions() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param $id_identite |
||
| 70 | * @param $id_faction |
||
| 71 | * @return array|string |
||
| 72 | * permet de récupérer les permissions d'un membre de la faction |
||
| 73 | */ |
||
| 74 | protected function getMembrePermissions($id_identite, $id_faction) { |
||
| 75 | $dbc = App::getDb(); |
||
| 76 | |||
| 77 | $nb_permissions = $this->getNombrePermissions(); |
||
| 78 | |||
| 79 | if ($this->getTestChefFaction($id_identite, $id_faction) === true) { |
||
| 80 | $permissions = []; |
||
| 81 | for ($i=1 ; $i<=$nb_permissions ; $i++) { |
||
| 82 | $permissions[$i] = "checked"; |
||
| 83 | } |
||
| 84 | |||
| 85 | return $permissions; |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | $permissions = []; |
||
| 90 | for ($i=1 ; $i<=$nb_permissions ; $i++) { |
||
| 91 | $query = $dbc->select()->from("_bataille_faction_permission_player") |
||
| 92 | ->where("ID_faction", "=", $id_faction, "AND") |
||
| 93 | ->where("ID_identite", "=", $id_identite, "AND") |
||
| 94 | ->where("ID_permission", "=", $i) |
||
| 95 | ->get(); |
||
| 96 | |||
| 97 | $permissions[$i] = ""; |
||
| 98 | if (count($query) == 1) { |
||
| 99 | $permissions[$i] = "checked"; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | return $permissions; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $id_faction |
||
| 109 | * @return bool |
||
| 110 | * fonction qui renvoi les permission du membre connecté |
||
| 111 | */ |
||
| 112 | public function getPermissionsMembre($id_faction) { |
||
| 138 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 139 | |||
| 140 | |||
| 141 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 142 | /** |
||
| 143 | * @param $id_permission |
||
| 144 | * @param $id_identite |
||
| 145 | * @param $id_faction |
||
| 146 | * @param $type |
||
| 147 | * cette fonction permet d'ajouter ou de supprimer un permission à un joueur |
||
| 148 | */ |
||
| 149 | public function setGererPermission($id_permission, $id_identite, $id_faction, $type) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $id_identite |
||
| 182 | * fonction qui va supprimer tous les droits d'accès du user |
||
| 183 | */ |
||
| 184 | protected function setSupprilerAllPermissions($id_identite) { |
||
| 189 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 190 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.