Passed
Push — master ( 1926d4...299422 )
by Anthony
02:29
created

Map::getParametres()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
	namespace modules\bataille\app\controller;
4
5
	use core\App;
6
7
	class Map {
8
9
10
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		public function __construct() {
13
			$dbc = App::getDb();
14
15
			$this->getParametres();
16
17
			$query = $dbc->select("_bataille_base.nom_base")
18
				->select("_bataille_base.points")
19
				->select("_bataille_base.posx")
20
				->select("_bataille_base.posy")
21
				->select("_bataille_base.ID_identite")
22
				->select("identite.pseudo")
23
				->from("identite")
24
				->from("_bataille_base")
25
				->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "", true)
26
				->get();
27
28
			foreach ($query as $obj) {
29
				if ($obj->ID_identite == 1) {
30
					$ma_base = "ma-base";
31
				}
32
33
				$map[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$map was never initialized. Although not strictly required by PHP, it is generally a good practice to add $map = 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...
34
					"nom_base" => $obj->nom_base,
35
					"points" => $obj->points,
36
					"posx" => $obj->posx,
37
					"posy" => $obj->posy,
38
					"id_identite" => $obj->ID_identite,
39
					"pseudo" => $obj->pseudo,
40
					"ma_base" => $ma_base
0 ignored issues
show
Bug introduced by
The variable $ma_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...
41
				];
42
			}
43
44
			Bataille::setValues(["map" => $map]);
0 ignored issues
show
Bug introduced by
The variable $map 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...
45
		}
46
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
47
48
49
50
		//-------------------------- GETTER ----------------------------------------------------------------------------//
51
		/**
52
		 * fonction qui sert à récupérer les parametres de la map
53
		 */
54
		private function getParametres() {
55
			$dbc = App::getDb();
56
57
			$query = $dbc->select()->from("_bataille_map")->where("ID_map", "=", 1)->get();
58
59
			foreach ($query as $obj) {
60
				Bataille::setValues([
61
					"largeur_map" => $obj->largeur,
62
					"hauteur_map" => $obj->hauteur
63
				]);
64
			}
65
		}
66
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
67
68
69
70
		//-------------------------- SETTER ----------------------------------------------------------------------------//
71
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
72
	}