Passed
Push — master ( f325e8...ec9023 )
by Anthony
02:34
created

Batiment::getUnBatiment()   C

Complexity

Conditions 13
Paths 10

Size

Total Lines 70
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 5.6314
c 0
b 0
f 0
cc 13
eloc 42
nc 10
nop 2

How to fix   Long Method    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\HTML\flashmessage\FlashMessage;
6
	use Nette\Utils\DateTime;
7
8
	class Batiment {
9
		//pour quand on recup un batiment
10
		private $nom_batiment;
11
		private $nom_batiment_sql;
12
		private $niveau_batiment;
13
		private $temps_construction;
14
		private $ressource_construire;
15
		private $id_batiment;
16
17
		private $info_batiment;
18
19
		//pour les constructions
20
		private $nom_batiment_construction;
21
		private $date_fin_construction;
22
		private $niveau_batiment_construction;
23
24
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
25
		public function __construct() {
26
27
		}
28
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
29
30
31
32
		//-------------------------- GETTER ----------------------------------------------------------------------------//
33
		public function getNomBatiment() {
34
			return $this->nom_batiment;
35
		}
36
		public function getNomBatimentSql() {
37
			return $this->nom_batiment_sql;
38
		}
39
		public function getNiveau() {
40
			return $this->niveau_batiment;
41
		}
42
		public function getTempsConstruction() {
43
			return $this->temps_construction;
44
		}
45
		public function getRessourceConstruire() {
46
			return $this->ressource_construire;
47
		}
48
		public function getInfoBatiment(){
49
		    return $this->info_batiment;
50
		}
51
52
		public function getNomBatimentConstruction() {
53
			return $this->nom_batiment_construction;
54
		}
55
		public function getDateFinConstruction() {
56
			return $this->date_fin_construction;
57
		}
58
		public function getNiveauBatimentConstruction() {
59
			return $this->niveau_batiment_construction;
60
		}
61
62
		/**
63
		 * @param $nom_batiment
64
		 * @return int
65
		 * pour recuperer le niveau d'un batiment
66
		 */
67
		private function getNiveauBatiment($nom_batiment_sql) {
68
			$dbc = App::getDb();
69
70
			$query = $dbc->select("niveau")
71
				->select("construction")
72
				->from("_bataille_batiment")
73
				->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND")
74
				->where("ID_base", "=", Bataille::getIdBase())
75
				->get();
76
77
			if ((is_array($query)) && (count($query) > 0)) {
78
				foreach ($query as $obj) {
79
					if ($obj->construction == 1) {
80
						return $obj->niveau-1;
81
					}
82
83
					return $obj->niveau;
84
				}
85
			}
86
			else {
87
				return 0;
88
			}
89
		}
90
91
		/**
92
		 * @param $ressource
93
		 * @return int
94
		 * recuperation de la production de chaque ressource en fonction du lvl des batiments + recup prod addon
95
		 */
96
		public function getProduction($ressource) {
97
			$dbc1 = Bataille::getDb();
98
99
			if ($ressource == "eau") $nom_batiment = "centrale_eau";
100
			if ($ressource == "electricite") $nom_batiment = "centrale_electrique";
101
			if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel";
102
			if ($ressource == "fer") $nom_batiment = "station_forage";
103
104
			$niveau = $this->getNiveauBatiment($nom_batiment);
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...
105
106
			if ($niveau > 0) {
107
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
108
109
				if ((is_array($query)) && (count($query) > 0)) {
110
					foreach ($query as $obj) {
111
						$prod = $obj->production;
112
					}
113
114
					$query = $dbc1->select("production")
115
						->from($nom_batiment."_addon")
116
						->where("ID_".$nom_batiment."_addon", "=", $this->getNiveauBatiment($nom_batiment."_addon"))
117
						->get();
118
119
					if ((is_array($query)) && (count($query) > 0)) {
120
						foreach ($query as $obj) {
121
							$prod_addon = $obj->production;
122
						}
123
					}
124
					else {
125
						$prod_addon = 0;
126
					}
127
128
					return $prod + $prod_addon;
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...
Bug introduced by
The variable $prod_addon 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...
129
				}
130
			}
131
			else {
132
				return 20;
133
			}
134
		}
135
136
		/**
137
		 * @return int
138
		 * fonction qui retourne le stockage de l'entrepot
139
		 */
140
		public function getStockageEntrepot() {
141
			$dbc1 = Bataille::getDb();
142
143
			$niveau = $this->getNiveauBatiment("entrepot");
144
145
			if ($niveau > 0) {
146
				$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $niveau)->get();
147
148 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...
149
					foreach ($query as $obj) {
150
						return $obj->stockage;
151
					}
152
				}
153
			}
154
			else {
155
				return 1000;
156
			}
157
		}
158
159
		/**
160
		 * permet de récupérer toutes les infos d'un batiment dans la popup
161
		 * @param $nom_batiment
162
		 * @param $emplacement
163
		 */
164
		/*public function getUnBatiment($nom_batiment, $emplacement) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
			$dbc = App::getDb();
166
			$dbc1 = Bataille::getDb();
167
168
			$construction = $this->getTestBatimentConstruction($nom_batiment);
169
170
			//recuperation des infos du batiment
171
			$query = $dbc->select()
172
				->from("_bataille_batiment")
173
				->where("nom_batiment", "=", $construction[0], "AND")
174
				->where("emplacement", "=", $emplacement, "AND")
175
				->where("ID_base", "=", Bataille::getIdBase())
176
				->get();
177
178
			if ((is_array($query)) && (count($query) > 0)) {
179
				foreach ($query as $obj) {
180
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
181
					$this->niveau_batiment = $obj->niveau;
182
					$this->id_batiment = $obj->ID_batiment;
183
				}
184
185
				if (($construction[1] == true) && ($this->niveau_batiment > 1)) {
186
					$this->niveau_batiment = $this->niveau_batiment+1;
187
				}
188
189
				return $this->getInfoUpgradeBatiment();
190
			}
191
			else {
192
				//on test voir si le bat est au niveau max et si il peut avoir un addon
193
				if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
194
					$query = $dbc1->select("nom_table")->from("liste_batiment")->where("nom", "=", $nom_batiment)->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
						$this->niveau_batiment = 0;
202
203
						return $this->getInfoUpgradeBatiment();
204
					}
205
					else {
206
						return 0;
207
					}
208
				}
209
210
211
				return 0;
212
			}
213
		}*/
214
		public function getUnBatiment($nom_batiment, $emplacement) {
215
			$dbc = App::getDb();
216
			$dbc1 = Bataille::getDb();
217
218
			$construction = $this->getTestBatimentConstruction($nom_batiment);
219
220
			//recuperation des infos du batiment
221
			$query = $dbc->select()
222
				->from("_bataille_batiment")
223
				->where("nom_batiment", "=", $construction[0], "AND")
224
				->where("emplacement", "=", $emplacement, "AND")
225
				->where("ID_base", "=", Bataille::getIdBase())
226
				->get();
227
228
			if ((is_array($query)) && (count($query) > 0)) {echo($construction[0].$emplacement.Bataille::getIdBase());
229
				foreach ($query as $obj) {
230
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
231
					$this->niveau_batiment = $obj->niveau;
232
					$this->id_batiment = $obj->ID_batiment;
233
				}
234
235
				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...
236
					$this->niveau_batiment = $this->niveau_batiment + 1;
237
				}
238
				$max_level =  $this->getInfoUpgradeBatiment();
239
			}
240
			else {
241
				//on test voir si le bat est au niveau max et si il peut avoir un addon
242
				if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
243
					$query = $dbc1->select("nom_table")->from("liste_batiment")->where("nom", "=", $nom_batiment)->get();
244
245
					if ((is_array($query)) && (count($query) > 0)) {
246
						foreach ($query as $obj) {
247
							$this->nom_batiment_sql = $obj->nom_table;
248
						}
249
250
						$this->niveau_batiment = 0;
251
252
						$max_level = $this->getInfoUpgradeBatiment();
253
					}
254
					else {
255
						$max_level = 0;
256
					}
257
				}
258
				else {
259
					$max_level = 0;
260
				}
261
			}
262
263
			//permet de savoir si le batiment produit bien des ressoures
264
			$batiment_production = [];
265
266
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
267
268
			if ((is_array($query)) && (count($query) > 0)) {
269
				foreach ($query as $obj) {
270
					$batiment_production[] = $obj->nom;
271
				}
272
			}
273
274
			Bataille::setValues([
275
				"nom_batiment_sql" => $this->nom_batiment_sql,
276
				"niveau_batiment" => $this->niveau_batiment,
277
				"id_batiment" => $this->niveau_batiment,
278
				"max_level" => $max_level,
279
				"batiment_production" => $batiment_production
280
			]);
281
282
			return $max_level;
283
		}
284
285
		/**
286
		 * pour récupérer la construction en cours dans la base
287
		 */
288
		public function getConstruction() {
289
			$dbc = App::getDb();
290
291
			$today = new DateTime();
292
			$today = $today->getTimestamp();
293
294
			$query = $dbc->select()
295
				->from("_bataille_construction")
296
				->from("_bataille_batiment")
297
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
298
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
299
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
300
				->get();
301
302
			if ((is_array($query)) && (count($query) > 0)) {
303
				foreach ($query as $obj) {
304
					$this->nom_batiment_construction = $obj->nom_batiment;
305
					$this->date_fin_construction = $obj->date_fin;
306
					$this->niveau_batiment_construction = $obj->niveau;
307
					$id_batiment = $obj->ID_batiment;
308
				}
309
310
				if ($this->date_fin_construction-$today <= 0) {
311
					$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...
312
				}
313
				else {
314
					Bataille::setValues([
315
						"date_fin_construction" => $this->date_fin_construction-$today,
316
						"nom_batiment_construction" => $this->nom_batiment_construction
317
					]);
318
				}
319
320
				return 1;
321
			}
322
323
			return 0;
324
		}
325
326
		/**
327
		 * pour récupérer la liste des batiments qu'il est possible de construire
328
		 */
329
		public function getBatimentAConstruire() {
330
			$dbc = App::getDb();
331
			$dbc1 = Bataille::getDb();
332
			$batiment_construit = [];
333
			$batiment_construire = [];
334
335
			//recuperation des batiments deja construit dans la base
336
			$query = $dbc->select("nom_batiment_sql")
337
				->select("nom_batiment")
338
				->select("niveau")
339
				->from("_bataille_batiment")
340
				->where("ID_base", "=", Bataille::getIdBase())
341
				->get();
342
343
			if ((is_array($query)) && (count($query) > 0)) {
344
				foreach ($query as $obj) {
345
					$batiment_construit[] = $obj->nom_batiment_sql;
346
				}
347
			}
348
349
			//recuperation de la liste complete des batiments
350
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
351
			if ((is_array($query)) && (count($query) > 0)) {
352
				foreach ($query as $obj) {
353
					$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...
354
					$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...
355
				}
356
357
				$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...
358
			}
359
360
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
361
			//et compare la liste des batiments qu'il faut pour construire le batiment
362
			//a ceux qui sont deja construit dans la base
363
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
364
			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...
365
				if (!in_array($all_batiment[$i], $batiment_construit)) {
366
					$query = $dbc1->select("pour_construire")
367
						->select("temps_construction")
368
						->from($all_batiment[$i])
369
						->where("ID_".$all_batiment[$i], "=", 1)
370
						->get();
371
372
					if ((is_array($query)) && (count($query) > 0)) {
373
						foreach ($query as $obj) {
374
							if ($obj->pour_construire != null) {
375
								$pour_construire = unserialize($obj->pour_construire);
376
							}
377
							else {
378
								$pour_construire = [];
379
							}
380
381
382
							$temps_construction = gmdate("H:i:s", $obj->temps_construction);
383
						}
384
					}
385
386
387
					if (count($pour_construire) == 1) {
388
						if (in_array($pour_construire[0][1], $batiment_construit)) {
389
							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...
390
								$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
391
392
								$batiment_construire[] = [
393
									"nom_batiment_sql" => $all_batiment[$i],
394
									"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...
395
									"ressource" => $ressource,
396
									"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...
397
								];
398
							}
399
						}
400
					}
401
					else if (count($pour_construire) > 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
402
						//TODO si besoin de plus d'un seul batiment pour etre construit
403
					}
404
					else {
405
						$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
406
407
						$batiment_construire[] = [
408
							"nom_batiment_sql" => $all_batiment[$i],
409
							"nom_batiment" => $all_batiment_nom[$i],
410
							"ressource" => $ressource,
411
							"temps_construction" => $temps_construction
412
						];
413
					}
414
				}
415
			}
416
			Bataille::setValues(["batiments" => $batiment_construire]);
417
		}
418
419
		/**
420
		 * @param $nom_batiment_sql
421
		 * @param $niveau
422
		 * @return array
423
		 * recuperation des ressources nécéssaire pour construire le batiment
424
		 */
425
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
426
			$dbc1 = Bataille::getDb();
427
428
			$niveau = $niveau+1;
429
430
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
431
432
			if ((is_array($query)) && (count($query) > 0)) {
433
				foreach ($query as $obj) {
434
					$ressource_tmp = $obj->ressource_construire;
435
				}
436
				$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...
437
438
				//on test si assez de ressources dans la base
439
				//fer
440
				$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $ressource_tmp[0]);
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...
441
				//fuel
442
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[1]);
443
				//eau
444
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[2]);
445
				//electricite
446
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[3]);
447
448
				return $ressource;
449
			}
450
		}
451
452
		/**
453
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
454
		 * + true pour dire que le batiment est en construction
455
		 *
456
		 * @param $nom_batiment
457
		 * @return array
458
		 */
459
		private function getTestBatimentConstruction($nom_batiment) {
460
			if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) {
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...
461
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
462
			}
463
464
			return [$nom_batiment, false];
465
		}
466
467
		/**
468
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
469
		 * ou la création d'un addon sur ce batiment
470
		 * @return int
471
		 */
472
		private function getInfoUpgradeBatiment() {
473
			$dbc1 = Bataille::getDb();
474
475
			//récupération du temps et des ressources pour construire
476
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
477
478
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
479
			if ((is_array($query)) && (count($query) > 0)) {
480
				foreach ($query as $obj) {
481
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
482
					$this->temps_construction = gmdate("H:i:s", $obj->temps_construction);
483
				}
484
485
				//récupération des éléments particulier à un batiment
486
				if ($this->nom_batiment_sql == "entrepot") {
487
					$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $this->niveau_batiment+1)->get();
488
489 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...
490
						foreach ($query as $obj) {
491
							$this->info_batiment = "Capacité de l'entrepôt au prochain niveau : ". $obj->stockage;
492
						}
493
					}
494
				}
495
496
				Bataille::setValues([
497
					"ressource" => $this->ressource_construire,
498
					"temps_construction" => $this->temps_construction,
499
					"info_batiment" => $this->info_batiment,
500
				]);
501
502
				return 1;
503
			}
504
505
			return 0;
506
		}
507
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
508
509
510
511
		//-------------------------- SETTER ----------------------------------------------------------------------------//
512
		/**
513
		 * fonction qui initialise la construction d'un batiment
514
		 * @param $nom_batiment
515
		 * @param $nom_batiment_sql
516
		 * @param $emplacement
517
		 */
518
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $emplacement) {
519
			$dbc = App::getDb();
520
			$dbc1 = Bataille::getDb();
521
522
			if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
523
				$emplacement = 0;
524
			}
525
526
			if ($this->getConstruction() == 0) {
527
				$un_batiment = $this->getUnBatiment($nom_batiment, $emplacement);
528
529
				//on test si assez de ressrouce pour construire le batiment
530
				if ($un_batiment == 0) {
531
					$ressource = $this->getRessourceConstruireBatiment($nom_batiment_sql, 0);
532
					$this->nom_batiment_sql = $nom_batiment_sql;
533
					$this->niveau_batiment = 0;
534
				}
535
				else {
536
					//si c'est le lvl 0 de l'addon
537
					if ($this->niveau_batiment == 0) {
538
						$un_batiment = 0;
539
					}
540
					$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
541
				}
542
543
				//si pas assez de ressource
544
				if (in_array("rouge", $ressource[0])) {
545
					FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
546
				}
547
				else {
548
					//recuperation du temps de construction
549
					$query = $dbc1->select("ressource_construire")
550
						->select("temps_construction")
551
						->from($this->nom_batiment_sql)
552
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
553
						->get();
554
555
					foreach ($query as $obj) {
556
						$temps_construction = $obj->temps_construction;
557
						$ressource_construction = explode(", ", $obj->ressource_construire);
558
					}
559
560
					//on insere la construction dans la table batiment si new batiment
561
					if ($un_batiment == 0) {
562
						$dbc->insert("niveau", $this->niveau_batiment+1)
563
							->insert("emplacement", $emplacement)
564
							->insert("nom_batiment", $nom_batiment)
565
							->insert("nom_batiment_sql", $this->nom_batiment_sql)
566
							->insert("construction", 1)
567
							->insert("ID_base", Bataille::getIdBase())
568
							->into("_bataille_batiment")
569
							->set();
570
571
						$this->id_batiment = $dbc->lastInsertId();
572
					}
573
					else {
574
						$dbc->update("niveau", $this->niveau_batiment+1)
575
							->update("construction", 1)
576
							->from("_bataille_batiment")
577
							->where("ID_batiment", "=", $this->id_batiment, "AND")
578
							->where("ID_base", "=", Bataille::getIdBase())
579
							->set();
580
					}
581
582
583
					//on initialise la construction
584
					//recuperation de la date en seconde
585
					$today = new DateTime();
586
					$today = $today->getTimestamp();
587
588
					//date de la fin de la construction en seconde
589
					$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...
590
591
					$dbc->insert("date_fin", $fin_construction)
592
						->insert("emplacement_construction", $emplacement)
593
						->insert("ID_base", Bataille::getIdBase())
594
						->insert("ID_batiment", $this->id_batiment)
595
						->into("_bataille_construction")
596
						->set();
597
598
					//on retire les ressources de la base
599
					Bataille::getRessource()->setRetirerRessource($ressource_construction[2], $ressource_construction[3], $ressource_construction[0], $ressource_construction[1]);
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...
600
				}
601
			}
602
			else {
603
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
604
			}
605
		}
606
607
		/**
608
		 * fonction qui termine la construction d'un batiment
609
		 * @param $id_batiment
610
		 */
611
		private function setTerminerConstruction($id_batiment) {
612
			$dbc = App::getDb();
613
614
			//on le retire de la table construction
615
			$dbc->delete()
616
				->from("_bataille_construction")
617
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
618
				->where("ID_batiment", "=", $id_batiment)
619
				->del();
620
621
			//on termine la construction dans la table batiment
622
			$dbc->update("construction", "NULL")
623
				->from("_bataille_batiment")
624
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
625
				->where("ID_batiment", "=", $id_batiment)
626
				->set();
627
		}
628
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
629
	}