Passed
Push — master ( c99e32...f48f1d )
by Anthony
07:10
created

Points   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 101
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 32 5
A getPointsBase() 0 18 4
A getPointAjoutBatiment() 0 3 1
A setAjouterPoints() 0 17 3
1
<?php
2
	namespace modules\bataille\app\controller;
3
4
	use core\App;
5
	
6
	
7
	class Points {
8
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
9
		public function __construct($start = null) {
10
			$dbc = App::getDb();
11
			
12
			if ($start === null) $start = 0;
13
			
14
			$query = $dbc->select("identite.pseudo")
15
				->select("_bataille_base.points")
16
				->select("_bataille_base.ID_identite")
17
				->from("_bataille_base")
18
				->from("identite")
19
				->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "", true)
20
				->orderBy("_bataille_base.points", "DESC")
21
				->limit($start, 50)
22
				->get();
23
			
24
			if ((is_array($query)) && (count($query) > 0)) {
25
				$count = 1;
26
				foreach ($query as $obj) {
27
					$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...
28
						"id_identite" => $obj->ID_identite,
29
						"pseudo" => $obj->pseudo,
30
						"points" => $obj->points,
31
						"classement" => $count
32
					];
33
					
34
					$count++;
35
				}
36
				
37
				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...
38
			}
39
			
40
		}
41
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
42
		
43
		
44
		//-------------------------- GETTER ----------------------------------------------------------------------------//
45
		/**
46
		 * @param $id_base
47
		 * @return int
48
		 * renvoi les points de la base
49
		 */
50
		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...
51
			$dbc = App::getDb();
52
			
53
			//on récupère les points de la base en cours
54
			$query = $dbc->select("points")
55
				->from("_bataille_base")
56
				->where("ID_base", "=", $id_base, "AND")
57
				->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])
58
				->get();
59
			
60
			if ((is_array($query)) && (count($query) > 0)) {
61
				foreach ($query as $obj) {
62
					return $obj->points;
63
				}
64
			}
65
			
66
			return 0;
67
		}
68
		
69
		/**
70
		 * @return mixed
71
		 * fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment
72
		 */
73
		private static function getPointAjoutBatiment() {
74
			return Bataille::getParam("points_batiment");
75
		}
76
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
77
		
78
		
79
		//-------------------------- SETTER ----------------------------------------------------------------------------//
80
		/**
81
		 * @param $id_base
82
		 * @param null $type
83
		 * @param null $points
84
		 * @return int|null
85
		 * fonction qui ajoute des points à la base en fonction du type
86
		 * le type peut etre : batiment, attaque, defense, troupe
87
		 */
88
		public static function setAjouterPoints($id_base, $type=null, $points=null) {
89
			$dbc = App::getDb();
90
91
			if ($type == "batiment") {
92
				$points = self::getPointsBase($id_base)+self::getPointAjoutBatiment();
93
			}
94
			else if ($type == "missions") {
95
				$points = self::getPointsBase($id_base)+$points;
96
			}
97
			
98
			$dbc->update("points", $points)
99
				->from("_bataille_base")
100
				->where("ID_base", "=", $id_base)
101
				->set();
102
			
103
			return $points;
104
		}
105
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
106
		
107
	}