Passed
Push — master ( 5c0550...ffd9ca )
by Anthony
02:19
created

CentreRecherche::getTempsRecherche()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\DateHeure;
7
8
	class CentreRecherche {
9
		private $coef_centre;
10
		
11
		
12
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
13
		public function __construct() {
14
			$dbc1 = Bataille::getDb();
15
16
			$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get();
17
18
			if ((is_array($query)) && (count($query) == 1)) {
19
				foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche;
20
			}
21
22
			$query = $dbc1->select()->from("recherche")
23
				->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche"))
24
				->get();
25
26
			if ((is_array($query)) && (count($query) > 0)) {
27
				foreach ($query as $obj) {
28
					$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type);
29
					$niveau_recherche = $niveau;
30
31
					$cout = unserialize($obj->cout);
32
					$temps_recherche = $this->getTempsRecherche($obj->temps_recherche);
33
34
					//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base
35 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...
36
						$cout = $this->getCoutRecherche($cout, $niveau);
37
						$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau);
38
					}
39
					else {
40
						$niveau_recherche = 1;
41
					}
42
43
					$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...
44
						"recherche" => $obj->recherche,
45
						"type" => $obj->type,
46
						"niveau" => $niveau,
47
						"cout" => $cout,
48
						"temps_recherche" => DateHeure::Secondeenheure($temps_recherche),
49
						"special" => Bataille::getUnite()->getCaracteristiqueUnite($obj->recherche, $niveau_recherche, $obj->type),
50
						"coef_amelioration" => Bataille::getParam("coef_niveau_unite")
51
					];
52
				}
53
			}
54
55
			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...
56
		}
57
58
		/**
59
		 * @param $recherche
60
		 * @param $type
61
		 * @return int
62
		 * fonction qui va cehrcher le niveau de la recherche actuelle
63
		 * renvoi 0 si elle n'a pas été trouvée
64
		 */
65 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...
66
			$dbc = App::getDb();
67
68
			$query = $dbc->select("niveau")
69
				->from("_bataille_centre_recherche")
70
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
71
				->where("recherche", "=", $recherche, "AND")
72
				->where("type", "=", $type)
73
				->get();
74
75
			if ((is_array($query)) && (count($query) > 0)) {
76
				foreach ($query as $obj) {
77
					return $obj->niveau;
78
				}
79
			}
80
81
			return 0;
82
		}
83
84
		private function getCoutRecherche($cout, $niveau_recherche) {
85
			return [
86
				"eau" => $cout["eau"] * ($this->coef_centre * $niveau_recherche),
87
				"electricite" => $cout["electricite"] * ($this->coef_centre * $niveau_recherche),
88
				"fer" => $cout["fer"] * ($this->coef_centre * $niveau_recherche),
89
				"fuel" => $cout["fuel"] * ($this->coef_centre * $niveau_recherche)
90
			];
91
		}
92
93
		private function getTempsRecherche($temps, $niveau = 0) {
94
			$pourcent = ($temps*Bataille::getBatiment()->getNiveauBatiment("centre_recherche")/100);
95
96
			if ($niveau == 0) {
97
				return round($temps-$pourcent);;
98
			}
99
100
			return round(($temps * ($this->coef_centre * $niveau))-$pourcent);
101
		}
102
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
103
		
104
		
105
		
106
		//-------------------------- GETTER ----------------------------------------------------------------------------//
107
		/**
108
		 * @param $type
109
		 * @return array|int
110
		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction
111
		 * d'un type donné
112
		 */
113
		public function getAllRechercheType($type) {
114
			$dbc = App::getDb();
115
116
			$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get();
117
118
			if ((is_array($query)) && (count($query) > 0)) {
119
				foreach ($query as $obj) {
120
					$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...
121
						"niveau" => $obj->niveau,
122
						"recherche" => $obj->recherche
123
					];
124
				}
125
126
				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...
127
			}
128
129
			return 0;
130
		}
131
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
132
		
133
		
134
		
135
		//-------------------------- SETTER ----------------------------------------------------------------------------//
136
		public function setCommencerRecherche($recherche, $type) {
137
			$dbc = App::getDb();
138
			$dbc1 = Bataille::getDb();
139
			$niveau_recherche = 0;
140
141
			//on récupère la recherche dans notre base savoir si on l'a déjà recherchée pour avoir son lvl
142
			$query = $dbc->select("niveau")->from("_bataille_centre_recherche")
143
				->where("recherche", "=", $recherche, "AND")
144
				->where("type", "=", $type, "AND")
145
				->where("ID_base", "=", Bataille::getIdBase())
146
				->get();
147
148
			if ((is_array($query)) && (count($query) == 1)) {
149
				foreach ($query as $obj) $niveau_recherche = $obj->niveau;
150
			}
151
152
			//récupération du cout initial plus temps de recherche initial pour calculer les bon en fonction
153
			//du lvl du centre + du niveau actuel de la recherche
154
			$query = $dbc1->select("cout")
155
				->select("temps_recherche")
156
				->from("recherche")
157
				->where("recherche", "=", $recherche, "AND")
158
				->where("type", "=", $type)
159
				->get();
160
161
			if ((is_array($query)) && (count($query) == 1)) {
162
				foreach ($query as $obj) {
163
					$cout = $obj->cout;
164
					$temps_recherche = $obj->temps_recherche;
165
				}
166
			}
167
168 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...
169
				$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...
Unused Code introduced by
$cout is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
170
				$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...
171
			}
172
173
			$date_fin = Bataille::getToday()+$temps_recherche;
174
175
			$dbc->insert("recherche", $recherche)
176
				->insert("type", $type)
177
				->insert("date_fin", $date_fin)
178
				->insert("ID_base", Bataille::getIdBase())
179
				->into("_bataille_recherche")
180
				->set();
181
		}
182
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
183
		
184
	}