Passed
Push — master ( 5dce17...e831d3 )
by Anthony
02:28
created

Unite::getUnitesMission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

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