Passed
Push — master ( 9c6704...e16eca )
by Anthony
02:58
created

Unite::getAllUnites()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 12
nc 12
nop 2
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
15
		
16
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
17
		public function __construct() {
18
			$this->coef_unite = Bataille::getParam("coef_niveau_unite");
19
		}
20
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
21
		
22
		
23
		//-------------------------- GETTER ----------------------------------------------------------------------------//
24
25
		/**
26
		 * @param $unite
27
		 * @param $niveau
28
		 * @param $type
29
		 * @return array
30
		 * récupère les caractéristiques de l'unité en fonction de son niveau
31
		 */
32
		public function getCaracteristiqueUnite($unite, $niveau, $type) {
33
			$dbc1 = Bataille::getDb();
34
35
			$query = $dbc1->select()
36
				->from("unites")
37
				->where("nom", "=", $unite, "AND")
38
				->where("type", "=", $type, "")
39
				->get();
40
41
			if ((is_array($query)) && (count($query) == 1)) {
42
				foreach ($query as $obj) {
43
					$base_carac = unserialize($obj->caracteristique);
44
					$ressource = unserialize($obj->pour_recruter);
45
					$temps_recrutement = DateHeure::Secondeenheure(round($obj->temps_recrutement-($obj->temps_recrutement*Bataille::getBatiment()->getNiveauBatiment("caserne")/100)));
46
				}
47
48
				$coef = $this->coef_unite*$niveau;
49
50
				if ($niveau == 1) $coef = 1;
51
52
				return [
53
					"caracteristique" => [
54
						"attaque" => round($base_carac["attaque"]*$coef),
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...
55
						"defense" => round($base_carac["defense"]*$coef),
56
						"resistance" => round($base_carac["resistance"]*$coef),
57
						"vitesse" => $base_carac["vitesse"]
58
					],
59
					"cout_recruter" => [
60
						"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...
61
						"electricite" => $ressource["electricite"],
62
						"fer" => $ressource["fer"],
63
						"fuel" => $ressource["fuel"],
64
					],
65
					"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...
66
				];
67
			}
68
			else {
69
				return [];
70
			}
71
		}
72
73
		/**
74
		 * @return array
75
		 * fonction qui renvoit tous les types d'unités qu'il est possible de recruter
76
		 */
77
		private function getAllType() {
78
			return explode(",", Bataille::getParam("type_unite"));
79
		}
80
81
		/**
82
		 * @param $type
83
		 * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction
84
		 * du type (batiment sur lequel on a cliqué)
85
		 */
86
		public function getUnitePossibleRecruter($type) {
87
			//on recup toutes les unites deja recherchée donc que l'on peut faire
88
			$unites = Bataille::getCentreRecherche()->getAllRechercheType($type);
89
90
			//recupérer les caractéristiques de l'unité en question
91
			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...
92
				$unites[$i] += $this->getCaracteristiqueUnite($unites[$i]["recherche"], $unites[$i]["niveau"], $type);
93
				$unites[$i] += ["type" => $type];
94
			}
95
96
			Bataille::setValues(["unites" => $unites]);
97
		}
98
99
		/**
100
		 * fonction qui renvoi les unité  en cours de recrutement
101
		 */
102
		public function getRecrutement() {
103
			$dbc = App::getDb();
104
105
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_base", "=", Bataille::getIdBase())->get();
106
107
			if ((is_array($query)) && (count($query) > 0)) {
108
				$today = Bataille::getToday();
109
110
				foreach ($query as $obj) {
111
					if ($obj->date_fin-$today <= 0) {
112
						$this->setTerminerRecrutement($obj->ID_recrutement);
113
					}
114
					else {
115
						$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...
116
							"nom" => $obj->nom,
117
							"type" => $obj->type,
118
							"nombre" => $obj->nombre,
119
							"date_fin_recrutement" => $obj->date_fin-$today,
120
							"id_recrutement" => $obj->ID_recrutement
121
						];
122
					}
123
				}
124
125
				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...
126
			}
127
		}
128
		
129
		/**
130
		 * @param null $id_base
131
		 * @param null $id_groupe
132
		 * fonction qui récupère toutes les unités qui sont dans la base
133
		 */
134
		public function getAllUnites($id_base = null, $id_groupe = null) {
135
136
			if ($id_base == null) $id_base = Bataille::getIdBase();
137
138
			$types = $this->getAllType();
139
			$count_type = count($types);
140
			$unites = [];
141
142
			for ($i=0 ; $i<$count_type ; $i++) {
143
				$type_unite = $this->getAllUniteType($types[$i], $id_base, $id_groupe);
144
145
				$unites = array_merge($unites, $type_unite);
146
			}
147
			
148
			if (count($unites) > 0) {
149
				if ($id_groupe == null) {
150
					Bataille::setValues(["unites" => $unites]);
151
				}
152
				
153
				return $unites;
154
			}
155
		}
156
		
157
		/**
158
		 * @param $type
159
		 * @param $id_base
160
		 * @param null $id_groupe
161
		 * @return array
162
		 * fonction qui récupère toutes les unités en fonction d'un type précis
163
		 */
164
		private function getAllUniteType($type, $id_base, $id_groupe = null) {
165
			$dbc = App::getDb();
166
			
167
			$groupe = "(ID_groupe IS NULL OR ID_groupe = 0)";
168
			
169
			if ($id_groupe != null) {
170
				$groupe = "ID_groupe = ".$id_groupe;
171
			}
172
173
			$query = $dbc->select("nom")->from("_bataille_unite")
174
				->where("type", "=", $type, "AND")
175
				->where("ID_base", "=", $id_base, "AND")
176
				->where($groupe, "", "", "AND", true)
177
				->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
178
				->orderBy("nom")
179
				->get();
180
181
			if ((is_array($query)) && (count($query) > 0)) {
182
				$count = 1;
183
				$nom = "";
184
				foreach ($query as $obj) {
185
					if ($nom != $obj->nom) {
186
						$count = 1;
187
					}
188
					$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...
189
						"nom" => $obj->nom,
190
						"nombre" => $count++
191
					];
192
					$nom = $obj->nom;
193
				}
194
195
				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...
196
			}
197
			
198
			return [];
199
		}
200
		
201
		/**
202
		 * @param $type
203
		 * @param $nom
204
		 * @return int
205
		 * renvoi le nombre d'unite en fonction d'un type et d'un nom qui ne sont ni dans un groupe ni
206
		 * en mission
207
		 */
208
		protected function getNombreUniteNom($type, $nom) {
209
			$dbc = App::getDb();
210
			
211
			$query = $dbc->select("nom")->from("_bataille_unite")
212
				->where("type", "=", $type, "AND")
213
				->where("nom", "=", $nom, "AND")
214
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
215
				->where("(ID_groupe IS NULL OR ID_groupe = 0)", "", "", "AND", true)
216
				->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
217
				->orderBy("nom")
218
				->get();
219
			
220
			return count($query);
221
		}
222
		
223
		/**
224
		 * @return int
225
		 * fonction qui renvoi le nombre d'unité vivante dans la base qui consomme de la nourriture
226
		 */
227 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...
228
			$dbc = App::getDb();
229
			
230
			$query = $dbc->select("ID_unite")->from("_bataille_unite")
231
				->where("type", "=", "infanterie", "AND")
232
				->where("ID_base", "=", Bataille::getIdBase())
233
				->get();
234
			
235
			return count($query);
236
		}
237
		
238
		/**
239
		 * @param $id_mission
240
		 * @return int
241
		 * fonction qui renvoi le nombre d'unités envoyées sur une mission en particulier
242
		 */
243 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...
244
			$dbc = App::getDb();
245
			
246
			$query = $dbc->select("ID_unite")->from("_bataille_unite")
247
				->where("ID_mission", "=", $id_mission, "AND")
248
				->where("ID_base", "=", Bataille::getIdBase())
249
				->get();
250
			
251
			return count($query);
252
		}
253
		
254
		/**
255
		 * @param $type
256
		 * @param $nom
257
		 * récupération du temmp de recrutement + les ressources nécéssaires
258
		 */
259
		private function getInfosRecrutementUnite($type, $nom) {
260
			$dbc1 = Bataille::getDb();
261
			
262
			$query = $dbc1->select("temps_recrutement")
263
				->select("pour_recruter")
264
				->from("unites")
265
				->where("nom", "=", $nom, "AND")
266
				->where("type", "=", $type, "")
267
				->get();
268
			
269
			if ((is_array($query)) && (count($query) == 1)) {
270
				foreach ($query as $obj) {
271
					$this->pour_recruter = unserialize($obj->pour_recruter);
272
					$this->temps_recrutement = round($obj->temps_recrutement-($obj->temps_recrutement*Bataille::getBatiment()->getNiveauBatiment("caserne")/100));
273
				}
274
			}
275
		}
276
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
277
		
278
		
279
		//-------------------------- SETTER ----------------------------------------------------------------------------//
280
		/**
281
		 * @param $nom -> nom de l'unité à recruter
282
		 * @param $type -> type de l'unité à recruter
283
		 * @param $nombre -> nombre d'unité à recruter
284
		 * fonction qui permet d'initialiser le début du recrutement d'unités
285
		 */
286
		public function setCommencerRecruter($nom, $type, $nombre) {
287
			$dbc = App::getDb();
288
289
			$this->getInfosRecrutementUnite($type, $nom);
290
291
			//on test si on a assez de ressource pour recruter les unites
292
			//on test si assez de ressources dans la base
293
			$retirer_eau = intval($this->pour_recruter["eau"])*$nombre;
294
			$retirer_electricite = intval($this->pour_recruter["electricite"])*$nombre;
295
			$retirer_fer = intval($this->pour_recruter["fer"])*$nombre;
296
			$retirer_fuel = intval($this->pour_recruter["fuel"])*$nombre;
297
			$eau = Bataille::getTestAssezRessourceBase("eau", $retirer_eau);
298
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $retirer_electricite);
299
			$fer = Bataille::getTestAssezRessourceBase("fer", $retirer_fer);
300
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $retirer_fuel);
301
302
303
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
304
				FlashMessage::setFlash("Pas assez de ressources pour recruter autant d'unités");
305
				return false;
306
			}
307
			else {
308
				//on retire les ressources
309
				Bataille::getRessource()->setUpdateRessource($retirer_eau, $retirer_electricite, $retirer_fer, $retirer_fuel, 0, "-");
310
311
				$date_fin = Bataille::getToday()+($this->temps_recrutement *$nombre);
312
313
				$dbc->insert("nom", $nom)
314
					->insert("type", $type)
315
					->insert("nombre", $nombre)
316
					->insert("date_fin", $date_fin)
317
					->insert("ID_base", Bataille::getIdBase())
318
					->into("_bataille_recrutement")
319
					->set();
320
321
				return true;
322
			}
323
		}
324
325
		/**
326
		 * @param $id_recrutement
327
		 * fonction appellée dans celle qui récupère les recrutement uniquement quand celui ci est finit
328
		 * fonction qui sert à terminer un rcrutement et ajouter les unités dans la base
329
		 */
330
		private function setTerminerRecrutement($id_recrutement) {
331
			$dbc = App::getDb();
332
333
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->get();
334
335
			if ((is_array($query)) && (count($query) == 1)) {
336
				foreach ($query as $obj) {
337
					$nombre = $obj->nombre;
338
					$nom = $obj->nom;
339
					$type = $obj->type;
340
				}
341
342
				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...
343
					$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...
344
						->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...
345
						->insert("ID_base", Bataille::getIdBase())
346
						->into("_bataille_unite")
347
						->set();
348
				}
349
350
				$dbc->delete()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->del();
351
			}
352
		}
353
		
354
		/**
355
		 * @param $nombre_unite
356
		 * @param $nom_unite
357
		 * @param $type_unite
358
		 * @param $id_mission
359
		 * @return bool
360
		 * permet de lancer des unites en expédition en ajoutant à chaque unité un id_mission
361
		 */
362
		public function setCommencerExpedition($nombre_unite, $nom_unite, $type_unite, $id_mission) {
363
			$dbc = App::getDb();
364
			
365
			$nombre_unite_base = $this->getNombreUniteNom($type_unite, $nom_unite);
366
			
367
			if ($nombre_unite > $nombre_unite_base) {
368
				FlashMessage::setFlash("Pas assez d'unités ".$nom_unite." disponibles dans la base pour partir en mission");
369
				return false;
370
			}
371
			
372
			$dbc->update("ID_mission", $id_mission)
373
				->from("_bataille_unite")
374
				->where("type", "=", $type_unite, "AND")
375
				->where("nom", "=", $nom_unite, "AND")
376
				->where("ID_base", "=", Bataille::getIdBase())
377
				->limit($nombre_unite, "no")
378
				->set();
379
			
380
			return true;
381
		}
382
		
383
		/**
384
		 * @param $id_mission
385
		 * @param $pourcentage_perte
386
		 * @return int
387
		 * fonction qui termine une expdedition au niveau des troupes, cette fonction s'occupe d'en
388
		 * supprimer de la bdd en fonction du nombre de troupe envoyé et du cpourcentage de perte
389
		 */
390
		public function setTerminerExpedition($id_mission, $pourcentage_perte) {
391
			$dbc = App::getDb();
392
			$perte = rand(0, $pourcentage_perte);
393
			
394
			$query = $dbc->select()->from("_bataille_unite")->where("ID_mission", "=", $id_mission, "AND")
395
				->where("ID_base", "=", Bataille::getIdBase())
396
				->get();
397
			
398
			//test si il y aura des unités à tuer
399
			$nombre_unite = count($query);
400
			$unite_tuees = 0;
401
			if ((is_array($query)) && ($nombre_unite > 0)) {
402
				$unite_tuees = round($nombre_unite*$perte/100);
403
			}
404
			
405
			//si oui on en delete aléatoirement
406
			if ($unite_tuees > 0) {
407
				$dbc->delete()->from("_bataille_unite")->where("ID_mission", "=", $id_mission, "AND")
408
					->where("ID_base", "=", Bataille::getIdBase())
409
					->orderBy("RAND() ")
410
					->limit($unite_tuees)
411
					->del();
412
			}
413
			
414
			$dbc->update("ID_mission", 0)
415
				->from("_bataille_unite")
416
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
417
				->where("ID_mission", "=", $id_mission, "", true)
418
				->set();
419
			
420
			//renvoi le nombre d'unites qui ont réussi àrentrer à la base
421
			return $nombre_unite-$unite_tuees;
422
		}
423
		
424
		/**
425
		 * @param $nombre
426
		 * fonction qui permet de tuer des unites
427
		 */
428
		public function setTuerUnites($nombre) {
429
			$dbc = App::getDb();
430
			
431
			if ($nombre > 0) {
432
				$dbc->delete()->from("_bataille_unite")
433
					->where("ID_base", "=", Bataille::getIdBase(), "AND")
434
					->where("type", "=", "infanterie", "AND")
435
					->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
436
					->orderBy("RAND() ")
437
					->limit($nombre)
438
					->del();
439
			}
440
		}
441
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
442
	}