Passed
Push — master ( 14cd89...28f717 )
by Anthony
03:40
created

Points::setRejoindreQuitterFaction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 17
nc 4
nop 1
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("_bataille_infos_player.ID_identite", "=", "identite.ID_identite", "", true)
21
				->orderBy("_bataille_infos_player.points", "DESC")
22
				->limit($start, 50)
23
				->get();
24
			
25 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
26
				$count = 1;
27
				foreach ($query as $obj) {
28
					$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...
29
						"id_identite" => $obj->ID_identite,
30
						"pseudo" => $obj->pseudo,
31
						"points" => $obj->points,
32
						"classement" => $count
33
					];
34
					
35
					$count++;
36
				}
37
				
38
				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...
39
			}
40
			
41
		}
42
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
43
		
44
		
45
		//-------------------------- GETTER ----------------------------------------------------------------------------//
46
		/**
47
		 * @param $id_base
48
		 * @return int
49
		 * renvoi les points de la base
50
		 */
51
		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...
52
			$dbc = App::getDb();
53
			
54
			//on récupère les points de la base en cours
55
			$query = $dbc->select("points")
56
				->from("_bataille_base")
57
				->where("ID_base", "=", $id_base, "AND")
58
				->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])
59
				->get();
60
			
61
			if ((is_array($query)) && (count($query) > 0)) {
62
				foreach ($query as $obj) {
63
					return $obj->points;
64
				}
65
			}
66
			
67
			return 0;
68
		}
69
		
70
		/**
71
		 * @param null $id_identite
72
		 * @return int
73
		 * renvoi les points totaux d'un joueur
74
		 */
75
		public static function getPointsJoueur($id_identite=null) {
76
			$dbc = App::getDb();
77
			
78
			if ($id_identite === null) {
79
				$id_identite = Bataille::getIdIdentite();
80
			}
81
			
82
			$query = $dbc->select("points")->from("_bataille_infos_player")->where("ID_identite", "=", $id_identite)->get();
83
			
84
			if (count($query) == 1) {
85
				foreach ($query as $obj) {
86
					return $obj->points;
87
				}
88
			}
89
			
90
			return 0;
91
		}
92
		
93
		/**
94
		 * @return mixed
95
		 * fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment
96
		 */
97
		private static function getPointAjoutBatiment() {
98
			return Bataille::getParam("points_batiment");
99
		}
100
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
101
		
102
		
103
		//-------------------------- SETTER ----------------------------------------------------------------------------//
104
		/**
105
		 * @param $id_base
106
		 * @param null $type
107
		 * @param null $points
108
		 * @return int|null
109
		 * fonction qui ajoute des points à la base en fonction du type
110
		 * le type peut etre : batiment, attaque, defense, troupe
111
		 */
112
		public static function setAjouterPoints($id_base, $type=null, $points=null) {
113
			$dbc = App::getDb();
114
115
			if ($type == "batiment") {
116
				$points_faction = self::getPointAjoutBatiment();
117
				$points = self::getPointsBase($id_base)+self::getPointAjoutBatiment();
118
			}
119
			
120
			$dbc->update("points", $points)
121
				->from("_bataille_base")
122
				->where("ID_base", "=", $id_base)
123
				->set();
124
			
125
			self::setAjouterPointsTotaux();
126
			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...
127
			
128
			return $points;
129
		}
130
		
131
		/**
132
		 * fonction qui prend les points de toutes les bases et qui les ajoute sur le joueur en lui même
133
		 */
134
		private static function setAjouterPointsTotaux() {
135
			$dbc = App::getDb();
136
			
137
			$query = $dbc->select("points")->from("_bataille_base")->where("ID_identite", "=", Bataille::getIdIdentite())->get();
138
			
139
			if ((is_array($query)) && (count($query) > 0)) {
140
				$points = 0;
141
				
142
				foreach ($query as $obj) {
143
					$points += $obj->points;
144
				}
145
				
146
				$dbc->update("points", $points)->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
147
			}
148
		}
149
		
150
		/**
151
		 * fonction qui permet d'ajouter tous les points du joueur aux points de la faction
152
		 */
153
		public static function setRejoindreQuitterFaction($del = null) {
154
			$dbc = App::getDb();
155
			
156
			$query = $dbc->select("_bataille_faction.points_faction, _bataille_faction.ID_faction")
157
				->from("_bataille_faction,_bataille_infos_player")
158
				->where("_bataille_infos_player.ID_identite", "=", Bataille::getIdIdentite(), "AND")
159
				->where("_bataille_faction.ID_faction", "=", "_bataille_infos_player.ID_faction", "", true)
160
				->get();
161
			
162
			if (count($query) > 0) {
163
				foreach ($query as $obj) {
164
					$point_joueur = Points::getPointsJoueur();
165
					$calc = $obj->points_faction - $point_joueur;
166
					
167
					if ($del === null) {
168
						$calc = $point_joueur+$obj->points_faction;
169
					}
170
					
171
					$dbc->update("points_faction", $calc)
172
						->from("_bataille_faction")
173
						->where("ID_faction", "=", $obj->ID_faction)
174
						->set();
175
				}
176
			}
177
		}
178
		
179
		/**
180
		 * @param $points
181
		 * permet d'ajouter des points à la faction
182
		 */
183
		public static function setAjouterPointsFaction($points) {
184
			$dbc = App::getDb();
185
			
186
			$query = $dbc->select("_bataille_faction.points_faction, _bataille_faction.ID_faction")
187
				->from("_bataille_faction,_bataille_infos_player")
188
				->where("_bataille_infos_player.ID_identite", "=", Bataille::getIdIdentite(), "AND")
189
				->where("_bataille_faction.ID_faction", "=", "_bataille_infos_player.ID_faction", "", true)
190
				->get();
191
			
192
			if (count($query) > 0) {
193
				foreach ($query as $obj) {
194
					$dbc->update("points_faction", $points+$obj->points_faction)
195
						->from("_bataille_faction")
196
						->where("ID_faction", "=", $obj->ID_faction)
197
						->set();
198
				}
199
			}
200
		}
201
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
202
		
203
	}