Passed
Push — master ( 213e81...182e59 )
by Anthony
02:30
created

MissionsAleatoire::getMissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	
7
	class MissionsAleatoire {
8
		
9
		
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		/**
13
		 * MissionsAleatoire constructor.
14
		 * le constructeur s'occupe de vérifier le last_check des missions et au cas ou si il est plus vieux d'un jour
15
		 * appeler la fonction pour recharger les missions
16
		 */
17
		public function __construct() {
18
			$dbc = App::getDb();
19
			
20
			$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get();
21
			
22
			if (is_array($query) && (count($query) == 1)) {
23
				foreach ($query as $obj) {
24
					$last_check_mission = $obj->last_check_mission;
25
				}
26
				
27
				if ($last_check_mission == "") {
28
					$this->setUpdateLastCheckMissions();
29
					$this->setMissionsAleatoire();
30
				}
31
				else {
32
					$today = new \DateTime();
33
					$last_check_mission = new \DateTime($last_check_mission);
0 ignored issues
show
Bug introduced by
The variable $last_check_mission 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...
34
					$interval = $last_check_mission->diff($today);
35
					
36
					$diff_jour = explode("+", $interval->format("%R%a"))[1];
37
					
38
					if ($diff_jour >= 1) {
39
						$this->setUpdateLastCheckMissions();
40
						$this->setMissionsAleatoire();
41
					}
42
				}
43
			}
44
		}
45
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
46
		
47
		
48
		
49
		//-------------------------- GETTER ----------------------------------------------------------------------------//
50
		/**
51
		 * fonction qui récupere tous les types de missions et les return dans un array
52
		 */
53
		private function getTypeMission() {
54
			return explode(",", Bataille::getParam("type_missions"));
55
		}
56
		
57
		/**
58
		 * @return int
59
		 * renvoi le nombre de missions encore disponibles dans la base
60
		 */
61
		public function getNbMissions() {
62
			$dbc = App::getDb();
63
			
64
			$query = $dbc->select("ID_mission_aleatoire")->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
65
			
66
			if ((is_array($query)) && (count($query))) {
67
				foreach ($query as $obj) {
68
					$id[] = $obj->ID_base;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$id was never initialized. Although not strictly required by PHP, it is generally a good practice to add $id = 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...
69
				}
70
				
71
				$count = count($id);
0 ignored issues
show
Bug introduced by
The variable $id 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...
72
				Bataille::setValues([
73
					"nb_mission" => $count
74
				]);
75
				
76
				return $count;
77
			}
78
		}
79
		
80
		public function getMissions() {
81
			$dbc = App::getDb();
0 ignored issues
show
Unused Code introduced by
$dbc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
			$dbc1 = Bataille::getDb();
0 ignored issues
show
Unused Code introduced by
$dbc1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
			
84
			
85
		}
86
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
87
		
88
		
89
		
90
		//-------------------------- SETTER ----------------------------------------------------------------------------//
91
		/**
92
		 * fonction qui met a jour le last_ckeck_missions dans _bataille_base
93
		 * le met à la date du jour
94
		 */
95
		public function setUpdateLastCheckMissions() {
96
			$dbc = App::getDb();
97
			
98
			$dbc->update("last_check_mission", date("Y-m-d"))
99
				->from("_bataille_base")
100
				->where("ID_base", "=", Bataille::getIdBase())
101
				->set();
102
		}
103
		
104
		/**
105
		 * @param $type
106
		 * fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute
107
		 * dans la table _bataille_mission_aleatoire
108
		 */
109
		private function setMissionsAleatoire() {
110
			$dbc = App::getDb();
111
			$dbc1 = Bataille::getDb();
112
			
113
			$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del();
114
			
115
			$type_missions = $this->getTypeMission();
116
			
117
			foreach ($type_missions as $un_type) {
118
				$query = $dbc1->select()->from("mission")
119
					->where("type", "=", $un_type)
120
					->orderBy("RAND()")
121
					->limit(0, 3)
122
					->get();
123
				
124
				if ((is_array($query)) && (count($query))) {
125
					foreach ($query as $obj) {
126
						$dbc->insert("ID_mission", $obj->ID_mission)
127
							->insert("ID_base", Bataille::getIdBase())
128
							->into("_bataille_mission_aleatoire")
129
							->set();
130
					}
131
				}
132
			}
133
		}
134
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
135
		
136
	}