Passed
Push — master ( 90327c...7effc1 )
by Anthony
02:52
created

Points   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 67
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPointsBase() 0 18 4
A getPointAjoutBatiment() 0 7 2
A setAjouterPoints() 0 13 2
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	class Points {
6
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
7
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
8
		
9
		
10
		//-------------------------- GETTER ----------------------------------------------------------------------------//
11
		/**
12
		 * @param $id_base
13
		 * @return int
14
		 * renvoi les points de la base
15
		 */
16
		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...
17
			$dbc = App::getDb();
18
			
19
			//on récupère les points de la base en cours
20
			$query = $dbc->select("points")
21
				->from("_bataille_base")
22
				->where("ID_base", "=", $id_base)
23
				->where("ID_identite", "=", $_SESSION['id_login'].CLEF_SITE)
24
				->get();
25
			
26
			if ((is_array($query)) && (count($query) > 1)) {
27
				foreach ($query as $obj) {
28
					return $obj->points;
29
				}
30
			}
31
			
32
			return 0;
33
		}
34
		
35
		/**
36
		 * @return mixed
37
		 * fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment
38
		 */
39
		private static function getPointAjoutBatiment() {
40
			$dbc = self::getDb();
0 ignored issues
show
Bug introduced by
The method getDb() does not seem to exist on object<modules\bataille\app\controller\Points>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
			
42
			$query = $dbc->select("points_batiment")->from("configuration")->where("ID_configuration", "=", 1)->get();
43
			
44
			foreach ($query as $obj) return $obj->points_batiment;
45
		}
46
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
47
		
48
		
49
		//-------------------------- SETTER ----------------------------------------------------------------------------//
50
		/**
51
		 * @param $id_base
52
		 * @param $type
53
		 * fonction qui ajoute des points à la base en fonction du type
54
		 * le type peut etre : batiment, attaque, defense, troupe
55
		 */
56
		public static function setAjouterPoints($id_base, $type) {
0 ignored issues
show
Coding Style introduced by
setAjouterPoints 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...
57
			$dbc = App::getDb();
58
			
59
			if ($type == "batiment") {
60
				$req = $dbc->update("points", self::getPointsBase($id_base)+self::getPointAjoutBatiment());
61
				
62
			}
63
			
64
			$req->from("_bataille_base")
0 ignored issues
show
Bug introduced by
The variable $req 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...
65
				->where("ID_base", $id_base)
66
				->where("ID_identite", $_SESSION['id_login'].CLEF_SITE)
67
				->set();
68
		}
69
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
70
		
71
	}