Passed
Push — master ( 7c57b8...164b78 )
by Anthony
02:45
created

CentreRecherche   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 282
Duplicated Lines 13.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 38
c 10
b 0
f 0
lcom 1
cbo 1
dl 38
loc 282
rs 8.3999

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 4
A getNiveauRecherche() 18 18 4
A getCoutRecherche() 0 8 1
A getTempsRecherche() 0 9 2
A getAllRechercheType() 0 18 4
B getAllRecherche() 7 41 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 View Code Duplication
		public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
			$dbc1 = Bataille::getDb();
18
19
			$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get();
20
21
			if ((is_array($query)) && (count($query) == 1)) {
22
				foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche;
23
			}
24
		}
25
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
26
		
27
		
28
		
29
		//-------------------------- GETTER ----------------------------------------------------------------------------//
30
		/**
31
		 * @param $recherche
32
		 * @param $type
33
		 * @return int
34
		 * fonction qui va cehrcher le niveau de la recherche actuelle
35
		 * renvoi 0 si elle n'a pas été trouvée
36
		 */
37 View Code Duplication
		private function getNiveauRecherche($recherche, $type) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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