Passed
Push — master ( 78d9ba...e105a7 )
by Anthony
02:56
created

Base::getBasesJoueur()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 38
Code Lines 25

Duplication

Lines 13
Ratio 34.21 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 38
rs 5.3846
cc 8
eloc 25
nc 14
nop 1
1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
5
	class Base {
6
		private $batiments;
7
8
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
9
		public function __construct() {
10
11
		}
12
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
13
14
15
16
		//-------------------------- GETTER ----------------------------------------------------------------------------//
17
		public function getBatiments() {
18
			return $this->batiments;
19
		}
20
21
		/**
22
		 * fonction qui va initaliser la base
23
		 */
24
		public function getMaBase() {
25
			$this->getInfoBase();
26
			
27
			$this->getBasesJoueur();
28
29
			Bataille::getRessource();
30
31
			Bataille::setValues([
32
				"production_eau" => Bataille::getBatiment()->getProduction("eau"),
33
				"production_electricite" => Bataille::getBatiment()->getProduction("electricite"),
34
				"production_fer" => Bataille::getBatiment()->getProduction("fer"),
35
				"production_fuel" => Bataille::getBatiment()->getProduction("fuel"),
36
				"stockage_entrepot" => Bataille::getBatiment()->getStockage(),
37
				"stockage_grenier" => Bataille::getBatiment()->getStockage("grenier")
38
			]);
39
		}
40
41
		/**
42
		 * @param null $id_base
43
		 * fonction qui va récupérer les infos de la base
44
		 */
45
		public function getInfoBase($id_base = null) {
46
			$dbc = App::getDb();
47
48
			if ($id_base == null) {
49
				$id_base = Bataille::getIdBase();
50
			}
51
52
			$query = $dbc->select("nom_base")->select("points")->from("_bataille_base")->where("ID_base", "=", $id_base)->get();
53
54
			if ((is_array($query)) && (count($query) > 0)) {
55
				foreach ($query as $obj) {
56
					Bataille::setValues([
57
						"nom_base" => $obj->nom_base,
58
						"points" => $obj->points
59
					]);
60
				}
61
			}
62
		}
63
64
		/**
65
		 * fonction qui recupere tous les batiments de la base
66
		 */
67
		public function getBatimentsBase() {
68
			$dbc = App::getDb();
69
70
			$query = $dbc->select()->from("_bataille_batiment")->where("ID_base", "=", Bataille::getIdBase())->get();
71
72
			if (count($query) > 0) {
73
				$batiments = [];
74
				
75
				foreach ($query as $obj) {
76
					$taille_batiment = Bataille::getBatiment()->getTailleBatiment($obj->nom_batiment_sql);
77
					$construction = "";
78
79
					if ($obj->construction) {
80
						$construction = "construction";
81
					}
82
83
					$batiments[] = [
84
						"nom_batiment" => $obj->nom_batiment,
85
						"nom_batiment_sql" => $obj->nom_batiment_sql,
86
						"niveau" => $obj->niveau,
87
						"posx" => $obj->posx,
88
						"posy" => $obj->posy,
89
						"width" => $taille_batiment[0],
90
						"height" => $taille_batiment[1],
91
						"construction" => $construction
92
					];
93
				}
94
				
95
				Bataille::setValues(["batiments" => $batiments]);
96
				
97
				$this->setBatimentsBase($batiments);
98
			}
99
		}
100
		
101
		/**
102
		 * @param null $id_identite
103
		 * fonction qui récupère les bases d'un joueur
104
		 */
105
		public function getBasesJoueur($id_identite = null) {
106
			$dbc = App::getDb();
107
			$exclude = 0;
108
			
109
			if ($id_identite === null) {
110
				$id_identite = Bataille::getIdIdentite();
111
				$exclude = Bataille::getIdBase();
112
			}
113
			
114
			$query = $dbc->select("pseudo")->from("identite")->where("ID_identite", "=", $id_identite)->get();
115
			
116
			if ((is_array($query)) && (count($query) == 1)) {
117
				foreach ($query as $obj) {
118
					Bataille::setValues(["pseudo" => $obj->pseudo]);
119
				}
120
				
121
				$query = $dbc->select()
122
					->from("_bataille_base")
123
					->where("ID_identite", "=", $id_identite, "AND")
124
					->where("ID_base", "!=", $exclude)
125
					->orderBy("ID_base", "DESC")
126
					->get();
127
				
128 View Code Duplication
				if ((is_array($query)) && (count($query) > 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...
129
					foreach ($query as $obj) {
130
						$une_base[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$une_base was never initialized. Although not strictly required by PHP, it is generally a good practice to add $une_base = 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...
131
							"id_base" => $obj->ID_base,
132
							"nom_base" => $obj->nom_base,
133
							"points_base" => $obj->points,
134
							"posx" => $obj->posx,
135
							"posy" => $obj->posy
136
						];
137
					}
138
					
139
					Bataille::setValues(["base_joueur" => $une_base]);
0 ignored issues
show
Bug introduced by
The variable $une_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...
140
				}
141
			}
142
		}
143
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
144
145
146
147
		//-------------------------- SETTER ----------------------------------------------------------------------------//
148
		private function setBatimentsBase($batiments) {
149
			$this->batiments = $batiments;
150
		}
151
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
152
	}