Passed
Branch master (c64b1d)
by Henri
01:38
created
src/Helper.php 1 patch
Spacing   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait Helper{
7
+trait Helper {
8 8
     use CheckTrait, ControllerTrait;
9 9
     
10 10
     private $currentRoute = null;
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
     protected function getProtocol(): string
28 28
     {
29 29
         $protocol = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? 'ajax' : 'get';
30
-        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD',$_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
30
+        $protocol = (array_key_existS('HTTP_REQUESTED_METHOD', $_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol;
31 31
             
32 32
         return $protocol;
33 33
     }
@@ -42,12 +42,12 @@  discard block
 block discarded – undo
42 42
         ];
43 43
     }
44 44
 
45
-    protected function ControllerForm($controller, string $method, array $values){
45
+    protected function ControllerForm($controller, string $method, array $values) {
46 46
 		$this->checkRole();
47 47
         $method = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
48
-        $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : null;
48
+        $data = (array_key_exists('data', $values)) ? json_decode($values['data'], true) : null;
49 49
 
50
-        call_user_func_array([$controller,$method],  $data);
50
+        call_user_func_array([$controller, $method], $data);
51 51
     }
52 52
 
53 53
     protected function Controller(string $controll)
@@ -55,36 +55,36 @@  discard block
 block discarded – undo
55 55
         $data = $this->getData();
56 56
 
57 57
         foreach ($data['GET'] as $name => $value) {
58
-            $controll = str_replace('{'.$name.'}',$value,$controll);
58
+            $controll = str_replace('{'.$name.'}', $value, $controll);
59 59
         }
60 60
 
61 61
         $this->checkControllSettable($controll)->checkControllExist($controll)->checkControllMethod($controll);
62 62
 
63
-        $controller = ucfirst(explode(':',$controll)[0]);
63
+        $controller = ucfirst(explode(':', $controll)[0]);
64 64
         $controller = new $controller();
65
-        $method = explode(':',$controll)[1];
65
+        $method = explode(':', $controll)[1];
66 66
 
67
-        if( ($this->getProtocol() == 'form') ){
67
+        if (($this->getProtocol() == 'form')) {
68 68
             $this->ControllerForm($controller, $method, $data['POST']);
69 69
         }else {
70
-            $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
71
-            call_user_func_array([$controller,$method],  $data);
70
+            $data = (array_key_exists('data', $data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET'];
71
+            call_user_func_array([$controller, $method], $data);
72 72
         }
73 73
 
74 74
         return $this;
75 75
     }    
76 76
 
77
-    protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array
77
+    protected function explodeRoutes(bool $bar, string $url, bool $bar_, string $url_): array
78 78
     {   
79
-        $url = $bar ? substr($url, 0, -1) : $url ;
80
-        $url = explode('/',$url);
79
+        $url = $bar ? substr($url, 0, -1) : $url;
80
+        $url = explode('/', $url);
81 81
 
82
-        $url_ = $bar_ ? substr($url_, 0, -1) : $url_ ;
83
-        $url_ = explode('/',$url_);
82
+        $url_ = $bar_ ? substr($url_, 0, -1) : $url_;
83
+        $url_ = explode('/', $url_);
84 84
 
85
-        foreach($url as $ur => $u){
86
-            if(substr($u,0,2) === '{?'){
87
-                if(!array_key_exists($ur,$url_)){
85
+        foreach ($url as $ur => $u) {
86
+            if (substr($u, 0, 2) === '{?') {
87
+                if (!array_key_exists($ur, $url_)) {
88 88
                     $url_[$ur] = '';
89 89
                 };
90 90
             }
@@ -95,25 +95,25 @@  discard block
 block discarded – undo
95 95
 
96 96
     protected function toHiking(array $route): bool
97 97
     {
98
-        $this->callOnRoute($route,'beforeAll')->callOnRoute($route,'before');
98
+        $this->callOnRoute($route, 'beforeAll')->callOnRoute($route, 'before');
99 99
 
100
-        if(is_string($route['role'])){
101
-            $this->Controller($route['role'])->callOnRoute($route,'after')->callOnRoute($route,'afterAll');
100
+        if (is_string($route['role'])) {
101
+            $this->Controller($route['role'])->callOnRoute($route, 'after')->callOnRoute($route, 'afterAll');
102 102
             return true;
103 103
         }
104 104
 
105
-        call_user_func_array($route['role'],$this->getData()['GET']);
105
+        call_user_func_array($route['role'], $this->getData()['GET']);
106 106
 
107
-        $this->callOnRoute($route,'after')->callOnRoute($route,'afterAll');
107
+        $this->callOnRoute($route, 'after')->callOnRoute($route, 'afterAll');
108 108
         return true;
109 109
     }
110 110
 
111
-    protected function callOnRoute(array $route,string $state)
111
+    protected function callOnRoute(array $route, string $state)
112 112
     {
113
-        if($route[$state] !== null){
114
-            if(is_string($route[$state])){
113
+        if ($route[$state] !== null) {
114
+            if (is_string($route[$state])) {
115 115
                 $this->Controller($route[$state]);
116
-            }else{
116
+            }else {
117 117
                 $route[$state]();
118 118
             }
119 119
         }
@@ -124,12 +124,12 @@  discard block
 block discarded – undo
124 124
     {
125 125
         $currentProtocol = $this->getProtocol();
126 126
 
127
-        foreach(array_reverse($this->routers) as $r => $route){
127
+        foreach (array_reverse($this->routers) as $r => $route) {
128 128
 
129 129
             $this->currentRoute = $route;
130 130
             $this->currentRoute['name'] = $r;
131 131
 
132
-            if(!$this->checkProtocol($route['protocol'], $currentProtocol)){
132
+            if (!$this->checkProtocol($route['protocol'], $currentProtocol)) {
133 133
                 continue;
134 134
             }
135 135
 
@@ -139,11 +139,11 @@  discard block
 block discarded – undo
139 139
 
140 140
 
141 141
             $routs = $this->explodeRoutes(
142
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url'],
143
-                (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']
142
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url'],
143
+                (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']
144 144
             );
145 145
 
146
-            if(!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){
146
+            if (!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])) {
147 147
                 continue;
148 148
             }
149 149
 
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
         }
153 153
         
154 154
         $this->currentRoute = null;
155
-	    throw new Exception('Page not found.',404);
155
+	    throw new Exception('Page not found.', 404);
156 156
     }
157 157
 
158 158
     protected function loadByName(?string $routName = null): Router
@@ -161,8 +161,8 @@  discard block
 block discarded – undo
161 161
         $this->checkName($routName);
162 162
         $route = $this->routers[$routName];
163 163
 
164
-        if(!$this->checkProtocol($route['protocol'], $currentProtocol)){
165
-            throw new Exception('Page not found.',404);
164
+        if (!$this->checkProtocol($route['protocol'], $currentProtocol)) {
165
+            throw new Exception('Page not found.', 404);
166 166
         }
167 167
 
168 168
         $this->currentRoute = $route;
Please login to merge, or discard this patch.
src/ControllerTrait.php 1 patch
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -4,11 +4,11 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait ControllerTrait{
7
+trait ControllerTrait {
8 8
 
9 9
     protected function checkControllSettable(string $controll)
10 10
     {
11
-        if(count(explode(':',$controll)) != 2){
11
+        if (count(explode(':', $controll)) != 2) {
12 12
             throw new Exception("Controller {$controll} badly set.");
13 13
         }
14 14
         return $this;
@@ -16,8 +16,8 @@  discard block
 block discarded – undo
16 16
 
17 17
     protected function checkControllExist(string $controll)
18 18
     {
19
-        $controllname = ucfirst(explode(':',$controll)[0]);
20
-        if(!class_exists($controllname)){
19
+        $controllname = ucfirst(explode(':', $controll)[0]);
20
+        if (!class_exists($controllname)) {
21 21
             throw new Exception("No controller {$controllname} found.");
22 22
         }
23 23
         return $this;
@@ -25,9 +25,9 @@  discard block
 block discarded – undo
25 25
 
26 26
     protected function checkControllMethod(string $controll)
27 27
     {
28
-        $controllname = ucfirst(explode(':',$controll)[0]);
29
-        $method = explode(':',$controll)[1];
30
-        if(!method_exists($controllname, $method)){
28
+        $controllname = ucfirst(explode(':', $controll)[0]);
29
+        $method = explode(':', $controll)[1];
30
+        if (!method_exists($controllname, $method)) {
31 31
             throw new Exception("No method {$method} found for {$controllname}.");
32 32
         }
33 33
         return $this;
Please login to merge, or discard this patch.
src/CheckWhere.php 1 patch
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -4,11 +4,11 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait CheckWhere{
7
+trait CheckWhere {
8 8
         
9 9
     protected function checkWhereParam($data): Router
10 10
     {
11
-        if(count($data) === 0){
11
+        if (count($data) === 0) {
12 12
             throw new Exception('It is necessary to define a condition to be tested.');
13 13
         }
14 14
         return $this;
@@ -16,7 +16,7 @@  discard block
 block discarded – undo
16 16
 
17 17
     protected function checkWhereParams($params): Router
18 18
     {
19
-        if(count($params) === 0){
19
+        if (count($params) === 0) {
20 20
             throw new Exception('The route in question has no parameters to be tested.');
21 21
         }
22 22
         return $this;
@@ -26,24 +26,24 @@  discard block
 block discarded – undo
26 26
     {
27 27
         $pass = true;
28 28
 
29
-        if(!is_array($route['where'])){
29
+        if (!is_array($route['where'])) {
30 30
             return $pass;
31 31
         }
32 32
 
33
-        $routeURI = explode('/',$route['url']);
33
+        $routeURI = explode('/', $route['url']);
34 34
         $params = [];
35
-        foreach($routeURI as $p => $part){
36
-            if(!$this->isWhered($part,$request[$p])){
35
+        foreach ($routeURI as $p => $part) {
36
+            if (!$this->isWhered($part, $request[$p])) {
37 37
                 continue;
38 38
             }
39 39
                 
40
-            $param = substr(str_replace('?','',$part),1,-1);
40
+            $param = substr(str_replace('?', '', $part), 1, -1);
41 41
 
42
-            if(array_key_exists($param,$route['where'])){
42
+            if (array_key_exists($param, $route['where'])) {
43 43
                     
44 44
                 $params[$param] = $route['where'][$param];
45 45
 
46
-                if(!preg_match("/^{$params[$param]}$/",$request[$p])){
46
+                if (!preg_match("/^{$params[$param]}$/", $request[$p])) {
47 47
                     $pass = false;
48 48
                 }
49 49
             }
@@ -54,17 +54,17 @@  discard block
 block discarded – undo
54 54
 
55 55
     private function isWhered(string $part, string $value): bool
56 56
     {
57
-        return $this->isParameter($part) && !$this->checkParameterOptional($part,$value);
57
+        return $this->isParameter($part) && !$this->checkParameterOptional($part, $value);
58 58
     }
59 59
 
60 60
     private function checkParameterOptional(string $part, string $value): bool
61 61
     {
62
-        return substr($part,0,2) === '{?' && empty($value);
62
+        return substr($part, 0, 2) === '{?' && empty($value);
63 63
     }
64 64
 
65 65
     protected function isParameter(string $part): bool
66 66
     {
67
-        return (substr($part,0,1) === '{' && substr($part,-1) === '}');
67
+        return (substr($part, 0, 1) === '{' && substr($part, -1) === '}');
68 68
     }
69 69
 
70 70
     protected function callWhereAdd($data): Router
@@ -74,16 +74,16 @@  discard block
 block discarded – undo
74 74
         $data = (count($data) > 1) ? [$data[0] => $data[1]] : $data[0];
75 75
         
76 76
         $route = end($this->routers);
77
-        $routeURI = explode('/',$route['url']);
77
+        $routeURI = explode('/', $route['url']);
78 78
         $params = [];
79
-        foreach($routeURI as $part){
80
-            if(!$this->isParameter($part)){
79
+        foreach ($routeURI as $part) {
80
+            if (!$this->isParameter($part)) {
81 81
                 continue;
82 82
             }
83 83
             
84
-            $param = substr(str_replace('?','',$part),1,-1);
84
+            $param = substr(str_replace('?', '', $part), 1, -1);
85 85
 
86
-            if(array_key_exists($param,$data)){
86
+            if (array_key_exists($param, $data)) {
87 87
                 $params[$param] = $data[$param];
88 88
             }
89 89
                 
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
 
92 92
         $this->checkWhereParams($params);
93 93
 
94
-        $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$params) : $params;
94
+        $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $params) : $params;
95 95
 
96 96
         $this->routers[count($this->routers)-1] = $route;
97 97
 
Please login to merge, or discard this patch.
src/FilterTrait.php 1 patch
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -4,13 +4,13 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait FilterTrait{
7
+trait FilterTrait {
8 8
     protected function checkFiltering(array $route): Router
9 9
     {
10
-        $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ];
10
+        $filters = (is_array($route['filters'])) ? $route['filters'] : [$route['filters']];
11 11
 
12
-        foreach($filters as $filter){
13
-            if(is_null($filter)){
12
+        foreach ($filters as $filter) {
13
+            if (is_null($filter)) {
14 14
                 continue;
15 15
             }
16 16
             $this->checkFilter($filter);
@@ -21,20 +21,20 @@  discard block
 block discarded – undo
21 21
 
22 22
     protected function checkFilter(string $filtername)
23 23
     {
24
-        if(count(explode(':',$filtername)) != 2){
24
+        if (count(explode(':', $filtername)) != 2) {
25 25
             throw new Exception("Wrongly configured filter: {$filtername}.");
26 26
         }
27 27
 
28
-        $filter = $this->checkFilClassExist(explode(':',$filtername)[0]);
28
+        $filter = $this->checkFilClassExist(explode(':', $filtername)[0]);
29 29
 
30
-        if(!$filter->check(explode(':',$filtername)[1])){
31
-            throw new Exception($filter->getMessage(explode(':',$filtername)[1]),403);
30
+        if (!$filter->check(explode(':', $filtername)[1])) {
31
+            throw new Exception($filter->getMessage(explode(':', $filtername)[1]), 403);
32 32
         }
33 33
     }
34 34
 
35 35
     protected function checkFilClassExist(string $class)
36 36
     {
37
-        if(class_exists($class)){
37
+        if (class_exists($class)) {
38 38
             return new $class();
39 39
         }
40 40
         throw new Exception("Filter {$class} not found.");
Please login to merge, or discard this patch.
src/CheckTrait.php 2 patches
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -95,7 +95,7 @@
 block discarded – undo
95 95
     protected function checkExistence(string $url, string $protocol): Router
96 96
     {
97 97
         foreach($this->routers as $key => $value){
98
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
98
+            if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
99 99
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
100 100
             }
101 101
         }
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait CheckTrait{
7
+trait CheckTrait {
8 8
     use FilterTrait, CheckWhere;
9 9
 
10 10
     protected function checkProtocol(string $expected, string $current): bool
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
14 14
 
15 15
     protected function checkName(string $routeName): Router
16 16
     {
17
-        if(!array_key_exists($routeName,$this->routers)){
17
+        if (!array_key_exists($routeName, $this->routers)) {
18 18
             throw new Exception('Page not found.', 404);
19 19
         }
20 20
         return $this;
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
 
23 23
     protected function checkTypeRole($role): Router
24 24
     {
25
-        if(!is_string($role) && @get_class($role) !== 'Closure' ){
25
+        if (!is_string($role) && @get_class($role) !== 'Closure') {
26 26
             throw new Exception("Improperly defined route track.");
27 27
         }
28 28
         return $this;
@@ -35,12 +35,12 @@  discard block
 block discarded – undo
35 35
 
36 36
     protected function checkParameters(array $routeLoop, array $routeRequest): bool
37 37
     {
38
-        foreach($routeLoop as $rr => $param){
39
-            if( (substr($param,0,1) === '{') ){
40
-                $_GET[ substr($param,1,strlen($param)-2) ] = $routeRequest[$rr];    
38
+        foreach ($routeLoop as $rr => $param) {
39
+            if ((substr($param, 0, 1) === '{')) {
40
+                $_GET[substr($param, 1, strlen($param)-2)] = $routeRequest[$rr];    
41 41
             }
42 42
     
43
-            if($this->checkParameter($param, $routeRequest[$rr])){
43
+            if ($this->checkParameter($param, $routeRequest[$rr])) {
44 44
                 return false;
45 45
             }
46 46
         }
@@ -50,12 +50,12 @@  discard block
 block discarded – undo
50 50
 
51 51
     protected function checkParameter(string $routeLoop, string $routeRequest): bool
52 52
     {
53
-        return !( substr($routeLoop,0,1) === '{' ) and $routeLoop !== $routeRequest;
53
+        return !(substr($routeLoop, 0, 1) === '{') and $routeLoop !== $routeRequest;
54 54
     }
55 55
 
56 56
     protected function checkRole(): Router
57 57
     {
58
-        if(!array_key_exists('role', $this->getData()['POST'])){
58
+        if (!array_key_exists('role', $this->getData()['POST'])) {
59 59
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
60 60
         }
61 61
         return $this;
@@ -63,10 +63,10 @@  discard block
 block discarded – undo
63 63
 
64 64
     protected function hasProtocol(array $route, string $currentProtocol): Router
65 65
     {
66
-        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
66
+        $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']];
67 67
 
68
-        foreach($protocols as $protocol){
69
-            if(strtoupper($protocol) !== strtoupper($currentProtocol)){
68
+        foreach ($protocols as $protocol) {
69
+            if (strtoupper($protocol) !== strtoupper($currentProtocol)) {
70 70
                 continue;
71 71
             }
72 72
         }
@@ -76,9 +76,9 @@  discard block
 block discarded – undo
76 76
 
77 77
     protected function checkToHiking($route, $routeRequest, $routeLoop): bool
78 78
     {
79
-        if($this->checkNumparams($routeLoop, $routeRequest) || 
79
+        if ($this->checkNumparams($routeLoop, $routeRequest) || 
80 80
             !$this->checkParameters($routeLoop, $routeRequest) ||
81
-            !$this->checkWhere($route, $routeRequest)){
81
+            !$this->checkWhere($route, $routeRequest)) {
82 82
                 return false;
83 83
         }
84 84
         return true;
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
 
87 87
     protected function hasRouteName(string $name): Router
88 88
     {
89
-        if(array_key_exists($name, $this->routers)){
89
+        if (array_key_exists($name, $this->routers)) {
90 90
             throw new Exception("There is already a route with the name {$name} configured.");
91 91
         }
92 92
         return $this;
@@ -94,8 +94,8 @@  discard block
 block discarded – undo
94 94
 
95 95
     protected function checkExistence(string $url, string $protocol): Router
96 96
     {
97
-        foreach($this->routers as $key => $value){
98
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
97
+        foreach ($this->routers as $key => $value) {
98
+    		if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) {
99 99
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
100 100
             }
101 101
         }
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
 
105 105
     protected function checkInGroup(): Router
106 106
     {
107
-        if($this->lastReturn){
107
+        if ($this->lastReturn) {
108 108
             throw new Exception("At the moment it is not allowed to assign names or tests of parameters in groups..");
109 109
         }
110 110
         return $this;
Please login to merge, or discard this patch.