Passed
Push — master ( 1c7663...341095 )
by Anthony
02:41
created

Marche::setCommencerTransport()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 8.5806
cc 4
eloc 19
nc 4
nop 7
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\HTML\flashmessage\FlashMessage;
7
8
	class Marche {
9
		private $id_base_dest;
10
		private $aller;
11
		private $ressources;
12
		private $date_arrivee;
13
		private $nom_base;
14
		private $id_marche_transport;
15
		private $duree_restante_trajet;
16
17
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
18
		public function __construct() {
19
			$dbc = App::getDb();
20
21
			//récupération des trajets en cours
22
			$query = $dbc->select()->from("_bataille_marche_transport")
23
				->from("_bataille_base")
24
				->where("_bataille_marche_transport.ID_base", "=", Bataille::getIdBase(), "AND")
25
				->where("_bataille_marche_transport.ID_base_dest", "=", "_bataille_base.ID_base", "", true)
26
				->get();
27
28
			if ((is_array($query)) && (count($query) > 0)) {
29
				foreach ($query as $obj) {
30
					$this->id_base_dest = $obj->ID_base_dest;
31
					$this->aller = $obj->aller;
32
					$this->ressources = $obj->ressources;
33
					$this->nom_base = $obj->nom_base;
34
					$this->date_arrivee = $obj->date_arrivee;
35
					$this->id_marche_transport = $obj->ID_marche_transport;
36
37
					$marche = $this->getTransportArrive();
38
				}
39
40
				Bataille::setValues(["marche" => $marche]);
0 ignored issues
show
Bug introduced by
The variable $marche 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
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
44
45
46
47
		//-------------------------- GETTER ----------------------------------------------------------------------------//
48
		/**
49
		 * fonction qui permet de savoir si un transport est arrivé à la base de destination
50
		 * +si arrivé appel la fonction pour ajouter les ressources et passe le trajet sur le retour
51
		 * ou en trajet fini suivant le temps de la date d'aujourd'hui et la date à laquelle le
52
		 * trajet aurait du revenir
53
		 */
54
		private function getTransportArrive() {
55
			$today = Bataille::getToday();
56
57
			echo "today : ".$today." ++ date arrivee : ";
58
			echo $this->date_arrivee."<br>";
59
60
			//on test si déja arrivé à destination
61
			if (($this->aller == 1) && (($this->date_arrivee-$today) <= 0)) {
62
				$this->setLivrerRessource();
63
64
				//on calcul la date d'arrivée du retour
65
				$date_retour = Bataille::getDureeTrajet($this->id_base_dest)+$this->date_arrivee;
66
67
				//si le retour du trajet est également arrivé on finit le transport sinon on le place sur le retour
68
				if ($date_retour < $today) {
69
					$this->setTerminerTransport();
70
				}
71
				else {
72
					$this->setTrajetRetour($date_retour);
73
					$this->duree_restante_trajet = $date_retour-$today;
74
					$set_array = true;
75
				}
76
			}
77
			else if (($this->aller == 0) && (($this->date_arrivee-$today) <= 0)) {
78
				$this->setTerminerTransport();
79
			}
80
			else {
81
				$this->duree_restante_trajet = $this->date_arrivee-$today;
82
				$set_array = true;
83
			}
84
85
			if ($set_array === true) {
0 ignored issues
show
Bug introduced by
The variable $set_array 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...
86
				if ($this->aller == 1) {
87
					$marche["aller"][] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$marche was never initialized. Although not strictly required by PHP, it is generally a good practice to add $marche = 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...
88
						"id_marche_transport" => $this->id_marche_transport,
89
						"date_arrivee" => $this->duree_restante_trajet,
90
						"nom_base_dest" => $this->nom_base
91
					];
92
				}
93
				else {
94
					$marche["retour"][] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$marche was never initialized. Although not strictly required by PHP, it is generally a good practice to add $marche = 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...
95
						"id_marche_transport" => $this->id_marche_transport,
96
						"date_arrivee" => $this->duree_restante_trajet,
97
						"nom_base_dest" => $this->nom_base
98
					];
99
				}
100
101
				return $marche;
102
			}
103
		}
104
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
105
106
107
108
		//-------------------------- SETTER ----------------------------------------------------------------------------//
109
		/**
110
		 * fonction qui permet d'ajouter les ressources à la base destinatrice du transport
111
		 */
112
		private function setLivrerRessource() {
113
			$ressource = new Ressource($this->id_base_dest);
114
115
			$ressource_transport = unserialize($this->ressources);
116
117
			$ressource->setUpdateRessource($ressource_transport['eau'], $ressource_transport['electricite'], $ressource_transport['fer'], $ressource_transport['fuel'], $ressource_transport['nourriture'], "+");
118
		}
119
120
		/**
121
		 * fonction qui place le trajet en retour
122
		 */
123
		private function setTrajetRetour($date_retour) {
124
			$dbc = App::getDb();
125
126
			$dbc->update("ressources", 0)
127
				->update("aller", 0)
128
				->update("date_arrivee", $date_retour)
129
				->from("_bataille_marche_transport")
130
				->where("ID_marche_transport", "=", $this->id_marche_transport)
131
				->set();
132
133
			$this->aller = 0;
134
		}
135
136
		/**
137
		 * permet de terminer totallement un transport
138
		 */
139
		private function setTerminerTransport() {
140
			$dbc = App::getDb();
141
142
			$dbc->delete()->from("_bataille_marche_transport")->where("ID_marche_transport", "=", $this->id_marche_transport)->del();
143
		}
144
145
		public function setCommencerTransport($eau, $electricite, $fer, $fuel, $nourriture, $posx, $posy) {
146
			$id_base_dest = Bataille::getBaseExistPosition($posx, $posy);
147
148
			if ($id_base_dest != 0) {
149
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $eau);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ressource was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ressource = 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...
150
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $electricite);
151
				$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $fer);
152
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $fuel);
153
				$ressource["nourriture"] = Bataille::getTestAssezRessourceBase("nourriture", $nourriture);
154
155
				//si pas assez de ressources dispo dans la base pour l'envoi on renvoi erreur
156
				foreach ($ressource as $tab) {
157
					if (in_array("rouge", $tab)) {echo $posy;echo $posx;
158
						FlashMessage::setFlash("Vous n'avez pas autant de ressources disponibles à l'envoi");
159
						return false;
160
					};
161
				}
162
163
				//sinon initialise le transport
164
				//on recup la date d'arrivee dans la base de destintation
165
				$date_arrivee = Bataille::getDureeTrajet($id_base_dest)+Bataille::getToday();
0 ignored issues
show
Unused Code introduced by
$date_arrivee 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...
166
167
168
				echo("<pre>");
169
				print_r($ressource);
170
				echo("</pre>");
171
172
				return true;
173
			}
174
175
			FlashMessage::setFlash("Aucune base présente aux coordonnées données");
176
			return false;
177
		}
178
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
179
	}