Passed
Push — master ( 44c94d...e81c16 )
by Anthony
15:56
created

Batiment::getEmplacementConstructionLibre()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 27
nc 4
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
	use core\functions\ChaineCaractere;
5
	use core\functions\DateHeure;
6
	use core\HTML\flashmessage\FlashMessage;
7
	use Nette\Utils\DateTime;
8
9
	class Batiment {
10
		//pour quand on recup un batiment
11
		private $nom_batiment;
12
		private $nom_batiment_sql;
13
		private $niveau_batiment;
14
		private $temps_construction;
15
		private $ressource_construire;
16
		private $id_batiment;
17
18
		private $info_batiment;
19
		private $info_batiment_next;
20
21
		//pour les constructions
22
		private $nom_batiment_construction;
23
		private $date_fin_construction;
24
		private $niveau_batiment_construction;
25
26
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
27
		public function __construct() {
28
29
		}
30
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
31
32
33
34
		//-------------------------- GETTER ----------------------------------------------------------------------------//
35
		public function getNomBatiment() {
36
			return $this->nom_batiment;
37
		}
38
		public function getNomBatimentSql() {
39
			return $this->nom_batiment_sql;
40
		}
41
		public function getNiveau() {
42
			return $this->niveau_batiment;
43
		}
44
		public function getTempsConstruction() {
45
			return $this->temps_construction;
46
		}
47
		public function getRessourceConstruire() {
48
			return $this->ressource_construire;
49
		}
50
		public function getInfoBatiment(){
51
		    return $this->info_batiment;
52
		}
53
		public function getInfoBatimentNext(){
54
			return $this->info_batiment_next;
55
		}
56
57
		public function getNomBatimentConstruction() {
58
			return $this->nom_batiment_construction;
59
		}
60
		public function getDateFinConstruction() {
61
			return $this->date_fin_construction;
62
		}
63
		public function getNiveauBatimentConstruction() {
64
			return $this->niveau_batiment_construction;
65
		}
66
67
		/**
68
		 * @param $nom_batiment
69
		 * @return int
70
		 * pour recuperer le niveau d'un batiment
71
		 */
72
		public function getNiveauBatiment($nom_batiment_sql, $id_base = null) {
73
			$dbc = App::getDb();
74
75
			if ($id_base == null) {
76
				$id_base = Bataille::getIdBase();
77
			}
78
79
			$query = $dbc->select("niveau")
80
				->select("construction")
81
				->from("_bataille_batiment")
82
				->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND")
83
				->where("ID_base", "=", $id_base)
84
				->get();
85
86
			if ((is_array($query)) && (count($query) > 0)) {
87
				foreach ($query as $obj) {
88
					if ($obj->construction == 1) {
89
						return $obj->niveau-1;
90
					}
91
92
					return $obj->niveau;
93
				}
94
			}
95
			else {
96
				return 0;
97
			}
98
		}
99
100
		/**
101
		 * @param $ressource
102
		 * @return int
103
		 * recuperation de la production de chaque ressource en fonction du lvl des batiments
104
		 */
105
		public function getProduction($ressource, $id_base = null) {
106
			$dbc1 = Bataille::getDb();
107
108
			if ($id_base == null) {
109
				$id_base = Bataille::getIdBase();
110
			}
111
112
			if ($ressource == "eau") $nom_batiment = "centrale_eau";
113
			if ($ressource == "electricite") $nom_batiment = "centrale_electrique";
114
			if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel";
115
			if ($ressource == "fer") $nom_batiment = "station_forage";
116
117
			$niveau = $this->getNiveauBatiment($nom_batiment, $id_base);
0 ignored issues
show
Bug introduced by
The variable $nom_batiment 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...
118
119
			if ($niveau > 0) {
120
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
121
122
				if ((is_array($query)) && (count($query) > 0)) {
123
					foreach ($query as $obj) {
124
						$prod = $obj->production;
125
					}
126
127
					return $prod;
0 ignored issues
show
Bug introduced by
The variable $prod 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...
128
				}
129
			}
130
			else {
131
				return 20;
132
			}
133
		}
134
135
		/**
136
		 * @return int
137
		 * fonction qui retourne le stockage de l'entrepot
138
		 */
139
		public function getStockageEntrepot($id_base = null) {
140
			$dbc1 = Bataille::getDb();
141
142
			if ($id_base == null) {
143
				$id_base = Bataille::getIdBase();
144
			}
145
146
			$niveau = $this->getNiveauBatiment("entrepot", $id_base);
147
148
			if ($niveau > 0) {
149
				$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $niveau)->get();
150
151
				if ((is_array($query)) && (count($query) > 0)){
152
					foreach ($query as $obj) {
153
						return $obj->stockage;
154
					}
155
				}
156
			}
157
			else {
158
				return 1000;
159
			}
160
		}
161
162
		/**
163
		 * permet de récupérer toutes les infos d'un batiment dans la popup
164
		 * @param $nom_batiment
165
		 */
166
		public function getUnBatiment($nom_batiment) {
167
			$dbc = App::getDb();
168
			$dbc1 = Bataille::getDb();
169
170
			$construction = $this->getTestBatimentConstruction($nom_batiment);
171
172
			//recuperation des infos du batiment
173
			$query = $dbc->select()
174
				->from("_bataille_batiment")
175
				->where("nom_batiment", "=", $construction[0], "AND")
176
				->where("ID_base", "=", Bataille::getIdBase())
177
				->get();
178
179
			if ((is_array($query)) && (count($query) > 0)) {
180
				foreach ($query as $obj) {
181
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
182
					$this->niveau_batiment = $obj->niveau;
183
					$this->id_batiment = $obj->ID_batiment;
184
				}
185
186
				if (($construction[1] == true) && ($this->niveau_batiment > 1)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
187
					$this->niveau_batiment = $this->niveau_batiment + 1;
188
				}
189
			}
190
			else {
191
				$query = $dbc1->select("nom_table")
192
					->from("liste_batiment")
193
					->where("nom", "=", $nom_batiment)
194
					->get();
195
196
				if ((is_array($query)) && (count($query) > 0)) {
197
					foreach ($query as $obj) {
198
						$this->nom_batiment_sql = $obj->nom_table;
199
					}
200
				}
201
202
				$this->niveau_batiment = 0;
203
			}
204
205
			$max_level =  $this->getInfoUpgradeBatiment();
206
207
			//permet de savoir si le batiment produit bien des ressoures
208
			$batiment_production = [];
209
210
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
211
212
			if ((is_array($query)) && (count($query) > 0)) {
213
				foreach ($query as $obj) {
214
					$batiment_production[] = $obj->nom;
215
				}
216
			}
217
218
			Bataille::setValues([
219
				"nom_batiment_sql" => $this->nom_batiment_sql,
220
				"niveau_batiment" => $this->niveau_batiment,
221
				"id_batiment" => $this->niveau_batiment,
222
				"max_level" => $max_level,
223
				"batiment_production" => $batiment_production
224
			]);
225
226
			return $max_level;
227
		}
228
229
		/**
230
		 * pour récupérer la construction en cours dans la base
231
		 */
232
		public function getConstruction() {
233
			$dbc = App::getDb();
234
235
			$today = Bataille::getToday();
236
237
			$query = $dbc->select()
238
				->from("_bataille_construction")
239
				->from("_bataille_batiment")
240
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
241
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
242
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
243
				->get();
244
245
			if ((is_array($query)) && (count($query) > 0)) {
246
				foreach ($query as $obj) {
247
					$this->nom_batiment_construction = $obj->nom_batiment;
248
					$this->date_fin_construction = $obj->date_fin;
249
					$this->niveau_batiment_construction = $obj->niveau;
250
					$id_batiment = $obj->ID_batiment;
251
				}
252
253
				if ($this->date_fin_construction-$today <= 0) {
254
					$this->setTerminerConstruction($id_batiment);
0 ignored issues
show
Bug introduced by
The variable $id_batiment 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...
255
				}
256
				else {
257
					Bataille::setValues([
258
						"date_fin_construction" => $this->date_fin_construction-$today,
259
						"nom_batiment_construction" => $this->nom_batiment_construction
260
					]);
261
				}
262
263
				return 1;
264
			}
265
266
			return 0;
267
		}
268
269
		/**
270
		 * pour récupérer la liste des batiments qu'il est possible de construire
271
		 */
272
		public function getBatimentAConstruire() {
273
			$dbc = App::getDb();
274
			$dbc1 = Bataille::getDb();
275
			$batiment_construit = [];
276
			$batiment_construire = [];
277
278
			//recuperation des batiments deja construit dans la base
279
			$query = $dbc->select("nom_batiment_sql")
280
				->select("nom_batiment")
281
				->select("niveau")
282
				->from("_bataille_batiment")
283
				->where("ID_base", "=", Bataille::getIdBase())
284
				->get();
285
286
			if ((is_array($query)) && (count($query) > 0)) {
287
				foreach ($query as $obj) {
288
					$batiment_construit[] = $obj->nom_batiment_sql;
289
				}
290
			}
291
292
			//recuperation de la liste complete des batiments
293
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
294
			if ((is_array($query)) && (count($query) > 0)) {
295
				foreach ($query as $obj) {
296
					$all_batiment[] = $obj->nom_table;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_batiment was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_batiment = 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...
297
					$all_batiment_nom[] = $obj->nom;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_batiment_nom was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_batiment_nom = 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...
298
				}
299
300
				$c_all_batiment = count($all_batiment);
0 ignored issues
show
Bug introduced by
The variable $all_batiment 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...
301
			}
302
303
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
304
			//et compare la liste des batiments qu'il faut pour construire le batiment
305
			//a ceux qui sont deja construit dans la base
306
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
307
			for ($i=0 ; $i<$c_all_batiment ; $i++) {
0 ignored issues
show
Bug introduced by
The variable $c_all_batiment 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...
308
				if (!in_array($all_batiment[$i], $batiment_construit)) {
309
					$query = $dbc1->select("pour_construire")
310
						->select("temps_construction")
311
						->from($all_batiment[$i])
312
						->where("ID_".$all_batiment[$i], "=", 1)
313
						->get();
314
315
					if ((is_array($query)) && (count($query) > 0)) {
316
						foreach ($query as $obj) {
317
							if ($obj->pour_construire != null) {
318
								$pour_construire = unserialize($obj->pour_construire);
319
							}
320
							else {
321
								$pour_construire = [];
322
							}
323
324
325
							$temps_construction = gmdate("H:i:s", $obj->temps_construction);
326
						}
327
					}
328
329
330
					if (count($pour_construire) == 1) {
331
						if (in_array($pour_construire[0][1], $batiment_construit)) {
332
							if ($pour_construire[0][2] <= $this->getNiveauBatiment($pour_construire[0][1])) {
0 ignored issues
show
Bug introduced by
The variable $pour_construire 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...
333
								$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
334
335
								$batiment_construire[] = [
336
									"nom_batiment_sql" => $all_batiment[$i],
337
									"nom_batiment" => $all_batiment_nom[$i],
0 ignored issues
show
Bug introduced by
The variable $all_batiment_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...
338
									"ressource" => $ressource,
339
									"temps_construction" => $temps_construction
0 ignored issues
show
Bug introduced by
The variable $temps_construction 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...
340
								];
341
							}
342
						}
343
					}
344
					else if (count($pour_construire) > 1) {
345
						$ok_construction = false;
346
						//test si tous les batiments sont construits et on le niveau nécéssaire
347
						for ($j=0 ; $j<count($pour_construire) ; $j++) {
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...
348
							if (in_array($pour_construire[$j][1], $batiment_construit)) {
349
								if ($pour_construire[$j][2] <= $this->getNiveauBatiment($pour_construire[$j][1])) {
350
									$ok_construction = true;
351
								}
352
								else {
353
									$ok_construction = false;
354
									break;
355
								}
356
							}
357
							else {
358
								$ok_construction = false;
359
								break;
360
							}
361
						}
362
363
						//si ok on affiche le batiment
364 View Code Duplication
						if ($ok_construction === true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
365
							$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
366
367
							$batiment_construire[] = [
368
								"nom_batiment_sql" => $all_batiment[$i],
369
								"nom_batiment" => $all_batiment_nom[$i],
370
								"ressource" => $ressource,
371
								"temps_construction" => $temps_construction
372
							];
373
						}
374
					}
375 View Code Duplication
					else {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
376
						$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
377
378
						$batiment_construire[] = [
379
							"nom_batiment_sql" => $all_batiment[$i],
380
							"nom_batiment" => $all_batiment_nom[$i],
381
							"ressource" => $ressource,
382
							"temps_construction" => $temps_construction
383
						];
384
					}
385
				}
386
			}
387
			Bataille::setValues(["batiments_construire" => $batiment_construire]);
388
		}
389
390
		public function getEmplacementConstructionLibre($case_depart, $nom_batiment_sql) {
0 ignored issues
show
Unused Code introduced by
The parameter $nom_batiment_sql is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
391
			$dbc = App::getDb();
392
393
			//récupération de la taille du batiment
394
			$width_batiment = 120;
395
			$height_batiment = 60;
396
397
			//récupération des coordonnées de la sae de départ du batiment
398
			$case_depart = explode(",", $case_depart);
399
			$posx = $case_depart[0];
400
			$posy = $case_depart[1];
401
			$finx = $width_batiment+$posx;
402
			$finy = $height_batiment+$posy;
403
404
			//récupération de tous les batiments
405
			$query = $dbc->select("posx")
406
				->select("posy")
407
				->select("nom_batiment_sql")
408
				->from("_bataille_batiment")
409
				->where("ID_base", "=", Bataille::getIdBase())
410
				->get();
411
412
			if ((is_array($query)) && (count($query) > 0)) {
413
				foreach ($query as $obj) {
414
					$taille_batiment = $this->getTailleBatiment($obj->nom_batiment_sql);
415
					$posx_batiment = $obj->posx;
416
					$posy_batiment = $obj->posy;
417
418
					$finx_batiment = $taille_batiment[0]+$posx_batiment;
419
					$finy_batiment = $taille_batiment[1]+$posy_batiment;
420
421
					//ok pour coin haut-gauche ++ coin bas-droite
422
					if (((($posx >= $posx_batiment) && ($posx <= $finx_batiment)) &&
423
						(($posy >= $posy_batiment) && ($posy <= $finy_batiment))) ||
424
						((($finx >= $posx_batiment) && ($finx <= $finx_batiment)) &&
425
						(($finy >= $posy_batiment) && ($finy <= $finy_batiment))))
426
					{
427
						echo("toto");
428
					}
429
				}
430
			}
431
		}
432
433
		/**
434
		 * @param $nom_batiment_sql
435
		 * @return array
436
		 * fonction qui renvoi un tableau contenant la taille et hauteur d'un batiment en
437
		 * fonction de son nom
438
		 */
439 View Code Duplication
		private function getTailleBatiment($nom_batiment_sql) {
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...
440
			$dbc1 = Bataille::getDb();
441
442
			$query = $dbc1->select("width")
443
				->select("height")
444
				->from("liste_batiment")
445
				->where("nom_table", "=", $nom_batiment_sql)
446
				->get();
447
448
			if ((is_array($query)) && (count($query) > 0)) {
449
				foreach ($query as $obj) {
450
					return [$obj->width, $obj->height];
451
				}
452
			}
453
		}
454
455
		/**
456
		 * @param $nom_batiment_sql
457
		 * @param $niveau
458
		 * @return array
459
		 * recuperation des ressources nécéssaire pour construire le batiment
460
		 */
461
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
462
			$dbc1 = Bataille::getDb();
463
464
			$niveau = $niveau+1;
465
466
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
467
468
			if ((is_array($query)) && (count($query) > 0)) {
469
				foreach ($query as $obj) {
470
					$ressource_tmp = $obj->ressource_construire;
471
				}
472
				$ressource_tmp = explode(", ", $ressource_tmp);
0 ignored issues
show
Bug introduced by
The variable $ressource_tmp 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...
473
474
				//on test si assez de ressources dans la base
475
				//fer
476
				$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $ressource_tmp[2]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ressource was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ressource = 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...
477
				//fuel
478
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[3]);
479
				//eau
480
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[0]);
481
				//electricite
482
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[1]);
483
484
				return $ressource;
485
			}
486
		}
487
488
		/**
489
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
490
		 * + true pour dire que le batiment est en construction
491
		 *
492
		 * @param $nom_batiment
493
		 * @return array
494
		 */
495
		private function getTestBatimentConstruction($nom_batiment) {
496
			if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) {
497
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
498
			}
499
500
			return [$nom_batiment, false];
501
		}
502
503
		/**
504
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
505
		 * @return int
506
		 */
507
		private function getInfoUpgradeBatiment() {
508
			$dbc1 = Bataille::getDb();
509
510
			//récupération du temps et des ressources pour construire
511
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
512
513
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
514
			if ((is_array($query)) && (count($query) > 0)) {
515
				foreach ($query as $obj) {
516
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
517
					$this->temps_construction = DateHeure::Secondeenheure($obj->temps_construction);
518
				}
519
520
				//récupération des éléments particulier à un batiment
521
				$xml = simplexml_load_file(MODULEROOT.'bataille/data/batiment.xml');
522
				$nom_batiment_sql = $this->nom_batiment_sql;
523
				$champ = $xml->$nom_batiment_sql->champ;
524
525
				if (!empty($champ)) {
526
					//récupération de la phrase pour le niveau actuel
527
					$query = $dbc1->select($xml->$nom_batiment_sql->champ)
528
						->from($this->nom_batiment_sql)
529
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment)
530
						->get();
531
532 View Code Duplication
					if ((is_array($query)) && (count($query) > 0)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
533
						foreach ($query as $obj) {
534
							$this->info_batiment = $xml->$nom_batiment_sql->phrase.$obj->$champ.$xml->$nom_batiment_sql->complement;
535
						}
536
					}
537
538
					//récupération de la phrase pour le niveau suivant
539
					$query = $dbc1->select($xml->$nom_batiment_sql->champ)
540
						->from($this->nom_batiment_sql)
541
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
542
						->get();
543
544 View Code Duplication
					if ((is_array($query)) && (count($query) > 0)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
545
						foreach ($query as $obj) {
546
							$this->info_batiment_next = $xml->$nom_batiment_sql->phrase_suivant.$obj->$champ.$xml->$nom_batiment_sql->complement;
547
						}
548
					}
549
				}
550
				else {
551
					$this->info_batiment = "";
552
					$this->info_batiment_next = "";
553
				}
554
555
556
				Bataille::setValues([
557
					"ressource" => $this->ressource_construire,
558
					"temps_construction" => $this->temps_construction,
559
					"info_batiment" => $this->info_batiment,
560
					"info_batiment_next" => $this->info_batiment_next,
561
				]);
562
563
				return 1;
564
			}
565
566
			return 0;
567
		}
568
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
569
570
571
572
		//-------------------------- SETTER ----------------------------------------------------------------------------//
573
		/**
574
		 * fonction qui initialise la construction d'un batiment
575
		 * @param $nom_batiment
576
		 * @param $nom_batiment_sql
577
		 */
578
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql) {
579
			$dbc = App::getDb();
580
			$dbc1 = Bataille::getDb();
581
582
			if ($this->getConstruction() == 0) {
583
				$un_batiment = $this->getUnBatiment($nom_batiment);
584
585
				//on test si assez de ressrouce pour construire le batiment
586
				if ($un_batiment == 0) {
587
					$ressource = $this->getRessourceConstruireBatiment($nom_batiment_sql, 0);
588
					$this->nom_batiment_sql = $nom_batiment_sql;
589
					$this->niveau_batiment = 0;
590
				}
591
				else {
592
					$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
593
				}
594
595
				//si pas assez de ressource
596
				if (in_array("rouge", $ressource[0])) {
597
					FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
598
				}
599
				else {
600
					//recuperation du temps de construction
601
					$query = $dbc1->select("ressource_construire")
602
						->select("temps_construction")
603
						->from($this->nom_batiment_sql)
604
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
605
						->get();
606
607
					foreach ($query as $obj) {
608
						$temps_construction = $obj->temps_construction;
609
						$ressource_construction = explode(", ", $obj->ressource_construire);
610
					}
611
612
					//on insere la construction dans la table batiment si new batiment
613
					if ($un_batiment == 0) {
614
						$dbc->insert("niveau", $this->niveau_batiment+1)
615
							->insert("nom_batiment", $nom_batiment)
616
							->insert("nom_batiment_sql", $this->nom_batiment_sql)
617
							->insert("construction", 1)
618
							->insert("ID_base", Bataille::getIdBase())
619
							->into("_bataille_batiment")
620
							->set();
621
622
						$this->id_batiment = $dbc->lastInsertId();
623
					}
624
					else {
625
						$dbc->update("niveau", $this->niveau_batiment+1)
626
							->update("construction", 1)
627
							->from("_bataille_batiment")
628
							->where("ID_batiment", "=", $this->id_batiment, "AND")
629
							->where("ID_base", "=", Bataille::getIdBase())
630
							->set();
631
					}
632
633
634
					//on initialise la construction
635
					//recuperation de la date en seconde
636
					$today = Bataille::getToday();
637
638
					//date de la fin de la construction en seconde
639
					$fin_construction = $today+$temps_construction;
0 ignored issues
show
Bug introduced by
The variable $temps_construction 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...
640
641
					$dbc->insert("date_fin", $fin_construction)
642
						->insert("ID_base", Bataille::getIdBase())
643
						->insert("ID_batiment", $this->id_batiment)
644
						->into("_bataille_construction")
645
						->set();
646
647
					//on retire les ressources de la base
648
					Bataille::getRessource()->setUpdateRessource($ressource_construction[2], $ressource_construction[3], $ressource_construction[0], $ressource_construction[1], 0, "-");
0 ignored issues
show
Bug introduced by
The variable $ressource_construction 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...
649
				}
650
			}
651
			else {
652
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
653
			}
654
		}
655
656
		/**
657
		 * fonction qui termine la construction d'un batiment
658
		 * @param $id_batiment
659
		 */
660
		private function setTerminerConstruction($id_batiment) {
661
			$dbc = App::getDb();
662
663
			//on le retire de la table construction
664
			$dbc->delete()
665
				->from("_bataille_construction")
666
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
667
				->where("ID_batiment", "=", $id_batiment)
668
				->del();
669
670
			//on termine la construction dans la table batiment
671
			$dbc->update("construction", 0)
672
				->from("_bataille_batiment")
673
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
674
				->where("ID_batiment", "=", $id_batiment)
675
				->set();
676
			
677
			//on ajoute les points à la base
678
			Points::setAjouterPoints(Bataille::getIdBase(), "batiment");
679
		}
680
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
681
	}