Passed
Push — master ( d11529...9e90e2 )
by Anthony
02:33
created

CentreRecherche::__construct()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 38
Code Lines 22

Duplication

Lines 18
Ratio 47.37 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 18
loc 38
rs 4.909
cc 9
eloc 22
nc 12
nop 0
1
<?php
2
	/**
3
	 * Created by PhpStorm.
4
	 * User: anthony
5
	 * Date: 08/12/2016
6
	 * Time: 20:44
7
	 */
8
	
9
	namespace modules\bataille\app\controller;
10
	
11
	
12
	use core\App;
13
14
	class CentreRecherche {
15
		
16
		
17
		
18
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
19
		public function __construct() {
20
			$dbc = App::getDb();
21
22
			$query = $dbc->select()->from("_bataille_centre_recherche")->where("ID_base", "=", Bataille::getIdBase())->get();
23
24 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
25
				foreach ($query as $obj) {
26
					$recherche_base[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recherche_base was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recherche_base = 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...
27
						"recherche" => $obj->recherche,
28
						"niveau" => $obj->niveau,
29
						"type" => $obj->type
30
					];
31
				}
32
			}
33
34
			$query = $dbc->select()->from("recherche")->where("niveau", ">=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche"))->get();
35
36 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37
				foreach ($query as $obj) {
38
					$all_recherche[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_recherche was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_recherche = 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...
39
						"recherche" => $obj->recherche,
40
						"type" => $obj->type,
41
						"cout" => unserialize($obj->cout)
42
					];
43
				}
44
			}
45
46
			$count = cout($all_recherche);
0 ignored issues
show
Bug introduced by
The variable $all_recherche 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...
47
48
			for ($i=0 ; $i<$count ; $i++) {
49
				if ((in_array($all_recherche[$i], $recherche_base))) {
0 ignored issues
show
Bug introduced by
The variable $recherche_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...
50
					echo("améliorer<br>");
51
				}
52
				else {
53
					echo("rechercher<br>");
54
				}
55
			}
56
		}
57
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
58
		
59
		
60
		
61
		//-------------------------- GETTER ----------------------------------------------------------------------------//
62
		/**
63
		 * @param $type
64
		 * @return array|int
65
		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction
66
		 * d'un type donné
67
		 */
68
		public function getAllRechercheType($type) {
69
			$dbc = App::getDb();
70
71
			$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get();
72
73 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
				foreach ($query as $obj) {
75
					$recherche[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$recherche was never initialized. Although not strictly required by PHP, it is generally a good practice to add $recherche = 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...
76
						"niveau" => $obj->niveau,
77
						"recherche" => $obj->recherche
78
					];
79
				}
80
81
				return $recherche;
0 ignored issues
show
Bug introduced by
The variable $recherche 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...
82
			}
83
84
			return 0;
85
		}
86
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
87
		
88
		
89
		
90
		//-------------------------- SETTER ----------------------------------------------------------------------------//
91
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
92
		
93
	}