Passed
Push — master ( 95951e...f270a7 )
by Anthony
02:23
created

CentreRecherche::getNiveauRecherche()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
rs 9.2
cc 4
eloc 12
nc 3
nop 2
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
7
	class CentreRecherche {
8
		private $coef_centre;
9
		
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		public function __construct() {
13
			$dbc1 = Bataille::getDb();
14
15
			$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get();
16
17
			if ((is_array($query)) && (count($query) == 1)) {
18
				foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche;
19
			}
20
21
			$query = $dbc1->select()->from("recherche")
22
				->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche"))
23
				->get();
24
25
			if ((is_array($query)) && (count($query) > 0)) {
26
				foreach ($query as $obj) {
27
					$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type);
28
29
					$cout = unserialize($obj->cout);
30
31
					//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base
32
					if ($niveau > 0) {
33
						$cout = [
34
							"eau" => $cout["eau"] * ($this->coef_centre * $niveau),
35
							"electricite" => $cout["electricite"] * ($this->coef_centre * $niveau),
36
							"fer" => $cout["fer"] * ($this->coef_centre * $niveau),
37
							"fuel" => $cout["fuel"] * ($this->coef_centre * $niveau)
38
						];
39
					}
40
41
					$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...
42
						"recherche" => $obj->recherche,
43
						"type" => $obj->type,
44
						"niveau" => $niveau,
45
						"cout" => $cout
46
					];
47
				}
48
			}
49
50
			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...
51
		}
52
53
		/**
54
		 * @param $recherche
55
		 * @param $type
56
		 * @return int
57
		 * fonction qui va cehrcher le niveau de la recherche actuelle
58
		 * renvoi 0 si elle n'a pas été trouvée
59
		 */
60 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...
61
			$dbc = App::getDb();
62
63
			$query = $dbc->select("niveau")
64
				->from("_bataille_centre_recherche")
65
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
66
				->where("recherche", "=", $recherche, "AND")
67
				->where("type", "=", $type)
68
				->get();
69
70
			if ((is_array($query)) && (count($query) > 0)) {
71
				foreach ($query as $obj) {
72
					return $obj->niveau;
73
				}
74
			}
75
76
			return 0;
77
		}
78
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
79
		
80
		
81
		
82
		//-------------------------- GETTER ----------------------------------------------------------------------------//
83
		/**
84
		 * @param $type
85
		 * @return array|int
86
		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction
87
		 * d'un type donné
88
		 */
89
		public function getAllRechercheType($type) {
90
			$dbc = App::getDb();
91
92
			$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get();
93
94
			if ((is_array($query)) && (count($query) > 0)) {
95
				foreach ($query as $obj) {
96
					$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...
97
						"niveau" => $obj->niveau,
98
						"recherche" => $obj->recherche
99
					];
100
				}
101
102
				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...
103
			}
104
105
			return 0;
106
		}
107
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
108
		
109
		
110
		
111
		//-------------------------- SETTER ----------------------------------------------------------------------------//
112
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
113
		
114
	}