Passed
Push — master ( fa2ac1...7245f1 )
by Anthony
02:48
created

Unite   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 218
Duplicated Lines 4.13 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 30
c 4
b 0
f 0
lcom 1
cbo 5
dl 9
loc 218
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 4
B getCaracteristiqueUnite() 0 47 5
A getUnitePossibleRecruter() 0 13 2
B getRecrutement() 0 28 5
C setCommencerRecruter() 0 51 8
B setTerminerRecrutement() 0 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
131
132
				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...
133
			}
134
		}
135
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
136
		
137
		
138
		//-------------------------- SETTER ----------------------------------------------------------------------------//
139
		/**
140
		 * @param $nom -> nom de l'unité à recruter
141
		 * @param $type -> type de l'unité à recruter
142
		 * @param $nombre -> nombre d'unité à recruter
143
		 * fonction qui permet d'initialiser le début du recrutement d'unités
144
		 */
145
		public function setCommencerRecruter($nom, $type, $nombre) {
146
			$dbc1 = Bataille::getDb();
147
			$dbc = App::getDb();
148
149
			$query = $dbc1->select("temps_recrutement")
150
				->select("pour_recruter")
151
				->from("unites")
152
				->where("nom", "=", $nom, "AND")
153
				->where("type", "=", $type, "")
154
				->get();
155
156
			if ((is_array($query)) && (count($query) == 1)) {
157
				foreach ($query as $obj) {
158
					$pour_recruter = unserialize($obj->pour_recruter);
159
					$temps_recrutement = $obj->temps_recrutement;
160
				}
161
			}
162
163
			//on test si on a assez de ressource pour recruter les unites
164
			//on test si assez de ressources dans la base
165
			$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...
166
			$retirer_electricite = $pour_recruter["electricite"]*$nombre;
167
			$retirer_fer = $pour_recruter["fer"]*$nombre;
168
			$retirer_fuel = $pour_recruter["fuel"]*$nombre;
169
			$eau = Bataille::getTestAssezRessourceBase("eau", $retirer_eau);
170
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $retirer_electricite);
171
			$fer = Bataille::getTestAssezRessourceBase("fer", $retirer_fer);
172
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $retirer_fuel);
173
174
175
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
176
				FlashMessage::setFlash("Pas assez de ressources pour recruter autant d'unités");
177
				return false;
178
			}
179
			else {
180
				//on retire les ressources
181
				Bataille::getRessource()->setUpdateRessource($retirer_eau, $retirer_electricite, $retirer_fer, $retirer_fuel, 0, "-");
182
183
				$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...
184
185
				$dbc->insert("nom", $nom)
186
					->insert("type", $type)
187
					->insert("nombre", $nombre)
188
					->insert("date_fin", $date_fin)
189
					->insert("ID_base", Bataille::getIdBase())
190
					->into("_bataille_recrutement")
191
					->set();
192
193
				return true;
194
			}
195
		}
196
197
		/**
198
		 * @param $id_recrutement
199
		 * fonction appellée dans celle qui récupère les recrutement uniquement quand celui ci est finit
200
		 * fonction qui sert à terminer un rcrutement et ajouter les unités dans la base
201
		 */
202
		private function setTerminerRecrutement($id_recrutement) {
203
			$dbc = App::getDb();
204
205
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->get();
206
207
			if ((is_array($query)) && (count($query) == 1)) {
208
				foreach ($query as $obj) {
209
					$nombre = $obj->nombre;
210
					$nom = $obj->nom;
211
					$type = $obj->type;
212
				}
213
214
				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...
215
216
				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...
217
					$dbc->insert("nom_unite", $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...
218
						->insert("ID_base", Bataille::getIdBase())
219
						->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...
220
						->set();
221
				}
222
223
				$dbc->delete()->from("_bataille_recrutement")->where("ID_recrutement", "=", $id_recrutement)->del();
224
			}
225
		}
226
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
227
	}