Passed
Push — master ( c64d93...89b8c6 )
by Anthony
28:43
created

Unite::getRecrutement()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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