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 |
||
| 7 | class Nourriture { |
||
| 8 | private $date_last_check; |
||
| 9 | |||
| 10 | |||
| 11 | //-------------------------- BUILDER ----------------------------------------------------------------------------// |
||
| 12 | public function __construct() { |
||
| 31 | //-------------------------- END BUILDER ----------------------------------------------------------------------------// |
||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
||
| 36 | /** |
||
| 37 | * @return int |
||
| 38 | * fonction qui renvoi la durée écoulée depuis le dernier check de la nourriture |
||
| 39 | */ |
||
| 40 | private function getLastCheckNourriture() { |
||
| 41 | $dbc= App::getDb(); |
||
| 42 | |||
| 43 | $query = $dbc->select("last_check_nourriture")->from("_bataille_base") |
||
| 44 | ->where("ID_identite", "=", Bataille::getIdIdentite(), "AND") |
||
| 45 | ->where("ID_base", "=", Bataille::getIdBase()) |
||
| 46 | ->get(); |
||
| 47 | |||
| 48 | if ((is_array($query)) && (count($query) == 1)) { |
||
| 49 | $today = Bataille::getToday(); |
||
| 50 | |||
| 51 | foreach ($query as $obj) { |
||
| 52 | $last_check = $obj->last_check_nourriture; |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->date_last_check = $last_check; |
||
|
|
|||
| 56 | |||
| 57 | $last_co = new \DateTime($last_check); |
||
| 58 | $last_co = $last_co->getTimestamp(); |
||
| 59 | |||
| 60 | return $today-$last_co; |
||
| 61 | } |
||
| 62 | |||
| 63 | return 0; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return mixed |
||
| 68 | * renvoi la consommation de nourriture d'une unité par heure |
||
| 69 | */ |
||
| 70 | private function getConsommationNourritureUnite() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return number |
||
| 76 | * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base |
||
| 77 | */ |
||
| 78 | private function getNombreUniteMortHeure() { |
||
| 79 | $nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); |
||
| 80 | |||
| 81 | if ($nb_unite == 0) { |
||
| 82 | return 0; |
||
| 83 | } |
||
| 84 | |||
| 85 | return abs(round((0-($nb_unite*$this->getConsommationNourritureUnite()))/100))+1; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return number |
||
| 90 | * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base |
||
| 91 | */ |
||
| 92 | private function getConsommationNourritureHeure() { |
||
| 97 | //-------------------------- END GETTER ----------------------------------------------------------------------------// |
||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
||
| 102 | /** |
||
| 103 | * remet la date de last_check_nourriture a Y-m-d H:i:s |
||
| 104 | */ |
||
| 105 | View Code Duplication | private function setUpdateLastCheckNourriture() { |
|
| 106 | $dbc = App::getDb(); |
||
| 107 | |||
| 108 | $dbc->update("last_check_nourriture", date("Y-m-d H:i:s"))->from("_bataille_base") |
||
| 109 | ->where("ID_identite", "=", Bataille::getIdIdentite(), "AND") |
||
| 110 | ->where("ID_base", "=", Bataille::getIdBase()) |
||
| 111 | ->set(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param $nb_heure |
||
| 116 | * @param $nb_unite |
||
| 117 | * fonction qui calcule la nourriture consomee en 1h |
||
| 118 | * si elle est inférieur à 0 on tue un nombre d'unite defini en fonction de combien le nombre de |
||
| 119 | * nourriture est en dessous de 0 (valeur divisée par 100) |
||
| 120 | * puis on retire les ressources du grenier |
||
| 121 | */ |
||
| 122 | private function setNourritureConsomee($nb_heure, $nb_unite) { |
||
| 136 | //-------------------------- END SETTER ----------------------------------------------------------------------------// |
||
| 137 | } |
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: