Passed
Branch master (0fcf32)
by Henri
01:14
created
src/CheckTrait.php 1 patch
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -4,27 +4,27 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait CheckTrait{
7
+trait CheckTrait {
8 8
 
9 9
     protected function check_protocol(string $expected, string $current)
10 10
     {
11
-        if($expected !== $current){
12
-            throw new Exception('Page not found.',404);
11
+        if ($expected !== $current) {
12
+            throw new Exception('Page not found.', 404);
13 13
         }
14 14
     }
15 15
 
16
-    protected function check_name(string $route_name){
17
-        if(!array_key_exists($route_name,$this->routers)){
18
-            throw new Exception('Page not found.',404);
16
+    protected function check_name(string $route_name) {
17
+        if (!array_key_exists($route_name, $this->routers)) {
18
+            throw new Exception('Page not found.', 404);
19 19
         }
20 20
     }
21 21
 
22 22
     protected function check_filtering(array $route)
23 23
     {
24
-        $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ];
24
+        $filters = (is_array($route['filters'])) ? $route['filters'] : [$route['filters']];
25 25
 
26
-        foreach($filters as $filter){
27
-            if(is_null($filter)){
26
+        foreach ($filters as $filter) {
27
+            if (is_null($filter)) {
28 28
                 continue;
29 29
             }
30 30
             $this->filter->filtering($filter);
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
 
34 34
     protected function check_config()
35 35
     {
36
-        if(!defined('ROUTER_CONFIG')){
36
+        if (!defined('ROUTER_CONFIG')) {
37 37
             throw new Exception("Information for loading routes has not been defined.");
38 38
         }
39 39
     }
@@ -45,12 +45,12 @@  discard block
 block discarded – undo
45 45
 
46 46
     protected function check_parameters(array $route_loop, array $route_request)
47 47
     {
48
-        foreach($route_loop as $rr => $param){
49
-            if( (substr($param,0,1) === '{') ){
50
-                $data[ substr($param,1,strlen($param)-2) ] = $route_request[$rr];    
48
+        foreach ($route_loop as $rr => $param) {
49
+            if ((substr($param, 0, 1) === '{')) {
50
+                $data[substr($param, 1, strlen($param)-2)] = $route_request[$rr];    
51 51
             }
52 52
     
53
-            if($this->check_parameter($param, $route_request[$rr])){
53
+            if ($this->check_parameter($param, $route_request[$rr])) {
54 54
                 return false;
55 55
             }
56 56
         }
@@ -58,22 +58,22 @@  discard block
 block discarded – undo
58 58
 
59 59
     protected function check_parameter(string $route_loop, string $route_request)
60 60
     {
61
-        return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request;
61
+        return !(substr($route_loop, 0, 1) === '{') and $route_loop !== $route_request;
62 62
     }
63 63
 
64 64
     protected function check_role()
65 65
     {
66
-        if(!array_key_exists('role', $this->getData()['POST'])){
66
+        if (!array_key_exists('role', $this->getData()['POST'])) {
67 67
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
68 68
         }
69 69
     }
70 70
 
71 71
     protected function hasProtocol(array $route, string $currentProtocol)
72 72
     {
73
-        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
73
+        $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']];
74 74
 
75
-        foreach($protocols as $protocol){
76
-            if($protocol !== $currentProtocol){
75
+        foreach ($protocols as $protocol) {
76
+            if ($protocol !== $currentProtocol) {
77 77
                 continue;
78 78
             }
79 79
         }
Please login to merge, or discard this patch.
src/Router.php 2 patches
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -61,23 +61,23 @@  discard block
 block discarded – undo
61 61
 
62 62
     public function set($url , $role , $protocol = null): Router
63 63
     {
64
-		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
64
+        $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
65 65
 
66
-    	foreach($this->routers as $key => $value){
67
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
66
+        foreach($this->routers as $key => $value){
67
+            if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
68 68
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
69 69
             }
70
-    	}
70
+        }
71 71
 
72
-		$route = [
73
-			'url' => $this->prefix.$url,
74
-			'role' => $role,
75
-			'protocol' => $protocol,
76
-			'filters' => null,
72
+        $route = [
73
+            'url' => $this->prefix.$url,
74
+            'role' => $role,
75
+            'protocol' => $protocol,
76
+            'filters' => null,
77 77
             'group' => self::getInstance()->group
78
-		];
78
+        ];
79 79
 
80
-		$this->routers[] = $route;		
80
+        $this->routers[] = $route;		
81 81
         
82 82
         return self::getInstance();
83 83
     }
@@ -140,13 +140,13 @@  discard block
 block discarded – undo
140 140
     {
141 141
         $this->byName($route_name);
142 142
 
143
-		$currentProtocol = $this->getProtocol();
143
+        $currentProtocol = $this->getProtocol();
144 144
 
145 145
         foreach(array_reverse($this->routers) as $r => $route){
146 146
 
147 147
             $this->hasProtocol($route, $currentProtocol);
148 148
 
149
-	        $route_loop = explode(
149
+            $route_loop = explode(
150 150
                 '/',
151 151
                 (substr($route['url'],strlen($route['url'])-1,1) === '/') 
152 152
                     ? substr($route['url'], 0, -1) 
@@ -160,17 +160,17 @@  discard block
 block discarded – undo
160 160
                 : $_SERVER['REQUEST_URI'] 
161 161
             );
162 162
 
163
-	        if($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)){
163
+            if($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)){
164 164
                 continue;
165 165
             }
166 166
             
167 167
             $this->check_filtering($route);
168 168
 
169 169
             $this->Controller($route['role']);
170
-	        return true;
171
-	    }
170
+            return true;
171
+        }
172 172
 
173
-	    throw new Exception('Page not found.',404);
173
+        throw new Exception('Page not found.',404);
174 174
     }
175 175
 
176 176
     public static function filter($filters): Router
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 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
-class Router{
7
+class Router {
8 8
     use Helper, CheckTrait;
9 9
 
10 10
     private static $instance = null;
@@ -59,12 +59,12 @@  discard block
 block discarded – undo
59 59
         return self::getInstance()->set($uri, $controll, $protocol);
60 60
     }
61 61
 
62
-    public function set($url , $role , $protocol = null): Router
62
+    public function set($url, $role, $protocol = null): Router
63 63
     {
64
-		$url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url;
64
+		$url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url;
65 65
 
66
-    	foreach($this->routers as $key => $value){
67
-    		if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){
66
+    	foreach ($this->routers as $key => $value) {
67
+    		if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) {
68 68
                 throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured.");
69 69
             }
70 70
     	}
@@ -82,9 +82,9 @@  discard block
 block discarded – undo
82 82
         return self::getInstance();
83 83
     }
84 84
 
85
-    public static function group(string $prefix,$callback): Router
85
+    public static function group(string $prefix, $callback): Router
86 86
     {
87
-        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
87
+        self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix;
88 88
         self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
89 89
         $callback();
90 90
         self::getInstance()->group = null;
@@ -96,14 +96,14 @@  discard block
 block discarded – undo
96 96
     public static function name(string $name): Router
97 97
     {
98 98
 
99
-        if(self::getInstance()->lastReturn){
99
+        if (self::getInstance()->lastReturn) {
100 100
             throw new Exception("There is no reason to call a {$name} route group.");
101 101
         }
102 102
 
103 103
         $currentRoute = end(self::getInstance()->routers);
104 104
 
105
-        foreach(self::getInstance()->routers as $key => $value){
106
-            if(array_key_exists($name, self::getInstance()->routers)){
105
+        foreach (self::getInstance()->routers as $key => $value) {
106
+            if (array_key_exists($name, self::getInstance()->routers)) {
107 107
                 throw new Exception("There is already a route with the name {$name} configured.");
108 108
             }
109 109
         }
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
 
120 120
     private function byName(?string $route_name)
121 121
     {
122
-        if(!is_null($route_name)){
122
+        if (!is_null($route_name)) {
123 123
             $currentProtocol = $this->getProtocol();
124 124
 
125 125
             $this->check_name($route_name);
@@ -142,25 +142,25 @@  discard block
 block discarded – undo
142 142
 
143 143
 		$currentProtocol = $this->getProtocol();
144 144
 
145
-        foreach(array_reverse($this->routers) as $r => $route){
145
+        foreach (array_reverse($this->routers) as $r => $route) {
146 146
 
147 147
             $this->hasProtocol($route, $currentProtocol);
148 148
 
149 149
 	        $route_loop = explode(
150 150
                 '/',
151
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') 
151
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/') 
152 152
                     ? substr($route['url'], 0, -1) 
153 153
                     : $route['url'] 
154 154
             );
155 155
 
156 156
             $route_request = explode(
157 157
                 '/',
158
-                (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') 
158
+                (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/') 
159 159
                 ? substr($_SERVER['REQUEST_URI'], 0, -1) 
160 160
                 : $_SERVER['REQUEST_URI'] 
161 161
             );
162 162
 
163
-	        if($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)){
163
+	        if ($this->check_numparams($route_loop, $route_request) || $this->check_parameters($route_loop, $route_request)) {
164 164
                 continue;
165 165
             }
166 166
             
@@ -170,18 +170,18 @@  discard block
 block discarded – undo
170 170
 	        return true;
171 171
 	    }
172 172
 
173
-	    throw new Exception('Page not found.',404);
173
+	    throw new Exception('Page not found.', 404);
174 174
     }
175 175
 
176 176
     public static function filter($filters): Router
177 177
     {
178
-        if(self::getInstance()->lastReturn !== null){
178
+        if (self::getInstance()->lastReturn !== null) {
179 179
             $currentGroup = end(self::getInstance()->routers)['group'];
180 180
 
181 181
             foreach (self::getInstance()->routers as $key => $value) {
182 182
 
183
-                if($value['group'] === $currentGroup){
184
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
183
+                if ($value['group'] === $currentGroup) {
184
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
185 185
                     self::getInstance()->routers[$key] = $currentRoute;
186 186
                 }
187 187
 
@@ -190,24 +190,24 @@  discard block
 block discarded – undo
190 190
             return self::getInstance();
191 191
         }
192 192
         
193
-        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
193
+        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
194 194
         return self::getInstance();
195 195
     }
196 196
 
197 197
     public static function addFilter(array $route, $filter): array
198 198
     {
199
-        if(is_null($route['filters'])){
199
+        if (is_null($route['filters'])) {
200 200
             $route['filters'] = $filter;
201 201
             return $route;
202 202
         }
203 203
 
204 204
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
205 205
 
206
-        if(is_array($route['filters'])){
206
+        if (is_array($route['filters'])) {
207 207
             foreach ($route['filters'] as $key => $value) {
208 208
                 $filters[] = $value;
209 209
             }
210
-        }else{
210
+        }else {
211 211
             $filters[] = $route['filters'];
212 212
         }
213 213
 
Please login to merge, or discard this patch.