Passed
Push — master ( f99231...67efd0 )
by Anthony
02:51
created

Unite::getUnitePossibleRecruter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
	
3
	namespace modules\bataille\app\controller;
4
	
5
	
6
	class Unite {
7
		private $coef_unite;
8
9
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12 View Code Duplication
		public function __construct() {
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...
13
			$dbc1 = Bataille::getDb();
14
15
			$query = $dbc1->select("coef_niveau_unite")->from("configuration")->where("ID_configuration", "=", 1)->get();
16
17
			if ((is_array($query)) && (count($query) == 1)) {
18
				foreach ($query as $obj) $this->coef_unite = $obj->coef_niveau_unite;
19
			}
20
		}
21
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
22
		
23
		
24
		//-------------------------- GETTER ----------------------------------------------------------------------------//
25
26
		/**
27
		 * @param $unite
28
		 * @param $niveau
29
		 * @param $type
30
		 * @return array
31
		 * récupère les caractéristiques de l'unité en fonction de son niveau
32
		 */
33
		private function getCaracteristiqueUnite($unite, $niveau, $type) {
34
			$dbc1 = Bataille::getDb();
35
36
			$query = $dbc1->select()
37
				->from("unites")
38
				->where("nom", "=", $unite, "AND")
39
				->where("type", "=", $type, "")
40
				->get();
41
42
			if ((is_array($query)) && (count($query) == 1)) {
43
				foreach ($query as $obj) {
44
					$base_carac = unserialize($obj->caracteristique);
45
				}
46
47
				$coef = $this->coef_unite*$niveau;
48
49
				if ($niveau == 1) $coef = 1;
50
51
				return [
52
					"attaque" => round($base_carac["attaque"]*$coef),
0 ignored issues
show
Bug introduced by
The variable $base_carac 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...
53
					"defense" => round($base_carac["defense"]*$coef),
54
					"resistance" => round($base_carac["resistance"]*$coef),
55
					"vitesse" => $base_carac["vitesse"]
56
				];
57
			}
58
			else {
59
				return [];
60
			}
61
		}
62
63
		/**
64
		 * @param $type
65
		 * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction
66
		 * du type (batiment sur lequel on a cliqué)
67
		 */
68
		public function getUnitePossibleRecruter($type) {
69
			//on recup toutes les unites deja recherchée donc que l'on peut faire
70
			$unites = Bataille::getCentreRecherche()->getAllRechercheType($type);
71
72
			//recupérer les caractéristiques de l'unité en question
73
			for ($i=0 ; $i<count($unites) ; $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
74
				$unites[$i]["caracteristique"] = $this->getCaracteristiqueUnite($unites[$i]["recherche"], $unites[$i]["niveau"], $type);
75
			}
76
77
			//si pas d'unites encore recherchees on renvoit un array juste avec 0 dedans
78
			Bataille::setValues(["unites" => $unites]);
79
		}
80
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
81
		
82
		
83
		//-------------------------- SETTER ----------------------------------------------------------------------------//
84
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
85
	}