Passed
Push — master ( ab8373...e7b2e8 )
by Anthony
02:31
created

Unite::getNombreUniteHumaine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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
		public function getNombreUniteHumaine() {
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
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
223
		
224
		
225
		//-------------------------- SETTER ----------------------------------------------------------------------------//
226
		/**
227
		 * @param $nom -> nom de l'unité à recruter
228
		 * @param $type -> type de l'unité à recruter
229
		 * @param $nombre -> nombre d'unité à recruter
230
		 * fonction qui permet d'initialiser le début du recrutement d'unités
231
		 */
232
		public function setCommencerRecruter($nom, $type, $nombre) {
233
			$dbc1 = Bataille::getDb();
234
			$dbc = App::getDb();
235
236
			$query = $dbc1->select("temps_recrutement")
237
				->select("pour_recruter")
238
				->from("unites")
239
				->where("nom", "=", $nom, "AND")
240
				->where("type", "=", $type, "")
241
				->get();
242
243
			if ((is_array($query)) && (count($query) == 1)) {
244
				foreach ($query as $obj) {
245
					$pour_recruter = unserialize($obj->pour_recruter);
246
					$temps_recrutement = round($obj->temps_recrutement-($obj->temps_recrutement*Bataille::getBatiment()->getNiveauBatiment("caserne")/100));
247
				}
248
			}
249
250
			//on test si on a assez de ressource pour recruter les unites
251
			//on test si assez de ressources dans la base
252
			$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...
253
			$retirer_electricite = intval($pour_recruter["electricite"])*$nombre;
254
			$retirer_fer = intval($pour_recruter["fer"])*$nombre;
255
			$retirer_fuel = intval($pour_recruter["fuel"])*$nombre;
256
			$eau = Bataille::getTestAssezRessourceBase("eau", $retirer_eau);
257
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $retirer_electricite);
258
			$fer = Bataille::getTestAssezRessourceBase("fer", $retirer_fer);
259
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $retirer_fuel);
260
261
262
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
263
				FlashMessage::setFlash("Pas assez de ressources pour recruter autant d'unités");
264
				return false;
265
			}
266
			else {
267
				//on retire les ressources
268
				Bataille::getRessource()->setUpdateRessource($retirer_eau, $retirer_electricite, $retirer_fer, $retirer_fuel, 0, "-");
269
270
				$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...
271
272
				$dbc->insert("nom", $nom)
273
					->insert("type", $type)
274
					->insert("nombre", $nombre)
275
					->insert("date_fin", $date_fin)
276
					->insert("ID_base", Bataille::getIdBase())
277
					->into("_bataille_recrutement")
278
					->set();
279
280
				return true;
281
			}
282
		}
283
284
		/**
285
		 * @param $id_recrutement
286
		 * fonction appellée dans celle qui récupère les recrutement uniquement quand celui ci est finit
287
		 * fonction qui sert à terminer un rcrutement et ajouter les unités dans la base
288
		 */
289
		private function setTerminerRecrutement($id_recrutement) {
290
			$dbc = App::getDb();
291
292
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->get();
293
294
			if ((is_array($query)) && (count($query) == 1)) {
295
				foreach ($query as $obj) {
296
					$nombre = $obj->nombre;
297
					$nom = $obj->nom;
298
					$type = $obj->type;
299
				}
300
301
				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...
302
					$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...
303
						->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...
304
						->insert("ID_base", Bataille::getIdBase())
305
						->into("_bataille_unite")
306
						->set();
307
				}
308
309
				$dbc->delete()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->del();
310
			}
311
		}
312
		
313
		/**
314
		 * @param $nombre_unite
315
		 * @param $nom_unite
316
		 * @param $type_unite
317
		 * @param $id_mission
318
		 * @return bool
319
		 * permet de lancer des unites en expédition en ajoutant à chaque unité un id_mission
320
		 */
321
		public function setCommencerExpedition($nombre_unite, $nom_unite, $type_unite, $id_mission) {
322
			$dbc = App::getDb();
323
			
324
			$nombre_unite_base = $this->getNombreUniteNom($type_unite, $nom_unite);
325
			
326
			if ($nombre_unite > $nombre_unite_base) {
327
				FlashMessage::setFlash("Pas assez d'unités ".$nom_unite." disponibles dans la base pour partir en mission");
328
				return false;
329
			}
330
			
331
			$dbc->update("ID_mission", $id_mission)
332
				->from("_bataille_unite")
333
				->where("type", "=", $type_unite, "AND")
334
				->where("nom", "=", $nom_unite, "AND")
335
				->where("ID_base", "=", Bataille::getIdBase())
336
				->limit($nombre_unite, "no")
337
				->set();
338
			
339
			return true;
340
		}
341
		
342
		/**
343
		 * @param $id_mission
344
		 * @param $pourcentage_perte
345
		 * @return int
346
		 * fonction qui termine une expdedition au niveau des troupes, cette fonction s'occupe d'en
347
		 * supprimer de la bdd en fonction du nombre de troupe envoyé et du cpourcentage de perte
348
		 */
349
		public function setTerminerExpedition($id_mission, $pourcentage_perte) {
350
			$dbc = App::getDb();
351
			$perte = rand(0, $pourcentage_perte);
352
			
353
			$query = $dbc->select()->from("_bataille_unite")->where("ID_mission", "=", $id_mission, "AND")
354
				->where("ID_base", "=", Bataille::getIdBase())
355
				->get();
356
			
357
			//test si il y aura des unités à tuer
358
			$nombre_unite = count($query);
359
			$unite_tuees = 0;
360
			if ((is_array($query)) && ($nombre_unite > 0)) {
361
				$unite_tuees = round($nombre_unite*$perte/100);
362
			}
363
			
364
			//si oui on en delete aléatoirement
365
			if ($unite_tuees > 0) {
366
				$dbc->delete()->from("_bataille_unite")->where("ID_mission", "=", $id_mission, "AND")
367
					->where("ID_base", "=", Bataille::getIdBase())
368
					->orderBy("RAND() ")
369
					->limit($unite_tuees)
370
					->del();
371
			}
372
			
373
			$dbc->update("ID_mission", 0)
374
				->from("_bataille_unite")
375
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
376
				->where("ID_mission", "=", $id_mission, "", true)
377
				->set();
378
			
379
			//renvoi le nombre d'unites qui ont réussi àrentrer à la base
380
			return $nombre_unite-$unite_tuees;
381
		}
382
		
383
		/**
384
		 * @param $nombre
385
		 * fonction qui permet de tuer des unites
386
		 */
387
		public function setTuerUnites($nombre) {
388
			$dbc = App::getDb();
389
			
390
			if ($nombre > 0) {
391
				$dbc->delete()->from("_bataille_unite")
392
					->where("ID_base", "=", Bataille::getIdBase(), "AND")
393
					->where("type", "=", "infanterie", "AND")
394
					->where("(ID_mission IS NULL OR ID_mission = 0)", "", "", "", true)
395
					->orderBy("RAND() ")
396
					->limit($nombre)
397
					->del();
398
			}
399
		}
400
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
401
	}