Passed
Push — master ( d27ad8...d52a81 )
by Anthony
02:58
created

Batiment::getProduction()   D

Complexity

Conditions 13
Paths 192

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 4.8178
c 0
b 0
f 0
cc 13
eloc 26
nc 192
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\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
		private $info_batiment_next;
19
20
		//pour les constructions
21
		private $nom_batiment_construction;
22
		private $date_fin_construction;
23
		private $niveau_batiment_construction;
24
25
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
26
		public function __construct() {
27
28
		}
29
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
30
31
32
33
		//-------------------------- GETTER ----------------------------------------------------------------------------//
34
		public function getNomBatiment() {
35
			return $this->nom_batiment;
36
		}
37
		public function getNomBatimentSql() {
38
			return $this->nom_batiment_sql;
39
		}
40
		public function getNiveau() {
41
			return $this->niveau_batiment;
42
		}
43
		public function getTempsConstruction() {
44
			return $this->temps_construction;
45
		}
46
		public function getRessourceConstruire() {
47
			return $this->ressource_construire;
48
		}
49
		public function getInfoBatiment(){
50
		    return $this->info_batiment;
51
		}
52
		public function getInfoBatimentNext(){
53
			return $this->info_batiment_next;
54
		}
55
56
		public function getNomBatimentConstruction() {
57
			return $this->nom_batiment_construction;
58
		}
59
		public function getDateFinConstruction() {
60
			return $this->date_fin_construction;
61
		}
62
		public function getNiveauBatimentConstruction() {
63
			return $this->niveau_batiment_construction;
64
		}
65
66
		/**
67
		 * @param $nom_batiment
68
		 * @return int
69
		 * pour recuperer le niveau d'un batiment
70
		 */
71
		public function getNiveauBatiment($nom_batiment_sql, $id_base = null) {
72
			$dbc = App::getDb();
73
74
			if ($id_base == null) {
75
				$id_base = Bataille::getIdBase();
76
			}
77
78
			$query = $dbc->select("niveau")
79
				->select("construction")
80
				->from("_bataille_batiment")
81
				->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND")
82
				->where("ID_base", "=", $id_base)
83
				->get();
84
85
			if ((is_array($query)) && (count($query) > 0)) {
86
				foreach ($query as $obj) {
87
					if ($obj->construction == 1) {
88
						return $obj->niveau-1;
89
					}
90
91
					return $obj->niveau;
92
				}
93
			}
94
			else {
95
				return 0;
96
			}
97
		}
98
99
		/**
100
		 * @param $ressource
101
		 * @return int
102
		 * recuperation de la production de chaque ressource en fonction du lvl des batiments + recup prod addon
103
		 */
104
		public function getProduction($ressource, $id_base = null) {
105
			$dbc1 = Bataille::getDb();
106
107
			if ($id_base == null) {
108
				$id_base = Bataille::getIdBase();
109
			}
110
111
			if ($ressource == "eau") $nom_batiment = "centrale_eau";
112
			if ($ressource == "electricite") $nom_batiment = "centrale_electrique";
113
			if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel";
114
			if ($ressource == "fer") $nom_batiment = "station_forage";
115
116
			$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...
117
118
			if ($niveau > 0) {
119
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
120
121
				if ((is_array($query)) && (count($query) > 0)) {
122
					foreach ($query as $obj) {
123
						$prod = $obj->production;
124
					}
125
126
					$query = $dbc1->select("production")
127
						->from($nom_batiment."_addon")
128
						->where("ID_".$nom_batiment."_addon", "=", $this->getNiveauBatiment($nom_batiment."_addon"))
129
						->get();
130
131
					if ((is_array($query)) && (count($query) > 0)) {
132
						foreach ($query as $obj) {
133
							$prod_addon = $obj->production;
134
						}
135
					}
136
					else {
137
						$prod_addon = 0;
138
					}
139
140
					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...
141
				}
142
			}
143
			else {
144
				return 20;
145
			}
146
		}
147
148
		/**
149
		 * @return int
150
		 * fonction qui retourne le stockage de l'entrepot
151
		 */
152
		public function getStockageEntrepot($id_base = null) {
153
			$dbc1 = Bataille::getDb();
154
155
			if ($id_base == null) {
156
				$id_base = Bataille::getIdBase();
157
			}
158
159
			$niveau = $this->getNiveauBatiment("entrepot", $id_base);
160
161
			if ($niveau > 0) {
162
				$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $niveau)->get();
163
164
				if ((is_array($query)) && (count($query) > 0)){
165
					foreach ($query as $obj) {
166
						return $obj->stockage;
167
					}
168
				}
169
			}
170
			else {
171
				return 1000;
172
			}
173
		}
174
175
		/**
176
		 * permet de récupérer toutes les infos d'un batiment dans la popup
177
		 * @param $nom_batiment
178
		 * @param $emplacement
179
		 */
180
		public function getUnBatiment($nom_batiment, $emplacement) {
181
			$dbc = App::getDb();
182
			$dbc1 = Bataille::getDb();
183
184
			$construction = $this->getTestBatimentConstruction($nom_batiment);
185
186
			//recuperation des infos du batiment
187
			$query = $dbc->select()
188
				->from("_bataille_batiment")
189
				->where("nom_batiment", "=", $construction[0], "AND")
190
				->where("emplacement", "=", $emplacement, "AND")
191
				->where("ID_base", "=", Bataille::getIdBase())
192
				->get();
193
194
			if ((is_array($query)) && (count($query) > 0)) {
195
				foreach ($query as $obj) {
196
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
197
					$this->niveau_batiment = $obj->niveau;
198
					$this->id_batiment = $obj->ID_batiment;
199
				}
200
201
				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...
202
					$this->niveau_batiment = $this->niveau_batiment + 1;
203
				}
204
				$niveau_batiment_base = $this->niveau_batiment;
205
				$max_level =  $this->getInfoUpgradeBatiment();
206
			}
207
			else {
208
				//on test voir si le bat est au niveau max et si il peut avoir un addon
209
				if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
210
					$query = $dbc1->select("nom_table")->from("liste_batiment")->where("nom", "=", $nom_batiment)->get();
211
212
					if ((is_array($query)) && (count($query) > 0)) {
213
						foreach ($query as $obj) {
214
							$this->nom_batiment_sql = $obj->nom_table;
215
						}
216
217
						$this->niveau_batiment = 0;
218
219
						$max_level = $this->getInfoUpgradeBatiment();
220
						$niveau_batiment_base = 10;
221
					}
222
					else {
223
						$max_level = 0;
224
					}
225
				}
226
				else {
227
					$max_level = 0;
228
				}
229
			}
230
231
			//permet de savoir si le batiment produit bien des ressoures
232
			$batiment_production = [];
233
234
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
235
236
			if ((is_array($query)) && (count($query) > 0)) {
237
				foreach ($query as $obj) {
238
					$batiment_production[] = $obj->nom;
239
				}
240
			}
241
242
			Bataille::setValues([
243
				"nom_batiment_sql" => $this->nom_batiment_sql,
244
				"niveau_batiment_base" => $niveau_batiment_base,
0 ignored issues
show
Bug introduced by
The variable $niveau_batiment_base 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...
245
				"niveau_batiment" => $this->niveau_batiment,
246
				"id_batiment" => $this->niveau_batiment,
247
				"max_level" => $max_level,
248
				"batiment_production" => $batiment_production
249
			]);
250
251
			return $max_level;
252
		}
253
254
		/**
255
		 * pour récupérer la construction en cours dans la base
256
		 */
257
		public function getConstruction() {
258
			$dbc = App::getDb();
259
260
			$today = Bataille::getToday();
261
262
			$query = $dbc->select()
263
				->from("_bataille_construction")
264
				->from("_bataille_batiment")
265
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
266
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
267
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
268
				->get();
269
270
			if ((is_array($query)) && (count($query) > 0)) {
271
				foreach ($query as $obj) {
272
					$this->nom_batiment_construction = $obj->nom_batiment;
273
					$this->date_fin_construction = $obj->date_fin;
274
					$this->niveau_batiment_construction = $obj->niveau;
275
					$id_batiment = $obj->ID_batiment;
276
				}
277
278
				if ($this->date_fin_construction-$today <= 0) {
279
					$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...
280
				}
281
				else {
282
					Bataille::setValues([
283
						"date_fin_construction" => $this->date_fin_construction-$today,
284
						"nom_batiment_construction" => $this->nom_batiment_construction
285
					]);
286
				}
287
288
				return 1;
289
			}
290
291
			return 0;
292
		}
293
294
		/**
295
		 * pour récupérer la liste des batiments qu'il est possible de construire
296
		 */
297
		public function getBatimentAConstruire() {
298
			$dbc = App::getDb();
299
			$dbc1 = Bataille::getDb();
300
			$batiment_construit = [];
301
			$batiment_construire = [];
302
303
			//recuperation des batiments deja construit dans la base
304
			$query = $dbc->select("nom_batiment_sql")
305
				->select("nom_batiment")
306
				->select("niveau")
307
				->from("_bataille_batiment")
308
				->where("ID_base", "=", Bataille::getIdBase())
309
				->get();
310
311
			if ((is_array($query)) && (count($query) > 0)) {
312
				foreach ($query as $obj) {
313
					$batiment_construit[] = $obj->nom_batiment_sql;
314
				}
315
			}
316
317
			//recuperation de la liste complete des batiments
318
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
319
			if ((is_array($query)) && (count($query) > 0)) {
320
				foreach ($query as $obj) {
321
					$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...
322
					$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...
323
				}
324
325
				$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...
326
			}
327
328
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
329
			//et compare la liste des batiments qu'il faut pour construire le batiment
330
			//a ceux qui sont deja construit dans la base
331
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
332
			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...
333
				if (!in_array($all_batiment[$i], $batiment_construit)) {
334
					$query = $dbc1->select("pour_construire")
335
						->select("temps_construction")
336
						->from($all_batiment[$i])
337
						->where("ID_".$all_batiment[$i], "=", 1)
338
						->get();
339
340
					if ((is_array($query)) && (count($query) > 0)) {
341
						foreach ($query as $obj) {
342
							if ($obj->pour_construire != null) {
343
								$pour_construire = unserialize($obj->pour_construire);
344
							}
345
							else {
346
								$pour_construire = [];
347
							}
348
349
350
							$temps_construction = gmdate("H:i:s", $obj->temps_construction);
351
						}
352
					}
353
354
355
					if (count($pour_construire) == 1) {
356
						if (in_array($pour_construire[0][1], $batiment_construit)) {
357
							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...
358
								$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
359
360
								$batiment_construire[] = [
361
									"nom_batiment_sql" => $all_batiment[$i],
362
									"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...
363
									"ressource" => $ressource,
364
									"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...
365
								];
366
							}
367
						}
368
					}
369
					else if (count($pour_construire) > 1) {
370
						$ok_construction = false;
371
						//test si tous les batiments sont construits et on le niveau nécéssaire
372
						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...
373
							if (in_array($pour_construire[$j][1], $batiment_construit)) {
374
								if ($pour_construire[$j][2] <= $this->getNiveauBatiment($pour_construire[$j][1])) {
375
									$ok_construction = true;
376
								}
377
							}
378
						}
379
380
						//si ok on affiche le batiment
381 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...
382
							$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
383
384
							$batiment_construire[] = [
385
								"nom_batiment_sql" => $all_batiment[$i],
386
								"nom_batiment" => $all_batiment_nom[$i],
387
								"ressource" => $ressource,
388
								"temps_construction" => $temps_construction
389
							];
390
						}
391
					}
392 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...
393
						$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
394
395
						$batiment_construire[] = [
396
							"nom_batiment_sql" => $all_batiment[$i],
397
							"nom_batiment" => $all_batiment_nom[$i],
398
							"ressource" => $ressource,
399
							"temps_construction" => $temps_construction
400
						];
401
					}
402
				}
403
			}
404
			Bataille::setValues(["batiments" => $batiment_construire]);
405
		}
406
407
		/**
408
		 * @param $nom_batiment_sql
409
		 * @param $niveau
410
		 * @return array
411
		 * recuperation des ressources nécéssaire pour construire le batiment
412
		 */
413
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
414
			$dbc1 = Bataille::getDb();
415
416
			$niveau = $niveau+1;
417
418
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
419
420
			if ((is_array($query)) && (count($query) > 0)) {
421
				foreach ($query as $obj) {
422
					$ressource_tmp = $obj->ressource_construire;
423
				}
424
				$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...
425
426
				//on test si assez de ressources dans la base
427
				//fer
428
				$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...
429
				//fuel
430
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[1]);
431
				//eau
432
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[2]);
433
				//electricite
434
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[3]);
435
436
				return $ressource;
437
			}
438
		}
439
440
		/**
441
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
442
		 * + true pour dire que le batiment est en construction
443
		 *
444
		 * @param $nom_batiment
445
		 * @return array
446
		 */
447
		private function getTestBatimentConstruction($nom_batiment) {
448
			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...
449
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
450
			}
451
452
			return [$nom_batiment, false];
453
		}
454
455
		/**
456
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
457
		 * ou la création d'un addon sur ce batiment
458
		 * @return int
459
		 */
460
		private function getInfoUpgradeBatiment() {
461
			$dbc1 = Bataille::getDb();
462
463
			//récupération du temps et des ressources pour construire
464
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
465
466
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
467
			if ((is_array($query)) && (count($query) > 0)) {
468
				foreach ($query as $obj) {
469
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
470
					$this->temps_construction = gmdate("H:i:s", $obj->temps_construction);
471
				}
472
473
				//récupération des éléments particulier à un batiment
474
				$xml = simplexml_load_file(MODULEROOT.'bataille/data/batiment.xml');
475
				$nom_batiment_sql = $this->nom_batiment_sql;
476
				$champ = $xml->$nom_batiment_sql->champ;
477
478
				if (!empty($champ)) {
479
					//récupération de la phrase pour le niveau actuel
480
					$query = $dbc1->select($xml->$nom_batiment_sql->champ)
481
						->from($this->nom_batiment_sql)
482
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment)
483
						->get();
484
485 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...
486
						foreach ($query as $obj) {
487
							$this->info_batiment = $xml->$nom_batiment_sql->phrase.$obj->$champ;
488
						}
489
					}
490
491
					//récupération de la phrase pour le niveau suivant
492
					$query = $dbc1->select($xml->$nom_batiment_sql->champ)
493
						->from($this->nom_batiment_sql)
494
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
495
						->get();
496
497 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...
498
						foreach ($query as $obj) {
499
							$this->info_batiment_next = $xml->$nom_batiment_sql->phrase_suivant.$obj->$champ;
500
						}
501
					}
502
				}
503
				else {
504
					$this->info_batiment = "";
505
					$this->info_batiment_next = "";
506
				}
507
508
509
				Bataille::setValues([
510
					"ressource" => $this->ressource_construire,
511
					"temps_construction" => $this->temps_construction,
512
					"info_batiment" => $this->info_batiment,
513
					"info_batiment_next" => $this->info_batiment_next,
514
				]);
515
516
				return 1;
517
			}
518
519
			return 0;
520
		}
521
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
522
523
524
525
		//-------------------------- SETTER ----------------------------------------------------------------------------//
526
		/**
527
		 * fonction qui initialise la construction d'un batiment
528
		 * @param $nom_batiment
529
		 * @param $nom_batiment_sql
530
		 * @param $emplacement
531
		 */
532
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $emplacement) {
533
			$dbc = App::getDb();
534
			$dbc1 = Bataille::getDb();
535
536
			if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
537
				$emplacement = 0;
538
			}
539
540
			if ($this->getConstruction() == 0) {
541
				$un_batiment = $this->getUnBatiment($nom_batiment, $emplacement);
542
543
				//on test si assez de ressrouce pour construire le batiment
544
				if ($un_batiment == 0) {
545
					$ressource = $this->getRessourceConstruireBatiment($nom_batiment_sql, 0);
546
					$this->nom_batiment_sql = $nom_batiment_sql;
547
					$this->niveau_batiment = 0;
548
				}
549
				else {
550
					//si c'est le lvl 0 de l'addon
551
					if ($this->niveau_batiment == 0) {
552
						$un_batiment = 0;
553
					}
554
					$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
555
				}
556
557
				//si pas assez de ressource
558
				if (in_array("rouge", $ressource[0])) {
559
					FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
560
				}
561
				else {
562
					//recuperation du temps de construction
563
					$query = $dbc1->select("ressource_construire")
564
						->select("temps_construction")
565
						->from($this->nom_batiment_sql)
566
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
567
						->get();
568
569
					foreach ($query as $obj) {
570
						$temps_construction = $obj->temps_construction;
571
						$ressource_construction = explode(", ", $obj->ressource_construire);
572
					}
573
574
					//on insere la construction dans la table batiment si new batiment
575
					if ($un_batiment == 0) {
576
						$dbc->insert("niveau", $this->niveau_batiment+1)
577
							->insert("emplacement", $emplacement)
578
							->insert("nom_batiment", $nom_batiment)
579
							->insert("nom_batiment_sql", $this->nom_batiment_sql)
580
							->insert("construction", 1)
581
							->insert("ID_base", Bataille::getIdBase())
582
							->into("_bataille_batiment")
583
							->set();
584
585
						$this->id_batiment = $dbc->lastInsertId();
586
					}
587
					else {
588
						$dbc->update("niveau", $this->niveau_batiment+1)
589
							->update("construction", 1)
590
							->from("_bataille_batiment")
591
							->where("ID_batiment", "=", $this->id_batiment, "AND")
592
							->where("ID_base", "=", Bataille::getIdBase())
593
							->set();
594
					}
595
596
597
					//on initialise la construction
598
					//recuperation de la date en seconde
599
					$today = Bataille::getToday();
600
601
					//date de la fin de la construction en seconde
602
					$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...
603
604
					$dbc->insert("date_fin", $fin_construction)
605
						->insert("emplacement_construction", $emplacement)
606
						->insert("ID_base", Bataille::getIdBase())
607
						->insert("ID_batiment", $this->id_batiment)
608
						->into("_bataille_construction")
609
						->set();
610
611
					//on retire les ressources de la base
612
					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...
613
				}
614
			}
615
			else {
616
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
617
			}
618
		}
619
620
		/**
621
		 * fonction qui termine la construction d'un batiment
622
		 * @param $id_batiment
623
		 */
624
		private function setTerminerConstruction($id_batiment) {
625
			$dbc = App::getDb();
626
627
			//on le retire de la table construction
628
			$dbc->delete()
629
				->from("_bataille_construction")
630
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
631
				->where("ID_batiment", "=", $id_batiment)
632
				->del();
633
634
			//on termine la construction dans la table batiment
635
			$dbc->update("construction", 0)
636
				->from("_bataille_batiment")
637
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
638
				->where("ID_batiment", "=", $id_batiment)
639
				->set();
640
			
641
			//on ajoute les points à la base
642
			Points::setAjouterPoints(Bataille::getIdBase(), "batiment");
643
		}
644
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
645
	}