Passed
Push — master ( 8a1794...d608f5 )
by Anthony
02:52
created

Navigation::getSousMenu()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 5
Ratio 35.71 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 14
rs 9.2
cc 4
eloc 8
nc 2
nop 1
1
<?php
2
	namespace core;
3
	class Navigation {
4
		private $navigation;
5
		private $last_ordre;
6
		
7
		
8
		
9
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
10
		public function __construct() {
11
			$dbc = App::getDb();
12
			$navigation = [];
13
14
			$query = $dbc->select()->from("navigation")->orderBy("ordre")->get();
15
16
			if (is_array($query) && (count($query) > 0)) {
17
				foreach ($query as $obj) {
18
					if ($obj->ID_page === null) {
19
						$navigation[] = $this->getLienNavigationModule($obj->ID_module);
20
					}
21
					else {
22
						$navigation[] = $this->getLienNavigationPage($obj->ID_page);
23
					}
24
					$last_ordre = $obj->ordre;
25
				}
26
27
				$this->last_ordre = $last_ordre;
0 ignored issues
show
Bug introduced by
The variable $last_ordre 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...
28
				$this->setNavigation($navigation);
29
			}
30
		}
31
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
32
		
33
		
34
		
35
		//-------------------------- GETTER ----------------------------------------------------------------------------//
36
		public function getNavigation() {
37
			return $this->navigation;
38
		}
39
40
		private function getLienNavigationPage($id_page) {
41
			$dbc = App::getDb();
42
43
			$query = $dbc->select()
44
				->from("navigation")
45
				->from("page")
46
				->where("navigation.ID_page", "=", "page.ID_page", "AND")
47
				->where("page.ID_page", "=", $id_page, "AND")
48
				->where("page.affiche", "=", 1, "AND")
49
				->where("page.parent", "=", 0)
50
				->get();
51
52 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...
53
				foreach ($query as $obj) {
54
					return [$obj->titre, $obj->url, $obj->balise_title, $this->getSousMenu($id_page)];
55
				}
56
			}
57
		}
58
59
		private function getSousMenu($id_page) {
60
			$dbc = App::getDb();
61
			$sous_menu = [];
62
63
			$query = $dbc->select()->from("page")->where("parent", "=", $id_page)->get();
64
65 View Code Duplication
			if (is_array($query) && (count($query) > 0)) {
1 ignored issue
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...
66
				foreach ($query as $obj) {
67
					$sous_menu[] = [$obj->titre, $obj->url, $obj->balise_title,];
68
				}
69
			}
70
71
			return $sous_menu;
72
		}
73
74
		private function getLienNavigationModule($id_module) {
75
			$dbc = App::getDb();
76
77
			$query = $dbc->select()
78
				->from("navigation")
79
				->from("module")
80
				->where("navigation.ID_module", "=", "module.ID_module", "AND")
81
				->where("module.ID_module", "=", $id_module, "AND")
82
				->where("module.installer", "=", 1, "AND")
83
				->where("module.activer", "=", 1)
84
				->get();
85
86 View Code Duplication
			if (is_array($query) && (count($query) > 0)) {
1 ignored issue
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...
87
				foreach ($query as $obj) {
88
					return [$obj->nom_module,$obj->url];
89
				}
90
			}
91
		}
92
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
93
		
94
		
95
		
96
		//-------------------------- SETTER ----------------------------------------------------------------------------//
97
		private function setNavigation($navigation) {
98
			$this->navigation = $navigation;
99
		}
100
101
		public function setTestAjoutLien($id, $value_id, $afficher) {
102
			$dbc = App::getDb();
103
104
			if ($afficher != null) {
105
				$dbc->insert($id, $value_id)->insert("ordre", $this->last_ordre+1)->into("navigation")->set();
106
			}
107
		}
108
109
		public function setSupprimerLien($id, $value_id) {echo("$id, $value_id");
110
			$dbc = App::getDb();
111
112
			$dbc->delete()->from("navigation")->where($id, "=", $value_id)->del();
113
		}
114
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
115
	}