Points::getPointAjoutBatiment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
4
	use core\App;
5
	use core\HTML\flashmessage\FlashMessage;
6
	
7
	
8
	class Points {
9
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
10
		public function __construct($start = null) {
11
			$dbc = App::getDb();
12
			
13
			if ($start === null) $start = 0;
14
			
15
			$query = $dbc->select("identite.pseudo")
16
				->select("_bataille_infos_player.points")
17
				->select("_bataille_infos_player.ID_identite")
18
				->from("_bataille_infos_player")
19
				->from("identite")
20
				->where("abandon", "!=", 1, "AND")
21
				->where("_bataille_infos_player.ID_identite", "=", "identite.ID_identite", "", true)
22
				->orderBy("_bataille_infos_player.points", "DESC")
23
				->limit($start, 50)
24
				->get();
25
			
26 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
27
				$count = 1;
28
				foreach ($query as $obj) {
29
					$values[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = 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...
30
						"id_identite" => $obj->ID_identite,
31
						"pseudo" => $obj->pseudo,
32
						"points" => $obj->points,
33
						"classement" => $count
34
					];
35
					
36
					$count++;
37
				}
38
				
39
				Bataille::setValues(["classement" => $values]);
0 ignored issues
show
Bug introduced by
The variable $values 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...
40
			}
41
			
42
		}
43
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
44
		
45
		
46
		//-------------------------- GETTER ----------------------------------------------------------------------------//
47
		/**
48
		 * @param $id_base
49
		 * @return int
50
		 * renvoi les points de la base
51
		 */
52
		public static function getPointsBase($id_base) {
0 ignored issues
show
Coding Style introduced by
getPointsBase uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
			$dbc = App::getDb();
54
			
55
			//on récupère les points de la base en cours
56
			$query = $dbc->select("points")
57
				->from("_bataille_base")
58
				->where("ID_base", "=", $id_base, "AND")
59
				->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])
60
				->get();
61
			
62
			if ((is_array($query)) && (count($query) > 0)) {
63
				foreach ($query as $obj) {
64
					return $obj->points;
65
				}
66
			}
67
			
68
			return 0;
69
		}
70
		
71
		/**
72
		 * @param null $id_identite
73
		 * @return int
74
		 * renvoi les points totaux d'un joueur
75
		 */
76
		public static function getPointsJoueur($id_identite=null) {
77
			$dbc = App::getDb();
78
			
79
			if ($id_identite === null) {
80
				$id_identite = Bataille::getIdIdentite();
81
			}
82
			
83
			$query = $dbc->select("points")->from("_bataille_infos_player")->where("ID_identite", "=", $id_identite)->get();
84
			
85
			if (count($query) == 1) {
86
				foreach ($query as $obj) {
87
					return $obj->points;
88
				}
89
			}
90
			
91
			return 0;
92
		}
93
		
94
		/**
95
		 * @return mixed
96
		 * fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment
97
		 */
98
		private static function getPointAjoutBatiment() {
99
			return Bataille::getParam("points_batiment");
100
		}
101
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
102
		
103
		
104
		//-------------------------- SETTER ----------------------------------------------------------------------------//
105
		/**
106
		 * @param $id_base
107
		 * @param null $type
108
		 * @param null $points
109
		 * @return int|null
110
		 * fonction qui ajoute des points à la base en fonction du type
111
		 * le type peut etre : batiment, attaque, defense, troupe
112
		 */
113
		public static function setAjouterPoints($id_base, $type=null, $points=null) {
114
			$dbc = App::getDb();
115
116
			if ($type == "batiment") {
117
				$points_faction = self::getPointAjoutBatiment();
118
				$points = self::getPointsBase($id_base)+self::getPointAjoutBatiment();
119
			}
120
			
121
			$dbc->update("points", $points)
122
				->from("_bataille_base")
123
				->where("ID_base", "=", $id_base)
124
				->set();
125
			
126
			self::setAjouterPointsTotaux();
127
			self::setAjouterPointsFaction($points_faction);
0 ignored issues
show
Bug introduced by
The variable $points_faction 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...
128
			
129
			return $points;
130
		}
131
		
132
		/**
133
		 * fonction qui prend les points de toutes les bases et qui les ajoute sur le joueur en lui même
134
		 */
135
		private static function setAjouterPointsTotaux() {
136
			$dbc = App::getDb();
137
			
138
			$query = $dbc->select("points")->from("_bataille_base")->where("ID_identite", "=", Bataille::getIdIdentite())->get();
139
			
140
			if ((is_array($query)) && (count($query) > 0)) {
141
				$points = 0;
142
				
143
				foreach ($query as $obj) {
144
					$points += $obj->points;
145
				}
146
				
147
				$dbc->update("points", $points)->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
148
			}
149
		}
150
		
151
		/**
152
		 * fonction qui permet d'ajouter tous les points du joueur aux points de la faction
153
		 */
154
		public static function setRejoindreQuitterFaction($del = null) {
155
			$dbc = App::getDb();
156
			
157
			$query = $dbc->select("_bataille_faction.points_faction, _bataille_faction.ID_faction")
158
				->from("_bataille_faction,_bataille_infos_player")
159
				->where("_bataille_infos_player.ID_identite", "=", Bataille::getIdIdentite(), "AND")
160
				->where("_bataille_faction.ID_faction", "=", "_bataille_infos_player.ID_faction", "", true)
161
				->get();
162
			
163
			if (count($query) > 0) {
164
				foreach ($query as $obj) {
165
					$point_joueur = Points::getPointsJoueur();
166
					$calc = $obj->points_faction - $point_joueur;
167
					
168
					if ($del === null) {
169
						$calc = $point_joueur+$obj->points_faction;
170
					}
171
					
172
					$dbc->update("points_faction", $calc)
173
						->from("_bataille_faction")
174
						->where("ID_faction", "=", $obj->ID_faction)
175
						->set();
176
				}
177
			}
178
		}
179
		
180
		/**
181
		 * @param $points
182
		 * permet d'ajouter des points à la faction
183
		 */
184
		public static function setAjouterPointsFaction($points) {
185
			$dbc = App::getDb();
186
			
187
			$query = $dbc->select("_bataille_faction.points_faction, _bataille_faction.ID_faction")
188
				->from("_bataille_faction,_bataille_infos_player")
189
				->where("_bataille_infos_player.ID_identite", "=", Bataille::getIdIdentite(), "AND")
190
				->where("_bataille_faction.ID_faction", "=", "_bataille_infos_player.ID_faction", "", true)
191
				->get();
192
			
193
			if (count($query) > 0) {
194
				foreach ($query as $obj) {
195
					$dbc->update("points_faction", $points+$obj->points_faction)
196
						->from("_bataille_faction")
197
						->where("ID_faction", "=", $obj->ID_faction)
198
						->set();
199
				}
200
			}
201
		}
202
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
203
		
204
	}