Passed
Push — master ( 760f14...760f14 )
by Anthony
02:43
created

Unite::getCaracteristiqueUnite()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
rs 8.5125
cc 5
eloc 34
nc 5
nop 3
1
<?php
2
	
3
	namespace modules\bataille\app\controller;
4
	
5
	
6
	use core\App;
7
	use core\functions\DateHeure;
8
	use core\HTML\flashmessage\FlashMessage;
9
10
	class Unite {
11
		private $coef_unite;
12
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
		private 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($obj->temps_recrutement);
51
				}
52
53
				$coef = $this->coef_unite*$niveau;
54
				$coef_ameliorer = $this->coef_unite*($niveau+1);
55
56
				if ($niveau == 1) $coef = 1;
57
58
				return [
59
					"caracteristique" => [
60
						"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...
61
						"defense" => round($base_carac["defense"]*$coef),
62
						"resistance" => round($base_carac["resistance"]*$coef),
63
						"vitesse" => $base_carac["vitesse"]
64
					],
65
					"cout_recruter" => [
66
						"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...
67
						"electricite" => $ressource["electricite"]*$coef,
68
						"fer" => $ressource["fer"]*$coef,
69
						"fuel" => $ressource["fuel"]*$coef,
70
					],
71
					"cout_ameliorer" => [
72
						"eau" => $ressource["eau"]*$coef_ameliorer,
73
						"electricite" => $ressource["electricite"]*$coef_ameliorer,
74
						"fer" => $ressource["fer"]*$coef_ameliorer,
75
						"fuel" => $ressource["fuel"]*$coef_ameliorer,
76
					],
77
					"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...
78
				];
79
			}
80
			else {
81
				return [];
82
			}
83
		}
84
85
		/**
86
		 * @param $type
87
		 * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction
88
		 * du type (batiment sur lequel on a cliqué)
89
		 */
90
		public function getUnitePossibleRecruter($type) {
91
			//on recup toutes les unites deja recherchée donc que l'on peut faire
92
			$unites = Bataille::getCentreRecherche()->getAllRechercheType($type);
93
94
			//recupérer les caractéristiques de l'unité en question
95
			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...
96
				$unites[$i] += $this->getCaracteristiqueUnite($unites[$i]["recherche"], $unites[$i]["niveau"], $type);
97
				$unites[$i] += ["type" => $type];
98
			}
99
100
			//si pas d'unites encore recherchees on renvoit un array juste avec 0 dedans
101
			Bataille::setValues(["unites" => $unites]);
102
		}
103
104
		/**
105
		 * fonction qui renvoi les unité  en cours de recrutement
106
		 */
107
		public function getRecrutement() {
108
			$dbc = App::getDb();
109
110
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_base", "=", Bataille::getIdBase())->get();
111
112
			if ((is_array($query)) && (count($query) > 0)) {
113
				$today = Bataille::getToday();
114
115
				foreach ($query as $obj) {
116
					if ($obj->date_fin-$today <= 0) {
117
						$this->setTerminerRecrutement($obj->ID_recrutement);
118
					}
119
					else {
120
						$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...
121
							"nom" => $obj->nom,
122
							"type" => $obj->type,
123
							"nombre" => $obj->nombre,
124
							"date_fin_recrutement" => $obj->date_fin-$today,
125
							"id_recrutement" => $obj->ID_recrutement
126
						];
127
					}
128
				}
129
130
				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...
131
			}
132
		}
133
134
		/**
135
		 * @param null $id_base
136
		 * fonction qui récupère toutes les unités qui sont dans la base
137
		 */
138
		public function getAllUnites($id_base = null) {
139
140
			if ($id_base == null) $id_base = Bataille::getIdBase();
141
142
			$unite_infanterie = $this->getAllUniteType("unité infanterie", $id_base);
143
			$unite_vehicule = $this->getAllUniteType("unité véhicule", $id_base);
144
145
			$unites = array_merge($unite_vehicule, $unite_infanterie);
146
147
			Bataille::setValues(["unites" => $unites]);
148
		}
149
150
		/**
151
		 * @param $type
152
		 * @param $id_base
153
		 * @return mixed
154
		 * fonction qui récupère toutes les unités en fonction d'un type précis
155
		 */
156
		public function getAllUniteType($type, $id_base) {
157
			$dbc = App::getDb();
158
159
			$query = $dbc->select()->from("_bataille_unite")
160
				->where("type", "=", $type, "AND")
161
				->where("ID_base", "=", $id_base)
162
				->orderBy("type")
163
				->get();
164
165
			if ((is_array($query)) && (count($query) > 0)) {
166
				$count = 1;
167
				foreach ($query as $obj) {
168
					$unite[] = $unites[$type][$obj->nom_unite] = [
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...
169
						"nom" => $obj->nom_unite,
170
						"type" => $obj->type,
171
						"nombre" => $count++
172
					];
173
				}
174
175
				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...
176
			}
177
		}
178
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
179
		
180
		
181
		//-------------------------- SETTER ----------------------------------------------------------------------------//
182
		/**
183
		 * @param $nom -> nom de l'unité à recruter
184
		 * @param $type -> type de l'unité à recruter
185
		 * @param $nombre -> nombre d'unité à recruter
186
		 * fonction qui permet d'initialiser le début du recrutement d'unités
187
		 */
188
		public function setCommencerRecruter($nom, $type, $nombre) {
189
			$dbc1 = Bataille::getDb();
190
			$dbc = App::getDb();
191
192
			$query = $dbc1->select("temps_recrutement")
193
				->select("pour_recruter")
194
				->from("unites")
195
				->where("nom", "=", $nom, "AND")
196
				->where("type", "=", $type, "")
197
				->get();
198
199
			if ((is_array($query)) && (count($query) == 1)) {
200
				foreach ($query as $obj) {
201
					$pour_recruter = unserialize($obj->pour_recruter);
202
					$temps_recrutement = $obj->temps_recrutement;
203
				}
204
			}
205
206
			//on test si on a assez de ressource pour recruter les unites
207
			//on test si assez de ressources dans la base
208
			$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...
209
			$retirer_electricite = $pour_recruter["electricite"]*$nombre;
210
			$retirer_fer = $pour_recruter["fer"]*$nombre;
211
			$retirer_fuel = $pour_recruter["fuel"]*$nombre;
212
			$eau = Bataille::getTestAssezRessourceBase("eau", $retirer_eau);
213
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $retirer_electricite);
214
			$fer = Bataille::getTestAssezRessourceBase("fer", $retirer_fer);
215
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $retirer_fuel);
216
217
218
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
219
				FlashMessage::setFlash("Pas assez de ressources pour recruter autant d'unités");
220
				return false;
221
			}
222
			else {
223
				//on retire les ressources
224
				Bataille::getRessource()->setUpdateRessource($retirer_eau, $retirer_electricite, $retirer_fer, $retirer_fuel, 0, "-");
225
226
				$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...
227
228
				$dbc->insert("nom", $nom)
229
					->insert("type", $type)
230
					->insert("nombre", $nombre)
231
					->insert("date_fin", $date_fin)
232
					->insert("ID_base", Bataille::getIdBase())
233
					->into("_bataille_recrutement")
234
					->set();
235
236
				return true;
237
			}
238
		}
239
240
		/**
241
		 * @param $id_recrutement
242
		 * fonction appellée dans celle qui récupère les recrutement uniquement quand celui ci est finit
243
		 * fonction qui sert à terminer un rcrutement et ajouter les unités dans la base
244
		 */
245
		private function setTerminerRecrutement($id_recrutement) {
246
			$dbc = App::getDb();
247
248
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->get();
249
250
			if ((is_array($query)) && (count($query) == 1)) {
251
				foreach ($query as $obj) {
252
					$nombre = $obj->nombre;
253
					$nom = $obj->nom;
254
					$type = $obj->type;
255
				}
256
257
				if ($type == "unité infanterie") $table = "_bataille_unite";
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...
258
259
				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...
260
					$dbc->insert("nom_unite", $type)
261
						->insert("type", $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...
262
						->insert("ID_base", Bataille::getIdBase())
263
						->into($table)
0 ignored issues
show
Bug introduced by
The variable $table 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...
264
						->set();
265
				}
266
267
				$dbc->delete()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->del();
268
			}
269
		}
270
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
271
	}