Passed
Push — master ( 506a77...0d121e )
by Henri
01:23
created
examples/Routes/default.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -3,10 +3,10 @@
 block discarded – undo
3 3
 use HnrAzevedo\Router\Router;
4 4
 
5 5
 
6
-Router::get('/my-account','User:my_account')->filter('User:user_in');
6
+Router::get('/my-account', 'User:my_account')->filter('User:user_in');
7 7
 
8
-Router::get('/{parameter}/{otherparameter}', function($data){
8
+Router::get('/{parameter}/{otherparameter}', function($data) {
9 9
     echo "Parameter 1:{$data['parameter']}, Parameter 2:{$data['otherparameter']}.";
10 10
 });
11 11
 
12
-Router::get('/{controller}/{method}','{controller}:{method}');
13 12
\ No newline at end of file
13
+Router::get('/{controller}/{method}', '{controller}:{method}');
14 14
\ No newline at end of file
Please login to merge, or discard this patch.
src/CheckTrait.php 1 patch
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -4,31 +4,31 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-trait CheckTrait{
7
+trait CheckTrait {
8 8
     use FilterTrait;
9 9
 
10 10
     protected function check_protocol(string $expected, string $current)
11 11
     {
12
-        if($expected !== $current){
13
-            throw new Exception('Page not found.',404);
12
+        if ($expected !== $current) {
13
+            throw new Exception('Page not found.', 404);
14 14
         }
15 15
     }
16 16
 
17
-    protected function check_name(string $route_name){
18
-        if(!array_key_exists($route_name,$this->routers)){
19
-            throw new Exception('Page not found.',404);
17
+    protected function check_name(string $route_name) {
18
+        if (!array_key_exists($route_name, $this->routers)) {
19
+            throw new Exception('Page not found.', 404);
20 20
         }
21 21
     }
22 22
 
23
-    protected function check_typeRole($role){
24
-        if(!is_string($role) && @get_class($role) !== 'Closure' ){
23
+    protected function check_typeRole($role) {
24
+        if (!is_string($role) && @get_class($role) !== 'Closure') {
25 25
             throw new Exception("Improperly defined route track.");
26 26
         }
27 27
     }
28 28
 
29 29
     protected function check_config()
30 30
     {
31
-        if(!defined('ROUTER_CONFIG')){
31
+        if (!defined('ROUTER_CONFIG')) {
32 32
             throw new Exception("Information for loading routes has not been defined.");
33 33
         }
34 34
     }
@@ -40,12 +40,12 @@  discard block
 block discarded – undo
40 40
 
41 41
     protected function check_parameters(array $route_loop, array $route_request): bool
42 42
     {
43
-        foreach($route_loop as $rr => $param){
44
-            if( (substr($param,0,1) === '{') ){
45
-                $_GET[ substr($param,1,strlen($param)-2) ] = $route_request[$rr];    
43
+        foreach ($route_loop as $rr => $param) {
44
+            if ((substr($param, 0, 1) === '{')) {
45
+                $_GET[substr($param, 1, strlen($param)-2)] = $route_request[$rr];    
46 46
             }
47 47
     
48
-            if($this->check_parameter($param, $route_request[$rr])){
48
+            if ($this->check_parameter($param, $route_request[$rr])) {
49 49
                 return false;
50 50
             }
51 51
         }
@@ -54,22 +54,22 @@  discard block
 block discarded – undo
54 54
 
55 55
     protected function check_parameter(string $route_loop, string $route_request)
56 56
     {
57
-        return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request;
57
+        return !(substr($route_loop, 0, 1) === '{') and $route_loop !== $route_request;
58 58
     }
59 59
 
60 60
     protected function check_role()
61 61
     {
62
-        if(!array_key_exists('role', $this->getData()['POST'])){
62
+        if (!array_key_exists('role', $this->getData()['POST'])) {
63 63
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
64 64
         }
65 65
     }
66 66
 
67 67
     protected function hasProtocol(array $route, string $currentProtocol)
68 68
     {
69
-        $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ];
69
+        $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']];
70 70
 
71
-        foreach($protocols as $protocol){
72
-            if($protocol !== $currentProtocol){
71
+        foreach ($protocols as $protocol) {
72
+            if ($protocol !== $currentProtocol) {
73 73
                 continue;
74 74
             }
75 75
         }
Please login to merge, or discard this patch.
src/Helper.php 1 patch
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -2,12 +2,12 @@  discard block
 block discarded – undo
2 2
 
3 3
 namespace HnrAzevedo\Router;
4 4
 
5
-trait Helper{
5
+trait Helper {
6 6
     use CheckTrait, ControllerTrait;
7 7
     
8 8
     protected function getProtocol(): string
9 9
     {
10
-        if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')){
10
+        if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) {
11 11
             return 'ajax';
12 12
         }
13 13
 
@@ -26,13 +26,13 @@  discard block
 block discarded – undo
26 26
     protected function import(string $path)
27 27
     {
28 28
         foreach (scandir($path) as $routeFile) {
29
-            if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){
30
-                require_once($path. DIRECTORY_SEPARATOR .$routeFile);
29
+            if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') {
30
+                require_once($path.DIRECTORY_SEPARATOR.$routeFile);
31 31
             }
32 32
         }
33 33
     }
34 34
 
35
-    protected function ControllerForm($controller, string $method, array $values){
35
+    protected function ControllerForm($controller, string $method, array $values) {
36 36
 		$this->check_role();
37 37
         $role = ($method !== 'method') ? $method : $this->getData()['POST']['role'];
38 38
         $data = (!is_null($values)) ? json_decode($values['data']) : null;
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
         $data = $this->getData();
45 45
 
46 46
         foreach ($data['GET'] as $name => $value) {
47
-            $controll = str_replace('{'.$name.'}',$value,$controll);
47
+            $controll = str_replace('{'.$name.'}', $value, $controll);
48 48
         }
49 49
 
50 50
         $this->check_controllsettable($controll);
@@ -53,11 +53,11 @@  discard block
 block discarded – undo
53 53
 
54 54
         $this->check_controllmethod($controll);
55 55
 
56
-        $controller = 'Controllers\\' . ucfirst(explode(':',$controll)[0]);
56
+        $controller = 'Controllers\\'.ucfirst(explode(':', $controll)[0]);
57 57
         $controller = new $controller();
58
-        $method = explode(':',$controll)[1];
58
+        $method = explode(':', $controll)[1];
59 59
 
60
-        if( ( $this->getProtocol() == 'form') ){
60
+        if (($this->getProtocol() == 'form')) {
61 61
             $this->ControllerForm($controller, $method, $data['POST']);
62 62
         }else {
63 63
             $controller->$method($data);
@@ -66,12 +66,12 @@  discard block
 block discarded – undo
66 66
 
67 67
     protected function explodeRoute(bool $bar, string $url): array
68 68
     {   
69
-        return explode( '/', $bar ? substr($url, 0, -1) : $url );
69
+        return explode('/', $bar ? substr($url, 0, -1) : $url);
70 70
     }
71 71
 
72 72
     protected function toHiking($walking)
73 73
     {
74
-        if(is_string($walking)){
74
+        if (is_string($walking)) {
75 75
             $this->Controller($walking);
76 76
             return true;
77 77
         }
Please login to merge, or discard this patch.
src/Router.php 1 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, $walking, $protocol);
60 60
     }
61 61
 
62
-    public function set($url ,$walking , string $protocol): Router
62
+    public function set($url, $walking, string $protocol): 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
         }
@@ -84,9 +84,9 @@  discard block
 block discarded – undo
84 84
         return self::getInstance();
85 85
     }
86 86
 
87
-    public static function group(string $prefix,$callback): Router
87
+    public static function group(string $prefix, $callback): Router
88 88
     {
89
-        self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix;
89
+        self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix;
90 90
         self::getInstance()->group = sha1(date('d/m/Y h:m:i'));
91 91
         $callback();
92 92
         self::getInstance()->group = null;
@@ -98,14 +98,14 @@  discard block
 block discarded – undo
98 98
     public static function name(string $name): Router
99 99
     {
100 100
 
101
-        if(self::getInstance()->lastReturn){
101
+        if (self::getInstance()->lastReturn) {
102 102
             throw new Exception("There is no reason to call a {$name} route group.");
103 103
         }
104 104
 
105 105
         $currentRoute = end(self::getInstance()->routers);
106 106
 
107
-        foreach(self::getInstance()->routers as $key => $value){
108
-            if(array_key_exists($name, self::getInstance()->routers)){
107
+        foreach (self::getInstance()->routers as $key => $value) {
108
+            if (array_key_exists($name, self::getInstance()->routers)) {
109 109
                 throw new Exception("There is already a route with the name {$name} configured.");
110 110
             }
111 111
         }
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 
122 122
     private function byName(?string $route_name)
123 123
     {
124
-        if(!is_null($route_name)){
124
+        if (!is_null($route_name)) {
125 125
             $currentProtocol = $this->getProtocol();
126 126
 
127 127
             $this->check_name($route_name);
@@ -143,15 +143,15 @@  discard block
 block discarded – undo
143 143
 
144 144
 		$currentProtocol = $this->getProtocol();
145 145
 
146
-        foreach(array_reverse($this->routers) as $r => $route){
146
+        foreach (array_reverse($this->routers) as $r => $route) {
147 147
 
148 148
             $this->hasProtocol($route, $currentProtocol);
149 149
 
150
-            $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']);
150
+            $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']);
151 151
             
152
-            $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']);
152
+            $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']);
153 153
 
154
-	        if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){
154
+	        if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) {
155 155
                 continue;
156 156
             }
157 157
             
@@ -161,18 +161,18 @@  discard block
 block discarded – undo
161 161
 	        return true;
162 162
 	    }
163 163
 
164
-	    throw new Exception('Page not found.',404);
164
+	    throw new Exception('Page not found.', 404);
165 165
     }
166 166
 
167 167
     public static function filter($filters): Router
168 168
     {
169
-        if(self::getInstance()->lastReturn !== null){
169
+        if (self::getInstance()->lastReturn !== null) {
170 170
             $currentGroup = end(self::getInstance()->routers)['group'];
171 171
 
172 172
             foreach (self::getInstance()->routers as $key => $value) {
173 173
 
174
-                if($value['group'] === $currentGroup){
175
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
174
+                if ($value['group'] === $currentGroup) {
175
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
176 176
                     self::getInstance()->routers[$key] = $currentRoute;
177 177
                 }
178 178
 
@@ -181,24 +181,24 @@  discard block
 block discarded – undo
181 181
             return self::getInstance();
182 182
         }
183 183
         
184
-        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
184
+        self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
185 185
         return self::getInstance();
186 186
     }
187 187
 
188 188
     public static function addFilter(array $route, $filter): array
189 189
     {
190
-        if(is_null($route['filters'])){
190
+        if (is_null($route['filters'])) {
191 191
             $route['filters'] = $filter;
192 192
             return $route;
193 193
         }
194 194
 
195 195
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
196 196
 
197
-        if(is_array($route['filters'])){
197
+        if (is_array($route['filters'])) {
198 198
             foreach ($route['filters'] as $key => $value) {
199 199
                 $filters[] = $value;
200 200
             }
201
-        }else{
201
+        }else {
202 202
             $filters[] = $route['filters'];
203 203
         }
204 204
 
Please login to merge, or discard this patch.