| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 	namespace modules\bataille\app\controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	use core\App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	class Nourriture { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		private $date_last_check; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 		//-------------------------- BUILDER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 			$last_check = $this->getLastCheckNourriture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 			if ($last_check >= 3600) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 				$this->setNourritureConsomee(round($last_check/3600), $nb_unite); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 				$this->setUpdateLastCheckNourriture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			else if (($last_check == 0) && ($nb_unite > 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 				$this->setUpdateLastCheckNourriture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			if (Bataille::getRessource()->getNourriture() == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 				Bataille::setValues(["nourriture_mort_heure" => $this->getNombreUniteMortHeure()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 				Bataille::setValues(["nourriture_consom_heure" => $this->getConsommationNourritureHeure()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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_last_connexion")->where("ID_identite", "=", Bataille::getIdIdentite())->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			if ((is_array($query)) && (count($query) == 1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				$today = Bataille::getToday(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				foreach ($query as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 					$last_check = $obj->last_check_nourriture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				$this->date_last_check = $last_check; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 				$last_co = new \DateTime($last_check); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				$last_co = $last_co->getTimestamp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 				return $today-$last_co; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		 * renvoi la consommation de nourriture d'une unité par heure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		private function getConsommationNourritureUnite() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			return  Bataille::getParam("unite_nourriture_heure"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		 * @return number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		 * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		private function getNombreUniteMortHeure() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			return abs(round((0-($nb_unite*$this->getConsommationNourritureUnite()))/100)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		 * @return number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		 * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		private function getConsommationNourritureHeure() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			return round(($nb_unite*$this->getConsommationNourritureUnite())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		//-------------------------- END GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		//-------------------------- SETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		 * remet la date de last_check_nourriture a Y-m-d H:i:s | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | 		 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 		private function setUpdateLastCheckNourriture() { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 			$dbc = App::getDb(); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 			 | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 			$dbc->update("last_check_nourriture", date("Y-m-d H:i:s"))->from("_bataille_last_connexion")->where("ID_identite", "=", Bataille::getIdIdentite())->set(); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		 * @param $nb_heure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		 * @param $nb_unite | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		 * fonction qui calcule la nourriture consomee en 1h | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		 * si elle est inférieur à 0 on tue un nombre d'unite defini en fonction de combien le nombre de | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		 * nourriture est en dessous de 0 (valeur divisée par 100) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		 * puis on retire les ressources du grenier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		private function setNourritureConsomee($nb_heure, $nb_unite) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			$nourriture_base = Bataille::getRessource()->getNourriture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			$nourriture_consommee = ($nb_unite*$this->getConsommationNourritureUnite())*$nb_heure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			$nourriture_retirer = $nourriture_base - $nourriture_consommee; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			if ($nourriture_retirer < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 				$unite_tuer = abs(round($nourriture_retirer/100)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 				Bataille::getUnite()->setTuerUnites($unite_tuer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $nourriture_consommee, "-"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  | 		//-------------------------- END SETTER ----------------------------------------------------------------------------// | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | 	} | 
            
                        
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: