Passed
Push — master ( 5c1cfb...bc7ca9 )
by Anthony
02:45
created

RouterModule::getModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
	namespace core\modules;
3
4
5
	use core\App;
6
	use core\functions\ChaineCaractere;
7
8
	class RouterModule {
9
		//varaibles de base de config
10
		private $controller;
11
		private $erreur;
12
		private $parametre;
13
		private $module; //varialbe qui contiendra le nom d'un module
14
		private $page;
15
16
17
		//-------------------------- GETTER ----------------------------------------------------------------------------//
18
		//pour les parametres du getUrl ++ getAction ++ getController
19
		public function getParametre() {
20
			return $this->parametre;
21
		}
22
		public function getPage() {
23
			return $this->page;
24
		}
25
		public function getModule(){
26
		    return $this->module;
27
		}
28
		public function getController() {
29
			return $this->controller;
30
		}
31
		public function getErreur() {
32
			return $this->erreur;
33
		}
34
35 View Code Duplication
		private function getAllModules() {
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...
36
			$dbc = App::getDb();
37
			$module = "";
38
39
			$query = $dbc->select()->from("module")->get();
40
41
			if ((is_array($query)) && (count($query) > 0)) {
42
				foreach ($query as $obj) {
43
					$module[] = str_replace("/", "", $obj->url);
44
				}
45
			}
46
47
			return $module;
48
		}
49
50
		/**
51
		 * Permets de générer l'url pour aller charger la page concernee pour le module blog
52
		 * appele également l'actoin à effectur dans la page
53
		 * @param $url
54
		 * @return string
55
		 */
56
		public function getUrl($url) {
57
			$explode = explode("/", $url);
58
			$count = count($explode);
59
			$debut_url = "";
60
61
			for ($i = 0; $i < $count; $i++) {
62
				if (in_array($explode[$i], $this->getAllModules())) {
63
					$this->module = $explode[$i];
64
					$debut_url = $explode[$i];
65
				}
66
				else if ($i >= 1) {
67
					$centre_url[] = $explode[$i];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$centre_url was never initialized. Although not strictly required by PHP, it is generally a good practice to add $centre_url = 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...
68
					$this->page[] = $explode[$i];
69
				}
70
				/*else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
71
					$this->parametre = $explode[$i];
72
				}*/
73
			}
74
			
75
			$centre_url = implode("/", $centre_url);
0 ignored issues
show
Bug introduced by
The variable $centre_url 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...
76
			$this->page = implode("/", $this->page);
77
78
			if (!isset($centre_url) || ($centre_url == "")) {
79
				$this->page = "index";
80
			}
81
			else {
82
				$file = ROOT."modules/".$debut_url."/app/views/".$this->page;
83
				
84
				if (!file_exists($file)) {
85
					$this->page = explode("/", $file);
86
					$this->parametre = array_pop($this->page);
87
					
88
					$this->page = implode("/", $this->page);
89
				}
90
			}
91
			
92
			echo $this->page;
93
			echo $this->parametre ;
94
95
			$this->setActionPage();
96
97
			return $this->page;
98
		}
99
100
		/**
101
		 * fonction qui permet de tester qu'une route existe bien
102
		 * appellee dans redirectError.class.php
103
		 * @param $url
104
		 */
105
		public function getRouteModuleExist($url) {
106
			$dbc = \core\App::getDb();
107
			$query = $dbc->select()->from("module")->get();
108
109
			if ((is_array($query)) && (count($query) > 0)) {
110
				foreach ($query as $obj) {
111
					$test_module = ChaineCaractere::FindInString($url, $obj->url);
112
					$test_module1 = ChaineCaractere::FindInString($url, str_replace("/", "", $obj->url));
113
					$module_activer = \core\modules\GestionModule::getModuleActiver($obj->nom_module);
114
115
					if ((($test_module === true) || ($test_module1 === true)) && ($module_activer === true)) {
116
						return true;
117
					}
118
				}
119
			}
120
		}
121
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
122
123
124
125
		//-------------------------- SETTER ----------------------------------------------------------------------------//
126
		/**
127
		 * Fonction qui va se charger en focntion $this->page et de $this->action d'appeler la fonctoin qui va bien
128
		 * fontction appelee dans getUrl()
129
		 */
130
		private function setActionPage() {
131
			//on require le fichier routes.php dans /modules/nom_module/router/routes.php
132
133
			require_once(MODULEROOT.$this->module."/router/routes.php");
134
		}
135
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
136
	}