Passed
Push — master ( 09abe8...7f2391 )
by Federico
02:56
created

WebApp::pathSeeker()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 16
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 19
rs 8.8571
1
<?php
2
	class WebApp extends Module {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
		protected $pages;
4
		protected $defaultPage;
5
		public $currentPage;
6
		public $jConfig;
7
		public function __construct() {
8
			parent::__construct();
9
			$this->pages = [];
10
			$this->defaultPage	= ["Page404",[]];
11
			$this->currentPage	= null;
12
			$this->$jConfig			= null;
0 ignored issues
show
Bug introduced by
The variable $jConfig does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
13
		}
14
		public function addPage( $_page ) {
15
			$path		= "";
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
16
			$class	= "";
0 ignored issues
show
Unused Code introduced by
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
17
			$param	= [];
18
			if(is_array($_page)) {
19
				$path		= $_page[0];
20
				$class	= $_page[1];
21
				if(isset($_page[2]))
22
					$param = $_page[2];
23
			} else {
24
				$path		= $_page;
25
				$class	= $_page;
26
			}
27
			$this->pages[$path] = [$class,$param];
28
		}
29
		public function addPages( $_pages ) {
30
			foreach ($_pages as $i)
31
				$this->addPage($i);
32
		}
33
		public function fetchPage( $_stack ) {
34
			$parameters = [];
35
			$temp	= $this->defaultPage;
36
			$variables = null;
0 ignored issues
show
Unused Code introduced by
$variables is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37
			foreach ($this->pages as $key => $value) {
38
				$variables = $this->pathSeeker(explode("/", $key), $_stack);
39
				if(is_array($variables)) {
40
					$temp = $value;
41
					$parameters = $variables;
42
					break;
43
				}
44
			}
45
			if( isset($temp[1]) && is_array($temp[1]) )
46
				$temp[1] = array_merge($temp[1], $parameters);
47
			else
48
				$temp[1] = $parameters;
49
			$this->currentPage = new $temp[0](["app" => $this->jConfig, "page" => $temp[1]]);
50
			return $this->currentPage;
51
		}
52
		public function setDefaultPage( $_page ) {
53
			$this->defaultPage = $_page;
54
		}
55
		public function draw() {
56
			$this->currentPage->uniforma();
57
			$gui = new GUI();
58
			$gui->init($this->currentPage);
59
			$gui->draw($this->currentPage->data["template"]);
60
		}
61
		public function pathSeeker( $_path, $_url ) {
62
			$urlLength = count($_url);
63
			$cont = 0;
64
			$variables = [];
65
			$pathLength = count($_path);
66
			if($urlLength == $pathLength) {
67
				while($cont < $urlLength) {
68
					if( $_path[$cont] == $_url[$cont] )
69
						$cont++;
70
					else if( strpos($_path[$cont], "\$") !== false ) {
71
						$variables[str_replace('$', "", $_path[$cont])] = $_url[$cont];
72
						$cont++;
73
					} else break;
74
				}
75
				if($cont == $urlLength)
76
					return $variables;
77
			}
78
			return null;
79
		}
80
		public function newConfig( $_path = "config/") {
81
			$this->jConfig = new JConfig();
82
			$this->jConfig->import("${_path}connection.json","connection");
83
			$this->jConfig->import("${_path}misc.json");
84
			$this->jConfig->import("${_path}router.json");
85
		}
86
	}
87
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
88