Passed
Push — master ( ab8373...e7b2e8 )
by Anthony
02:31
created

CentreRecherche   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 303
Duplicated Lines 3.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 35
c 2
b 0
f 0
lcom 1
cbo 1
dl 11
loc 303
rs 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getNiveauRecherche() 0 18 4
A getCoutRecherche() 0 20 1
A getTempsRecherche() 0 9 2
B getAllRechercheType() 0 22 4
B getAllRecherche() 7 42 6
B getRecherche() 0 34 5
C setCommencerRecherche() 4 61 10
B setTerminerRecherche() 0 25 2

How to fix   Duplicated Code   

Duplicated Code

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

Common duplication problems, and corresponding solutions are:

1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\DateHeure;
7
	use core\HTML\flashmessage\FlashMessage;
8
9
	class CentreRecherche {
10
		private $coef_centre;
11
		private $recherche;
12
		private $type;
13
		
14
		
15
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
16
		public function __construct() {
17
			$this->coef_centre = Bataille::getParam("coef_centre_recherche");
18
		}
19
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
20
		
21
		
22
		
23
		//-------------------------- GETTER ----------------------------------------------------------------------------//
24
		/**
25
		 * @param $recherche
26
		 * @param $type
27
		 * @return int
28
		 * fonction qui va cehrcher le niveau de la recherche actuelle
29
		 * renvoi 0 si elle n'a pas été trouvée
30
		 */
31
		private function getNiveauRecherche($recherche, $type) {
32
			$dbc = App::getDb();
33
34
			$query = $dbc->select("niveau")
35
				->from("_bataille_centre_recherche")
36
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
37
				->where("recherche", "=", $recherche, "AND")
38
				->where("type", "=", $type)
39
				->get();
40
41
			if ((is_array($query)) && (count($query) > 0)) {
42
				foreach ($query as $obj) {
43
					return $obj->niveau;
44
				}
45
			}
46
47
			return 0;
48
		}
49
50
		/**
51
		 * @param $cout
52
		 * @param integer $niveau_recherche
53
		 * @return array
54
		 * fonction qui renvoi le cout d'une recherche
55
		 */
56
		private function getCoutRecherche($cout, $niveau_recherche) {
57
			$cout_eau = $cout["eau"] * ($this->coef_centre * $niveau_recherche);
58
			$cout_eau = Bataille::getTestAssezRessourceBase("eau", $cout_eau);
59
			
60
			$cout_electricite = $cout["electricite"] * ($this->coef_centre * $niveau_recherche);
61
			$cout_electricite = Bataille::getTestAssezRessourceBase("electricite", $cout_electricite);
62
			
63
			$cout_fer = $cout["fer"] * ($this->coef_centre * $niveau_recherche);
64
			$cout_fer = Bataille::getTestAssezRessourceBase("fer", $cout_fer);
65
			
66
			$cout_fuel = $cout["fuel"] * ($this->coef_centre * $niveau_recherche);
67
			$cout_fuel = Bataille::getTestAssezRessourceBase("fuel", $cout_fuel);
68
			
69
			return [
70
				"eau" => $cout_eau,
71
				"electricite" => $cout_electricite,
72
				"fer" => $cout_fer,
73
				"fuel" => $cout_fuel
74
			];
75
		}
76
77
		/**
78
		 * @param $temps
79
		 * @param int $niveau
80
		 * @return double fonction qui renvoi le temps qu'il faut pour effectuer une recherche
81
		 */
82
		private function getTempsRecherche($temps, $niveau = 0) {
83
			$pourcent = ($temps*Bataille::getBatiment()->getNiveauBatiment("centre_recherche")/100);
84
85
			if ($niveau == 0) {
86
				return round($temps-$pourcent);;
87
			}
88
89
			return round(($temps * ($this->coef_centre * $niveau))-$pourcent);
90
		}
91
92
		/**
93
		 * @param $type
94
		 * @return array|int
95
		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction
96
		 * d'un type donné
97
		 */
98
		public function getAllRechercheType($type) {
99
			$dbc = App::getDb();
100
			$recherche = [];
101
102
			$query = $dbc->select()->from("_bataille_centre_recherche")
103
				->where("type", "=", $type, "AND")
104
				->where("ID_base", "=", Bataille::getIdBase())
105
				->get();
106
107
			if ((is_array($query)) && (count($query) > 0)) {
108
				foreach ($query as $obj) {
109
					$recherche[] = [
110
						"niveau" => $obj->niveau,
111
						"recherche" => $obj->recherche
112
					];
113
				}
114
115
				return $recherche;
116
			}
117
118
			return 0;
119
		}
120
121
		/**
122
		 * fonction qui renvoi toutes les recherches effectuées ou non dans un tableau
123
		 * (ne renvoi que celle que l'on peut faire en fonction du niveau du centre)
124
		 */
125
		public function getAllRecherche() {
126
			$dbc1 = Bataille::getDb();
127
128
			//avant de récupérer toutes les recherches, on finit au cas celle en court
129
			if ($this->getRecherche() == false) {
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...
130
				$query = $dbc1->select()->from("recherche")
131
					->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche"))
132
					->get();
133
134
				if ((is_array($query)) && (count($query) > 0)) {
135
					$recherche = [];
136
					foreach ($query as $obj) {
137
						$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type);
138
						$niveau_recherche = $niveau;
139
140
						$cout = unserialize($obj->cout);
141
						$temps_recherche = $this->getTempsRecherche($obj->temps_recherche);
142
143
						//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base
144 View Code Duplication
						if ($niveau > 0) {
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...
145
							$cout = $this->getCoutRecherche($cout, $niveau);
146
							$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau);
147
						}
148
						else {
149
							$niveau_recherche = 1;
150
						}
151
						
152
						$recherche[] = [
153
							"recherche" => $obj->recherche,
154
							"type" => $obj->type,
155
							"niveau" => $niveau,
156
							"cout" => $cout,
157
							"temps_recherche" => DateHeure::Secondeenheure($temps_recherche),
158
							"special" => Bataille::getUnite()->getCaracteristiqueUnite($obj->recherche, $niveau_recherche, $obj->type),
159
							"coef_amelioration" => Bataille::getParam("coef_niveau_unite")
160
						];
161
					}
162
				}
163
164
				Bataille::setValues(["centre_recherche" => $recherche]);
0 ignored issues
show
Bug introduced by
The variable $recherche 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...
165
			}
166
		}
167
168
		/**
169
		 * @return bool
170
		 * fonction qui renvoi un tableau contenant la recherche en cours si celle-ci n'est pas finie
171
		 * sinon elle appelle la fonction setTerminerRecherche
172
		 */
173
		public function getRecherche() {
174
			$dbc = App::getDb();
175
176
			$query = $dbc->select()->from("_bataille_recherche")->where("ID_base", "=", Bataille::getIdBase())->get();
177
178
			if ((is_array($query)) && (count($query) > 0)) {
179
				$today = Bataille::getToday();
180
181
				foreach ($query as $obj) {
182
					$this->recherche = $obj->recherche;
183
					$this->type = $obj->type;
184
185
					if ($obj->date_fin-$today <= 0) {
186
						$this->setTerminerRecherche($obj->ID_recherche);
187
188
						return false;
189
					}
190
					else {
191
						$recherche = [
192
							"recherche" => $obj->recherche,
193
							"type" => $obj->type,
194
							"date_fin_recherche" => $obj->date_fin-$today,
195
							"id_recherche" => $obj->ID_recherche
196
						];
197
					}
198
				}
199
200
				Bataille::setValues(["recherche" => $recherche]);
0 ignored issues
show
Bug introduced by
The variable $recherche 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...
201
202
				return true;
203
			}
204
205
			return false;
206
		}
207
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
208
		
209
		
210
		
211
		//-------------------------- SETTER ----------------------------------------------------------------------------//
212
		/**
213
		 * @param $recherche
214
		 * @param $type
215
		 * @return bool
216
		 * fonction qui va initialiser la recherche en question
217
		 */
218
		public function setCommencerRecherche($recherche, $type) {
219
			$dbc = App::getDb();
220
			$dbc1 = Bataille::getDb();
221
222
			//on test si il n'y a pas déjà une recherche en cours
223
			if ($this->getRecherche() == 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...
224
				FlashMessage::setFlash("Une recherche est déjà en cours, merci d'attendre la fin de celle-ci");
225
				return false;
226
			}
227
228
			//on récupère la recherche dans notre base savoir si on l'a déjà recherchée pour avoir son lvl
229
			$niveau_recherche = $this->getNiveauRecherche($recherche, $type);
230
231
			//récupération du cout initial plus temps de recherche initial pour calculer les bon en fonction
232
			//du lvl du centre + du niveau actuel de la recherche
233
			$query = $dbc1->select("cout")
234
				->select("temps_recherche")
235
				->from("recherche")
236
				->where("recherche", "=", $recherche, "AND")
237
				->where("type", "=", $type)
238
				->get();
239
240
			if ((is_array($query)) && (count($query) == 1)) {
241
				foreach ($query as $obj) {
242
					$cout = unserialize($obj->cout);
243
					$temps_recherche = $obj->temps_recherche;
244
				}
245
			}
246
247 View Code Duplication
			if ($niveau_recherche > 0) {
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...
248
				$cout = $this->getCoutRecherche($cout, $niveau_recherche);
0 ignored issues
show
Bug introduced by
The variable $cout 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...
249
				$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau_recherche);
0 ignored issues
show
Bug introduced by
The variable $temps_recherche 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...
250
			}
251
252
			//on test si assez de ressources pour effectuer la recherche
253
			$eau = Bataille::getTestAssezRessourceBase("eau", $cout["eau"]["ressource"]);
254
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $cout["electricite"]["ressource"]);
255
			$fer = Bataille::getTestAssezRessourceBase("fer", $cout["fer"]["ressource"]);
256
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $cout["fuel"]["ressource"]);
257
258
259
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
260
				FlashMessage::setFlash("Pas assez de ressources pour effectuer cette recherche");
261
				return false;
262
			}
263
			else {
264
				//on retire les ressources
265
				Bataille::getRessource()->setUpdateRessource($cout["eau"]["ressource"], $cout["electricite"]["ressource"], $cout["fer"]["ressource"], $cout["fuel"]["ressource"], 0, "-");
266
267
				$date_fin = Bataille::getToday()+$temps_recherche;
268
269
				$dbc->insert("recherche", $recherche)
270
					->insert("type", $type)
271
					->insert("date_fin", $date_fin)
272
					->insert("ID_base", Bataille::getIdBase())
273
					->into("_bataille_recherche")
274
					->set();
275
276
				return true;
277
			}
278
		}
279
280
		/**
281
		 * @param $id_recherche
282
		 * fonction qui va terminer une recherche en fonction de son ID
283
		 */
284
		private function setTerminerRecherche($id_recherche) {
285
			$dbc = App::getDb();
286
			$niveau_recherche = $this->getNiveauRecherche($this->recherche, $this->type);
287
288
			if ($niveau_recherche == 0) {
289
				$dbc->insert("recherche", $this->recherche)
290
					->insert("type", $this->type)
291
					->insert("niveau", 1)
292
					->insert("ID_base", Bataille::getIdBase())
293
					->into("_bataille_centre_recherche")
294
					->set();
295
			}
296
			else {
297
				$dbc->update("niveau", $niveau_recherche+1)
298
					->from("_bataille_centre_recherche")
299
					->where("recherche", "=", $this->recherche, "AND")
300
					->where("type", "=", $this->type, "AND")
301
					->where("ID_base", "=", Bataille::getIdBase())
302
					->set();
303
			}
304
305
			$dbc->delete()->from("_bataille_recherche")->where("ID_recherche", "=", $id_recherche, "AND")
306
				->where("ID_base", "=", Bataille::getIdBase())
307
				->del();
308
		}
309
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
310
		
311
	}