Passed
Push — master ( b9668a...d3086a )
by Anthony
04:34
created

Unite::getAmeliorationUnite()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 4
nop 3
1
<?php
2
	
3
	namespace modules\bataille\app\controller;
4
	
5
	
6
	use core\App;
7
	use core\functions\DateHeure;
8
	use core\HTML\flashmessage\FlashMessage;
9
10
	class Unite {
11
		private $coef_unite;
12
		private $pour_recruter;
13
		private $temps_recrutement;
14
		private $ameliorations;
15
16
		
17
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
18
		public function __construct() {
19
			$this->coef_unite = Bataille::getParam("coef_niveau_unite");
20
		}
21
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
22
		
23
		
24
		//-------------------------- GETTER ----------------------------------------------------------------------------//
25
		/**
26
		 * @param $caracteristique
27
		 * @param $force
28
		 * @param $niveau
29
		 * @return float
30
		 * fonction qui permet de renvoyer la puissance d'une unité dans un élément spécifique
31
		 */
32
		private function getAmeliorationUnite($caracteristique, $force, $niveau) {
33
			$coef = $this->coef_unite*$niveau;
34
			if ($niveau == 1) $coef = 1;
35
			
36
			if ((in_array($caracteristique, $this->ameliorations)) || ($this->ameliorations == "all")) {
37
				return round($force*$coef);
38
			}
39
			
40
			return $force;
41
		}
42
		
43
		
44
		/**
45
		 * @param $unite
46
		 * @param $niveau
47
		 * @param $type
48
		 * @return array
49
		 * récupère les caractéristiques de l'unité en fonction de son niveau
50
		 */
51
		public function getCaracteristiqueUnite($unite, $niveau, $type) {
52
			$dbc1 = Bataille::getDb();
53
54
			$query = $dbc1->select()
55
				->from("unites")
56
				->where("nom", "=", $unite, "AND")
57
				->where("type", "=", $type, "")
58
				->get();
59
60
			if ((is_array($query)) && (count($query) == 1)) {
61
				$this->ameliorations = "all";
62
				foreach ($query as $obj) {
63
					$base_carac = unserialize($obj->caracteristique);
64
					$ressource = unserialize($obj->pour_recruter);
65
					$temps_recrutement = DateHeure::Secondeenheure(round($obj->temps_recrutement-($obj->temps_recrutement*Bataille::getBatiment()->getNiveauBatiment("caserne")/100)));
66
					
67
					if ($obj->amelioration != "") {
68
						$this->ameliorations = explode(",", $obj->amelioration);
69
					}
70
				}
71
72
				return [
73
					"caracteristique" => [
74
						"attaque" => $this->getAmeliorationUnite("attaque", $base_carac["attaque"], $niveau),
0 ignored issues
show
Bug introduced by
The variable $base_carac does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
75
						"defense" => $this->getAmeliorationUnite("defense", $base_carac["defense"], $niveau),
76
						"resistance" => $this->getAmeliorationUnite("resistance", $base_carac["resistance"], $niveau),
77
						"vitesse" => $base_carac["vitesse"]
78
					],
79
					"cout_recruter" => [
80
						"eau" => $ressource["eau"],
0 ignored issues
show
Bug introduced by
The variable $ressource does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
						"electricite" => $ressource["electricite"],
82
						"fer" => $ressource["fer"],
83
						"fuel" => $ressource["fuel"],
84
					],
85
					"amelioration" => $this->ameliorations,
86
					"temps_recrutement" => $temps_recrutement
0 ignored issues
show
Bug introduced by
The variable $temps_recrutement does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
87
				];
88
			}
89
			else {
90
				return [];
91
			}
92
		}
93
94
		/**
95
		 * @return array
96
		 * fonction qui renvoit tous les types d'unités qu'il est possible de recruter
97
		 */
98
		private function getAllType() {
99
			return explode(",", Bataille::getParam("type_unite"));
100
		}
101
102
		/**
103
		 * @param $type
104
		 * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction
105
		 * du type (batiment sur lequel on a cliqué)
106
		 */
107
		public function getUnitePossibleRecruter($type) {
108
			//on recup toutes les unites deja recherchée donc que l'on peut faire
109
			$unites = Bataille::getCentreRecherche()->getAllRechercheType($type);
110
111
			//recupérer les caractéristiques de l'unité en question
112
			for ($i=0 ; $i<count($unites) ; $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
113
				$unites[$i] += $this->getCaracteristiqueUnite($unites[$i]["recherche"], $unites[$i]["niveau"], $type);
114
				$unites[$i] += ["type" => $type];
115
			}
116
117
			Bataille::setValues(["unites" => $unites]);
118
		}
119
120
		/**
121
		 * fonction qui renvoi les unité  en cours de recrutement
122
		 */
123
		public function getRecrutement() {
124
			$dbc = App::getDb();
125
126
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_base", "=", Bataille::getIdBase())->get();
127
128
			if ((is_array($query)) && (count($query) > 0)) {
129
				$today = Bataille::getToday();
130
131
				foreach ($query as $obj) {
132
					if ($obj->date_fin-$today <= 0) {
133
						$this->setTerminerRecrutement($obj->ID_recrutement);
134
					}
135
					else {
136
						$recrutement[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recrutement was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recrutement = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
137
							"nom" => $obj->nom,
138
							"type" => $obj->type,
139
							"nombre" => $obj->nombre,
140
							"date_fin_recrutement" => $obj->date_fin-$today,
141
							"id_recrutement" => $obj->ID_recrutement
142
						];
143
					}
144
				}
145
146
				Bataille::setValues(["recrutement" => $recrutement]);
0 ignored issues
show
Bug introduced by
The variable $recrutement does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
147
			}
148
		}
149
		
150
		/**
151
		 * @param null $id_base
152
		 * @param null $id_groupe
153
		 * fonction qui récupère toutes les unités qui sont dans la base
154
		 */
155
		public function getAllUnites($id_base = null, $id_groupe = null) {
156
157
			if ($id_base == null) $id_base = Bataille::getIdBase();
158
159
			$types = $this->getAllType();
160
			$count_type = count($types);
161
			$unites = [];
162
163
			for ($i=0 ; $i<$count_type ; $i++) {
164
				$type_unite = $this->getAllUniteType($types[$i], $id_base, $id_groupe);
165
166
				$unites = array_merge($unites, $type_unite);
167
			}
168
			
169
			if (count($unites) > 0) {
170
				if ($id_groupe == null) {
171
					Bataille::setValues(["unites" => $unites]);
172
				}
173
				
174
				return $unites;
175
			}
176
			
177
			return 0;
178
		}
179
		
180
		/**
181
		 * @param $type
182
		 * @param $id_base
183
		 * @param null $id_groupe
184
		 * @return array
185
		 * fonction qui récupère toutes les unités en fonction d'un type précis
186
		 */
187
		private function getAllUniteType($type, $id_base, $id_groupe = null) {
188
			$dbc = App::getDb();
189
			
190
			$groupe = "(ID_groupe IS NULL OR ID_groupe = 0)";
191
			
192
			if ($id_groupe != null) {
193
				$groupe = "ID_groupe = ".$id_groupe;
194
			}
195
196
			$query = $dbc->select("nom")->from("_bataille_unite")
197
				->where("type", "=", $type, "AND")
198
				->where("ID_base", "=", $id_base, "AND")
199
				->where($groupe, "", "", "AND", true)
200
				->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
201
				->orderBy("nom")
202
				->get();
203
204
			if ((is_array($query)) && (count($query) > 0)) {
205
				$count = 1;
206
				$nom = "";
207
				foreach ($query as $obj) {
208
					if ($nom != $obj->nom) {
209
						$count = 1;
210
					}
211
					$unite[] = $unites[$type][$obj->nom] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$unite was never initialized. Although not strictly required by PHP, it is generally a good practice to add $unite = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
Coding Style Comprehensibility introduced by
$unites was never initialized. Although not strictly required by PHP, it is generally a good practice to add $unites = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
212
						"nom" => $obj->nom,
213
						"nombre" => $count++
214
					];
215
					$nom = $obj->nom;
216
				}
217
218
				return $unites;
0 ignored issues
show
Bug introduced by
The variable $unites does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
219
			}
220
			
221
			return [];
222
		}
223
		
224
		/**
225
		 * @param $type
226
		 * @param $nom
227
		 * @return int
228
		 * renvoi le nombre d'unite en fonction d'un type et d'un nom qui ne sont ni dans un groupe ni
229
		 * en mission
230
		 */
231
		protected function getNombreUniteNom($type, $nom) {
232
			$dbc = App::getDb();
233
			
234
			$query = $dbc->select("nom")->from("_bataille_unite")
235
				->where("type", "=", $type, "AND")
236
				->where("nom", "=", $nom, "AND")
237
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
238
				->where("(ID_groupe IS NULL OR ID_groupe = 0)", "", "", "AND", true)
239
				->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
240
				->orderBy("nom")
241
				->get();
242
			
243
			return count($query);
244
		}
245
		
246
		/**
247
		 * @return int
248
		 * fonction qui renvoi le nombre d'unité vivante dans la base qui consomme de la nourriture
249
		 */
250 View Code Duplication
		public function getNombreUniteHumaine() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
			$dbc = App::getDb();
252
			
253
			$query = $dbc->select("ID_unite")->from("_bataille_unite")
254
				->where("type", "=", "infanterie", "AND")
255
				->where("ID_base", "=", Bataille::getIdBase())
256
				->get();
257
			
258
			return count($query);
259
		}
260
		
261
		/**
262
		 * @param $id_mission
263
		 * @return int
264
		 * fonction qui renvoi le nombre d'unités envoyées sur une mission en particulier
265
		 */
266 View Code Duplication
		public function getUnitesMission($id_mission) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
			$dbc = App::getDb();
268
			
269
			$query = $dbc->select("ID_unite")->from("_bataille_unite")
270
				->where("ID_mission", "=", $id_mission, "AND")
271
				->where("ID_base", "=", Bataille::getIdBase())
272
				->get();
273
			
274
			return count($query);
275
		}
276
		
277
		/**
278
		 * @param $type
279
		 * @param $nom
280
		 * récupération du temmp de recrutement + les ressources nécéssaires
281
		 */
282
		private function getInfosRecrutementUnite($type, $nom) {
283
			$dbc1 = Bataille::getDb();
284
			
285
			$query = $dbc1->select("temps_recrutement")
286
				->select("pour_recruter")
287
				->from("unites")
288
				->where("nom", "=", $nom, "AND")
289
				->where("type", "=", $type, "")
290
				->get();
291
			
292
			if ((is_array($query)) && (count($query) == 1)) {
293
				foreach ($query as $obj) {
294
					$this->pour_recruter = unserialize($obj->pour_recruter);
295
					$this->temps_recrutement = round($obj->temps_recrutement-($obj->temps_recrutement*Bataille::getBatiment()->getNiveauBatiment("caserne")/100));
296
				}
297
			}
298
		}
299
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
300
		
301
		
302
		//-------------------------- SETTER ----------------------------------------------------------------------------//
303
		/**
304
		 * @param $nom -> nom de l'unité à recruter
305
		 * @param $type -> type de l'unité à recruter
306
		 * @param $nombre -> nombre d'unité à recruter
307
		 * fonction qui permet d'initialiser le début du recrutement d'unités
308
		 */
309
		public function setCommencerRecruter($nom, $type, $nombre) {
310
			$dbc = App::getDb();
311
312
			$this->getInfosRecrutementUnite($type, $nom);
313
314
			//on test si on a assez de ressource pour recruter les unites
315
			//on test si assez de ressources dans la base
316
			$retirer_eau = intval($this->pour_recruter["eau"])*$nombre;
317
			$retirer_electricite = intval($this->pour_recruter["electricite"])*$nombre;
318
			$retirer_fer = intval($this->pour_recruter["fer"])*$nombre;
319
			$retirer_fuel = intval($this->pour_recruter["fuel"])*$nombre;
320
			$eau = Bataille::getTestAssezRessourceBase("eau", $retirer_eau);
321
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $retirer_electricite);
322
			$fer = Bataille::getTestAssezRessourceBase("fer", $retirer_fer);
323
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $retirer_fuel);
324
325
326
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
327
				FlashMessage::setFlash("Pas assez de ressources pour recruter autant d'unités");
328
				return false;
329
			}
330
			else {
331
				//on retire les ressources
332
				Bataille::getRessource()->setUpdateRessource($retirer_eau, $retirer_electricite, $retirer_fer, $retirer_fuel, 0, "-");
333
334
				$date_fin = Bataille::getToday()+($this->temps_recrutement *$nombre);
335
336
				$dbc->insert("nom", $nom)
337
					->insert("type", $type)
338
					->insert("nombre", $nombre)
339
					->insert("date_fin", $date_fin)
340
					->insert("ID_base", Bataille::getIdBase())
341
					->into("_bataille_recrutement")
342
					->set();
343
344
				return true;
345
			}
346
		}
347
348
		/**
349
		 * @param $id_recrutement
350
		 * fonction appellée dans celle qui récupère les recrutement uniquement quand celui ci est finit
351
		 * fonction qui sert à terminer un rcrutement et ajouter les unités dans la base
352
		 */
353
		private function setTerminerRecrutement($id_recrutement) {
354
			$dbc = App::getDb();
355
356
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->get();
357
358
			if ((is_array($query)) && (count($query) == 1)) {
359
				foreach ($query as $obj) {
360
					$nombre = $obj->nombre;
361
					$nom = $obj->nom;
362
					$type = $obj->type;
363
				}
364
365
				for ($i=0 ; $i<$nombre ; $i++) {
0 ignored issues
show
Bug introduced by
The variable $nombre does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
366
					$dbc->insert("nom", $nom)
0 ignored issues
show
Bug introduced by
The variable $nom does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
367
						->insert("type", $type)
0 ignored issues
show
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
368
						->insert("ID_base", Bataille::getIdBase())
369
						->into("_bataille_unite")
370
						->set();
371
				}
372
373
				$dbc->delete()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->del();
374
			}
375
		}
376
		
377
		/**
378
		 * @param $nombre_unite
379
		 * @param $nom_unite
380
		 * @param $type_unite
381
		 * @param $id_mission
382
		 * @return bool
383
		 * permet de lancer des unites en expédition en ajoutant à chaque unité un id_mission
384
		 */
385
		public function setCommencerExpedition($nombre_unite, $nom_unite, $type_unite, $id_mission) {
386
			$dbc = App::getDb();
387
			
388
			$nombre_unite_base = $this->getNombreUniteNom($type_unite, $nom_unite);
389
			
390
			if ($nombre_unite > $nombre_unite_base) {
391
				FlashMessage::setFlash("Pas assez d'unités ".$nom_unite." disponibles dans la base pour partir en mission");
392
				return false;
393
			}
394
			
395
			$dbc->update("ID_mission", $id_mission)
396
				->from("_bataille_unite")
397
				->where("type", "=", $type_unite, "AND")
398
				->where("nom", "=", $nom_unite, "AND")
399
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
400
				->where("(ID_groupe IS NULL OR ID_groupe = 0)", "", "", "AND", true)
401
				->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
402
				->limit($nombre_unite, "no")
403
				->set();
404
			
405
			return true;
406
		}
407
		
408
		/**
409
		 * @param $id_mission
410
		 * @param $pourcentage_perte
411
		 * @return int
412
		 * fonction qui termine une expdedition au niveau des troupes, cette fonction s'occupe d'en
413
		 * supprimer de la bdd en fonction du nombre de troupe envoyé et du cpourcentage de perte
414
		 */
415
		public function setTerminerExpedition($id_mission, $pourcentage_perte) {
416
			$dbc = App::getDb();
417
			$perte = rand(0, $pourcentage_perte);
418
			
419
			$query = $dbc->select()->from("_bataille_unite")->where("ID_mission", "=", $id_mission, "AND")
420
				->where("ID_base", "=", Bataille::getIdBase())
421
				->get();
422
			
423
			//test si il y aura des unités à tuer
424
			$nombre_unite = count($query);
425
			$unite_tuees = 0;
426
			if ((is_array($query)) && ($nombre_unite > 0)) {
427
				$unite_tuees = round($nombre_unite*$perte/100);
428
			}
429
			
430
			//si oui on en delete aléatoirement
431
			if ($unite_tuees > 0) {
432
				$dbc->delete()->from("_bataille_unite")->where("ID_mission", "=", $id_mission, "AND")
433
					->where("ID_base", "=", Bataille::getIdBase())
434
					->orderBy("RAND() ")
435
					->limit($unite_tuees)
436
					->del();
437
			}
438
			
439
			$dbc->update("ID_mission", 0)
440
				->from("_bataille_unite")
441
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
442
				->where("ID_mission", "=", $id_mission, "", true)
443
				->set();
444
			
445
			//renvoi le nombre d'unites qui ont réussi àrentrer à la base
446
			return $nombre_unite-$unite_tuees;
447
		}
448
		
449
		/**
450
		 * @param $nombre
451
		 * fonction qui permet de tuer des unites
452
		 */
453
		public function setTuerUnites($nombre) {
454
			$dbc = App::getDb();
455
			
456
			if ($nombre > 0) {
457
				$dbc->delete()->from("_bataille_unite")
458
					->where("ID_base", "=", Bataille::getIdBase(), "AND")
459
					->where("type", "=", "infanterie", "AND")
460
					->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
461
					->orderBy("RAND() ")
462
					->limit($nombre)
463
					->del();
464
			}
465
		}
466
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
467
	}