Passed
Push — master ( 16f022...ac76b4 )
by Henri
01:51
created
src/PrioritizeTrait.php 1 patch
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace HnrAzevedo\Router;
6 6
 
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
 
13 13
     protected function sorted(?bool $sorted = null): bool
14 14
     {
15
-        if(null !== $sorted) {
15
+        if (null !== $sorted) {
16 16
             $this->sorted = $sorted;
17 17
         }
18 18
         return $this->sorted;
@@ -20,19 +20,19 @@  discard block
 block discarded – undo
20 20
     
21 21
     protected function sortRoutes(): bool
22 22
     {
23
-        if($this->sorted()) {
23
+        if ($this->sorted()) {
24 24
             return true;
25 25
         }
26 26
 
27 27
         $staticRoutes = [];
28 28
         $paramRoutes = [];
29 29
 
30
-        foreach($this->getRoutes() as $r => $route){
30
+        foreach ($this->getRoutes() as $r => $route) {
31 31
 
32 32
             $path = urldecode(unserialize($route['uri'])->getPath());
33 33
 
34
-            if(strstr($path, '{')) {
35
-                $paramRoutes[$this->getKeyArray(substr_count($path, '/') + substr_count($path, '{'), $paramRoutes)] = $route;
34
+            if (strstr($path, '{')) {
35
+                $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route;
36 36
                 continue;    
37 37
             }
38 38
 
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 
49 49
     private function getKeyArray(int $index, array $array): int
50 50
     {
51
-        while(array_key_exists($index, $array)){
51
+        while (array_key_exists($index, $array)) {
52 52
             $index++;
53 53
         }
54 54
         return $index;
@@ -57,8 +57,8 @@  discard block
 block discarded – undo
57 57
     private function orderRoutes(array $routes):void
58 58
     {
59 59
         $kRoutes = $routes;
60
-        foreach($routes as $r => $route){
61
-            if(array_key_exists('name', $route)) {
60
+        foreach ($routes as $r => $route) {
61
+            if (array_key_exists('name', $route)) {
62 62
                 unset($kRoutes[$r]);
63 63
                 $kRoutes["'{$route['name']}'"] = $route;
64 64
             }
Please login to merge, or discard this patch.
src/Router.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace HnrAzevedo\Router;
6 6
 
@@ -35,7 +35,7 @@  discard block
 block discarded – undo
35 35
     {
36 36
         $route = self::getInstance()->inSave();
37 37
 
38
-        if(null === $value) {
38
+        if (null === $value) {
39 39
             return (isset($route['attributes'][$name])) ? $route['attributes'][$name] : null;
40 40
         }
41 41
 
@@ -48,8 +48,8 @@  discard block
 block discarded – undo
48 48
     public static function groupAttribute(string $name, $value = null, ?array $excepts = []): Router
49 49
     {
50 50
         $group = self::getInstance()->inSave()['group'];
51
-        foreach(self::getInstance()->getRoutes() as $r => $route){
52
-            if($route['group'] !== $group || in_array($route['name'], $excepts)) {
51
+        foreach (self::getInstance()->getRoutes() as $r => $route) {
52
+            if ($route['group'] !== $group || in_array($route['name'], $excepts)) {
53 53
                 continue;
54 54
             }
55 55
 
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
     public static function group(string $prefix, \Closure $closure): Router
63 63
     {
64 64
         $id = sha1(date('d/m/Y h:m:i'));
65
-        while(array_key_exists($id, self::getInstance()->groupsName)){
65
+        while (array_key_exists($id, self::getInstance()->groupsName)) {
66 66
             $id = sha1(date('d/m/Y h:m:i').$id);
67 67
         }
68 68
 
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
     {
84 84
         self::getInstance()->loaded = true;
85 85
 
86
-        if(null !== $name) {
86
+        if (null !== $name) {
87 87
             return self::getInstance()->loadByName($name);
88 88
         }
89 89
 
@@ -91,15 +91,15 @@  discard block
 block discarded – undo
91 91
 
92 92
         $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
93 93
 
94
-        foreach(self::getInstance()->getRoutes() as $r => $route){
94
+        foreach (self::getInstance()->getRoutes() as $r => $route) {
95 95
             self::getInstance()->currentRoute = $route;
96 96
             self::getInstance()->currentRoute['name'] = $r;
97 97
 
98
-            try{
98
+            try {
99 99
                 self::getInstance()->checkMethod($route, $requestMethod);
100 100
                 self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath());
101 101
                 return self::getInstance();
102
-            }catch(\Exception $er){
102
+            }catch (\Exception $er) {
103 103
                 continue;
104 104
             }
105 105
         }
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
 
114 114
     public static function run(?string $name = null): RouterInterface
115 115
     {
116
-        if(!self::getInstance()->loaded) {
116
+        if (!self::getInstance()->loaded) {
117 117
             self::getInstance()->load($name);
118 118
         }
119 119
 
@@ -123,10 +123,10 @@  discard block
 block discarded – undo
123 123
 
124 124
         self::getInstance()->executeBefore();
125 125
         
126
-        try{
126
+        try {
127 127
             $action = unserialize(self::getInstance()->current()['action']);
128 128
             self::getInstance()->executeRouteAction((is_string($action)) ? $action : $action->getClosure());
129
-        }catch(\Exception $er){
129
+        }catch (\Exception $er) {
130 130
             self::getInstance()->error = $er;
131 131
         }
132 132
         
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 
140 140
     private function checkError(): void
141 141
     {
142
-        if(isset($this->error)) {
142
+        if (isset($this->error)) {
143 143
             throw $this->error;
144 144
         }
145 145
     }
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 
154 154
     public static function routes(?array $routes = null): array
155 155
     {
156
-        if(null !== $routes) {
156
+        if (null !== $routes) {
157 157
             self::getInstance()->setRoutes($routes);
158 158
             self::getInstance()->sortRoutes();
159 159
         }
Please login to merge, or discard this patch.
examples/Routes/default.php 1 patch
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -7,12 +7,12 @@  discard block
 block discarded – undo
7 7
 /**
8 8
  * Defines a route for the GET method
9 9
  */
10
-Router::get('/foo','HnrAzevedo\Router\Example\Controllers\Controller@method');
10
+Router::get('/foo', 'HnrAzevedo\Router\Example\Controllers\Controller@method');
11 11
 
12 12
 /**
13 13
  * Defines a route to the GET method with an anonymous function
14 14
  */
15
-Router::get('/bar', function(){
15
+Router::get('/bar', function() {
16 16
     //
17 17
     echo '/bar executed!';
18 18
 });
@@ -20,14 +20,14 @@  discard block
 block discarded – undo
20 20
 /**
21 21
  * Defines a route to the POST method with an anonymous function
22 22
  */
23
-Router::post('/bar', function(){
23
+Router::post('/bar', function() {
24 24
     //
25 25
 });
26 26
 
27 27
 /**
28 28
  * Defining route for any method
29 29
  */
30
-Router::any('/any', function(){
30
+Router::any('/any', function() {
31 31
     //
32 32
 });
33 33
 
@@ -37,14 +37,14 @@  discard block
 block discarded – undo
37 37
  * @param string $route
38 38
  * @param \Closure|string $action
39 39
  */
40
-Router::match('GET|post', '/get_post', function(){
40
+Router::match('GET|post', '/get_post', function() {
41 41
     //
42 42
 });
43 43
 
44 44
 /**
45 45
  * Defines the route name
46 46
  */
47
-Router::get('/named', function(){
47
+Router::get('/named', function() {
48 48
     //
49 49
 })->name('baz');
50 50
 
@@ -56,32 +56,32 @@  discard block
 block discarded – undo
56 56
 /**
57 57
  * Run before all requests, regardless of error occurrences
58 58
  */
59
-Router::beforeAll(function(){
59
+Router::beforeAll(function() {
60 60
     //
61 61
 }, null);
62 62
 
63 63
 /**
64 64
  * Run after all requests, regardless of error occurrences
65 65
  */
66
-Router::afterAll(function(){
66
+Router::afterAll(function() {
67 67
     //
68 68
 }, null);
69 69
 
70 70
 /**
71 71
  * Executes after executing the triggered route action
72 72
  */
73
-Router::get('/after', function(){
73
+Router::get('/after', function() {
74 74
     //
75
-})->after(function(){
75
+})->after(function() {
76 76
     //
77 77
 });
78 78
 
79 79
 /**
80 80
  * Executes before executing the triggered route action
81 81
  */
82
-Router::get('/before', function(){
82
+Router::get('/before', function() {
83 83
     //
84
-})->before(function(){
84
+})->before(function() {
85 85
     //
86 86
 });
87 87
 /**
@@ -102,21 +102,21 @@  discard block
 block discarded – undo
102 102
 /**
103 103
  * Example of route definition with parameters
104 104
  */
105
-Router::get('/passingParameters/{param}', function($param){
105
+Router::get('/passingParameters/{param}', function($param) {
106 106
     echo $param;
107 107
 });
108 108
 
109 109
 /**
110 110
  * Example of setting an optional parameter
111 111
  */
112
-Router::get('/passingParameters/{?optionalParam}', function($optionalParam){
112
+Router::get('/passingParameters/{?optionalParam}', function($optionalParam) {
113 113
     echo $optionalParam;
114 114
 });
115 115
 
116 116
 /**
117 117
  * Example of regular expression test for parameters
118 118
  */
119
-Router::get('/testingParameters/{param}', function($param){
119
+Router::get('/testingParameters/{param}', function($param) {
120 120
     echo $param;
121 121
 })->where([
122 122
     'param'=>'[0-9]{1,11}'
@@ -132,16 +132,16 @@  discard block
 block discarded – undo
132 132
  * @param string $prefix
133 133
  * @param \Closure $routeDefinitions
134 134
  */
135
-Router::group('/admin', function(){
136
-    Router::get('/users/{teste}', function($teste){
135
+Router::group('/admin', function() {
136
+    Router::get('/users/{teste}', function($teste) {
137 137
         echo $teste;
138 138
     })->where([
139 139
         'teste'=>'[a-zA-Z]*'
140 140
     ]);
141 141
 });
142 142
 
143
-Router::group('/admin2', function(){
144
-    Router::get('/users/{teste}', function($teste){
143
+Router::group('/admin2', function() {
144
+    Router::get('/users/{teste}', function($teste) {
145 145
         echo $teste;
146 146
     })->name('teste');
147 147
 })
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
  */
153 153
 ->groupWhere([
154 154
     'teste'=>'[a-zA-Z]*'
155
-],[]);
155
+], []);
156 156
 
157 157
 
158 158
 
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
 /**
170 170
  * Defining route middleware - implements Psr\Http\Server\MiddlewareInterface
171 171
  */
172
-Router::get('/passingParameters/{?optionalParam}', function($optionalParam = null){
172
+Router::get('/passingParameters/{?optionalParam}', function($optionalParam = null) {
173 173
     echo $optionalParam;
174 174
 })->middleware([
175 175
     HnrAzevedo\Router\Example\Middleware\Auth::class
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
 /**
179 179
  * Defining middleware by nickname
180 180
  */
181
-Router::get('/lasted', function(){
181
+Router::get('/lasted', function() {
182 182
     //
183 183
 })->middleware([
184 184
     'Lasted'
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
  * Defining multiple middlewares
189 189
  * NOTE: Importantly, the execution follows the same order of definition
190 190
  */
191
-Router::get('/middlewares', function(){
191
+Router::get('/middlewares', function() {
192 192
     //
193 193
 })->middleware([
194 194
     HnrAzevedo\Router\Example\Middleware\Auth::class,
Please login to merge, or discard this patch.
examples/AttributeExample.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 
8 8
 use HnrAzevedo\Router\Router;
9 9
 
10
-try{
10
+try {
11 11
 
12 12
     /**
13 13
      * Add to composer.json autoload
@@ -35,7 +35,7 @@  discard block
 block discarded – undo
35 35
      */ 
36 36
     $action = Router::currentAction();
37 37
 
38
-}catch(Exception $er){
38
+}catch (Exception $er) {
39 39
 
40 40
     die("Code Error: {$er->getCode()}".PHP_EOL."Line: {$er->getLine()}".PHP_EOL."File: {$er->getFile()}".PHP_EOL."Message: {$er->getMessage()}.");
41 41
 
Please login to merge, or discard this patch.
examples/Controllers/Controller.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@
 block discarded – undo
2 2
 
3 3
 namespace HnrAzevedo\Router\Example\Controllers;
4 4
 
5
-class Controller{
5
+class Controller {
6 6
 
7 7
     public function method(): void
8 8
     {
Please login to merge, or discard this patch.
examples/MiddlewareExample.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -14,15 +14,15 @@  discard block
 block discarded – undo
14 14
 use HnrAzevedo\Router\Router;
15 15
 use Psr\Http\Server\MiddlewareInterface;
16 16
 
17
-try{
17
+try {
18 18
     $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
19 19
 
20 20
     $serverRequest = (new Factory())->createServerRequest($requestMethod, new Uri($_SERVER['REQUEST_URI']));
21 21
 
22
-    class App implements MiddlewareInterface{
22
+    class App implements MiddlewareInterface {
23 23
         public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
24 24
         {
25
-            if(empty($request->getAttribute('route')))
25
+            if (empty($request->getAttribute('route')))
26 26
             {
27 27
                 throw new Exception('Page not found', 404);
28 28
             }
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
         }
34 34
     }
35 35
 
36
-    define('GLOBAL_MIDDLEWARES',[
36
+    define('GLOBAL_MIDDLEWARES', [
37 37
         Router::class,
38 38
         App::class
39 39
     ]);
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 
70 70
     function runMiddlewares($serverRequest)
71 71
     {
72
-        nextExample(new class implements RequestHandlerInterface{
72
+        nextExample(new class implements RequestHandlerInterface {
73 73
             public function handle(ServerRequestInterface $request): ResponseInterface
74 74
             {
75 75
                 return (new Factory())->createResponse(200);
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 
80 80
     runMiddlewares($serverRequest);
81 81
 
82
-}catch(Exception $er){
82
+}catch (Exception $er) {
83 83
 
84 84
     die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}.");
85 85
 
Please login to merge, or discard this patch.
src/Route.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -12,8 +12,8 @@
 block discarded – undo
12 12
         private array $methods,
13 13
         private string $name,
14 14
         private ?array $where,
15
-        private ?string|?\Closure $after,
16
-        private ?string|?\Closure $before,
15
+        private ?string | ?\Closure $after,
16
+        private ?string | ?\Closure $before,
17 17
         private ?array $attributes,
18 18
         private ?array $middleware
19 19
     ) {}
Please login to merge, or discard this patch.
examples/Controllers/ControllerAttribute.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@
 block discarded – undo
4 4
 
5 5
 use HnrAzevedo\Router\Route;
6 6
 
7
-class ControllerAttribute{
7
+class ControllerAttribute {
8 8
 
9 9
     #[Route(
10 10
         uri:'/fooo/{param}',
Please login to merge, or discard this patch.
src/AttributeTrait.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types = 1);
3
+declare(strict_types=1);
4 4
 
5 5
 namespace HnrAzevedo\Router;
6 6
 
@@ -29,23 +29,23 @@  discard block
 block discarded – undo
29 29
 
30 30
     public static function loadPipeline(): void
31 31
     {
32
-        foreach(self::getInstance()->getPipeline() as $line){
32
+        foreach (self::getInstance()->getPipeline() as $line) {
33 33
             self::getInstance()->loadLine(new ReflectionObject(new $line()));
34 34
         }
35 35
     }
36 36
 
37 37
     private function loadLine(ReflectionObject $reflection): void
38 38
     {
39
-        foreach($reflection->getMethods() as $method){
39
+        foreach ($reflection->getMethods() as $method) {
40 40
             $this->loadMethod($method);
41 41
         }
42 42
     }
43 43
 
44 44
     private function loadMethod(ReflectionMethod $method): void
45 45
     {
46
-        try{
46
+        try {
47 47
             foreach ($method->getAttributes() as $attr) {
48
-                if($attr->getName() != 'HnrAzevedo\Router\Route') continue;
48
+                if ($attr->getName() != 'HnrAzevedo\Router\Route') continue;
49 49
 
50 50
                 $args = $attr->getArguments();
51 51
     
@@ -64,14 +64,14 @@  discard block
 block discarded – undo
64 64
                     ->attrWhere($args)
65 65
                     ->attrMiddleware($args);
66 66
             }
67
-        }catch(Exception $er){
67
+        }catch (Exception $er) {
68 68
             throw new Exception('Failed to add route via attribute: '.$er->getMessage());
69 69
         }
70 70
     }
71 71
 
72 72
     private function checkArgs(array $args): self
73 73
     {
74
-        if(!array_key_exists('uri', $args) && !array_key_exists(0, $args)) {
74
+        if (!array_key_exists('uri', $args) && !array_key_exists(0, $args)) {
75 75
             throw new Exception('Misconfigured route attribute');
76 76
         }
77 77
         return $this;
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 
80 80
     private function attrName(array $attr): self
81 81
     {
82
-        if(array_key_exists('name', $attr)) {
82
+        if (array_key_exists('name', $attr)) {
83 83
             self::getInstance()->name($attr['name']);
84 84
         }
85 85
         return $this;
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 
88 88
     private function attrBefore(array $attr): self
89 89
     {
90
-        if(array_key_exists('before', $attr)) {
90
+        if (array_key_exists('before', $attr)) {
91 91
             self::getInstance()->before($attr['before']);
92 92
         }
93 93
         return $this;
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 
96 96
     private function attrAfter(array $attr): self
97 97
     {
98
-        if(array_key_exists('after', $attr)) {
98
+        if (array_key_exists('after', $attr)) {
99 99
             self::getInstance()->after($attr['after']);
100 100
         }
101 101
         return $this;
@@ -103,8 +103,8 @@  discard block
 block discarded – undo
103 103
 
104 104
     private function attrAttributes(array $attr): self
105 105
     {
106
-        if(array_key_exists('attributes', $attr)) {
107
-            foreach($attr['attributes'] as $attribute => $attrValue){
106
+        if (array_key_exists('attributes', $attr)) {
107
+            foreach ($attr['attributes'] as $attribute => $attrValue) {
108 108
                 self::getInstance()->attribute($attribute, $attrValue);
109 109
             }
110 110
         }
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
 
114 114
     private function attrWhere(array $attr): self
115 115
     {
116
-        if(array_key_exists('where', $attr)) {
116
+        if (array_key_exists('where', $attr)) {
117 117
             self::getInstance()->where($attr['where']);
118 118
         }
119 119
         return $this;
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
 
122 122
     private function attrMiddleware(array $attr): self
123 123
     {
124
-        if(array_key_exists('middleware', $attr)) {
124
+        if (array_key_exists('middleware', $attr)) {
125 125
             self::getInstance()->middleware($attr['middleware']);
126 126
         }
127 127
         return $this;
Please login to merge, or discard this patch.