Passed
Push — master ( f3d414...859e0d )
by Anthony
02:35
created

Bataille   B

Complexity

Total Complexity 33

Size/Duplication

Total Lines 227
Duplicated Lines 10.13 %

Coupling/Cohesion

Components 6
Dependencies 5

Importance

Changes 0
Metric Value
wmc 33
lcom 6
cbo 5
dl 23
loc 227
rs 8.5454
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getValues() 0 3 1
A getRessource() 0 7 2
A getBase() 0 7 2
A getBatiment() 0 7 2
A getDb() 0 6 2
A getIdIdentite() 0 3 1
A getIdBase() 0 9 2
A getLastConnexion() 12 12 4
A getNombreEmplacementBase() 11 11 4
A getTestAssezRessourceBase() 0 16 2
A getDureeTrajet() 0 14 1
A getPosistionBase() 0 16 2
B getNationBase() 0 20 5
A setLastConnexion() 0 8 1
A setValues() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
	use core\database\Database;
5
6
	class Bataille {
7
		private static $ressource;
8
		private static $base;
9
		private static $batiment;
10
		private static $database;
11
12
		private static $id_base;
13
14
		public static $values = [];
15
16
		
17
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
18
		public function __construct() {
19
20
		}
21
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
22
		
23
		
24
		
25
		//-------------------------- GETTER ----------------------------------------------------------------------------//
26
		/**
27
		 * @return array
28
		 * get array of all values wich will be used in the page
29
		 */
30
		public static function getValues() {
31
		    return ["bataille" => self::$values];
32
		}
33
34
		//initilisation of all classes of battle
35
		//initialisation of Ressource class
36
		public static function getRessource() {
37
			if (self::$ressource == null) {
38
				self::$ressource = new Ressource();
39
			}
40
41
			return self::$ressource;
42
		}
43
44
		//initialisation of Base class
45
		public static function getBase() {
46
			if (self::$base == null) {
47
				self::$base = new Base();
48
			}
49
50
			return self::$base;
51
		}
52
53
		//initialisation of Batiment class
54
		public static function getBatiment() {
55
			if (self::$batiment == null) {
56
				self::$batiment = new Batiment();
57
			}
58
59
			return self::$batiment;
60
		}
61
62
		//initialisation of Database Core connexion
63
		public static function getDb() {
64
			if (self::$database == null) {
65
				self::$database = new Database("mysql", "bataille_core", "root", "Gerto80", "127.0.0.1");
66
			}
67
			return self::$database;
68
		}
69
70
		public static function getIdIdentite() {
0 ignored issues
show
Coding Style introduced by
getIdIdentite 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...
71
			return $_SESSION['idlogin'.CLEF_SITE];
72
		}
73
74
		public static function getIdBase() {
0 ignored issues
show
Coding Style introduced by
getIdBase 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...
75
			if (self::$id_base == null) {
76
				self::$id_base = $_SESSION['id_base'];
77
78
				return self::$id_base;
79
			}
80
81
			return self::$id_base;
82
		}
83
84
		/**
85
		 * @return mixed
86
		 * recupere la date de la derniere connexion
87
		 */
88 View Code Duplication
		public static function getLastConnexion() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
			$dbc = App::getDb();
90
91
			$query = $dbc->select()->from("_bataille_last_connexion")->where("ID_identite", "=", self::getIdIdentite())->get();
92
93
			if ((is_array($query)) && (count($query) > 0)) {
94
				foreach ($query as $obj) {
95
					return $obj->last_connexion;
96
				}
97
			}
98
99
		}
100
101
		/**
102
		 * @return mixed
103
		 * recupere le nombre maximum d'emplacement dans la base
104
		 */
105 View Code Duplication
		public static function getNombreEmplacementBase() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
			$dbc1 = self::getDb();
107
108
			$query = $dbc1->select("nombre_emplacement")->from("configuration")->where("ID_configuration", "=", 1)->get();
109
110
			if ((is_array($query)) && (count($query) > 0)) {
111
				foreach ($query as $obj) {
112
					return $obj->nombre_emplacement;
113
				}
114
			}
115
		}
116
117
		/**
118
		 * @param $nom_ressource
119
		 * @param $ressource
120
		 * @return array
121
		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment
122
		 * ou pour creer une unité...
123
		 */
124
		public static function getTestAssezRessourceBase($nom_ressource, $ressource) {
125
			$f = "get".ucfirst($nom_ressource);
126
127
			if ($ressource >  Bataille::getRessource()->$f()) {
128
				/*echo("$nom_ressource $ressource ".Bataille::getRessource()->getEau()." ---");*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
				return [
130
					"ressource" => $ressource,
131
					"class" => "rouge"
132
				];
133
			}
134
135
			return [
136
				"ressource" => $ressource,
137
				"class" => ""
138
			];
139
		}
140
		
141
		/**
142
		 * @param $id_base
143
		 * @return number
144
		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base
145
		 */
146
		public static function getDureeTrajet($id_base) {
0 ignored issues
show
Coding Style introduced by
getDureeTrajet 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...
147
			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué
148
			$base_joueur = self::getPosistionBase($_SESSION['id_base']);
149
			$base_autre = self::getPosistionBase($id_base);
150
			
151
			//calcul des distances séparant les deux bases en x et y
152
			//cette dstance sera multipliée par 10 sur x et y puis ajoutée pour avoir le temps du trajte en seconde
153
			$calc_x = abs($base_joueur['posx']-$base_autre['posx']);
154
			$calc_y = abs($base_joueur['posy']-$base_autre['posy']);
155
			
156
			$temps_voyage = ($calc_x*15)+($calc_y*15);
157
			
158
			return $temps_voyage;
159
		}
160
		
161
		/**
162
		 * @param $id_base
163
		 * @return array
164
		 * fonction qui renvoi les posisitons en x et y d'une base
165
		 */
166
		private static function getPosistionBase($id_base) {
167
			$dbc = App::getDb();
168
			
169
			$query = $dbc->select("posx")
170
				->select("posy")
171
				->from("_bataille_base")
172
				->where("ID_base", "=", $id_base)
173
				->get();
174
			
175
			foreach ($query as $obj) {
176
				$posx = $obj->posx;
177
				$posy = $obj->posy;
178
			}
179
			
180
			return ["posx" => $posx, "posy" => $posy];
0 ignored issues
show
Bug introduced by
The variable $posx 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...
Bug introduced by
The variable $posy 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...
181
		}
182
183
		/**
184
		 * @param null $id_identite
185
		 * get nation of a player
186
		 */
187
		public static function getNationBase($id_identite = null) {
0 ignored issues
show
Coding Style introduced by
getNationBase 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...
188
			$dbc = App::getDb();
189
190
			if ($id_identite === null) {
191
				$id_identite = $_SESSION['idlogin'.CLEF_SITE];
192
			}
193
194
			$query = $dbc->select("nation")
195
				->from("identite")
196
				->from("_bataille_nation")
197
				->where("identite.ID_identite", "=", $id_identite, "AND")
198
				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true)
199
				->get();
200
201
			if ((is_array($query)) && (count($query) > 0)) {
202
				foreach ($query as $obj) {
203
					self::setValues(["nation" => $obj->nation]);
204
				}
205
			}
206
		}
207
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
208
		
209
		
210
		
211
		//-------------------------- SETTER ----------------------------------------------------------------------------//
212
		/**
213
		 * set la date de derniere connexion a now
214
		 */
215
		public static function setLastConnexion() {
216
			$dbc = App::getDb();
217
218
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))
219
				->from("_bataille_last_connexion")
220
				->where("ID_identite", "=", self::getIdIdentite())
221
				->set();
222
		}
223
224
		/**
225
		 * @param $values
226
		 * can set values while keep older infos
227
		 */
228
		public static function setValues($values) {
229
			Bataille::$values = array_merge(Bataille::$values, $values);
230
		}
231
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
232
	}