Passed
Push — master ( b146cb...4e125c )
by Henri
01:36
created
src/MiddlewareTrait.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
 
@@ -22,8 +22,8 @@  discard block
 block discarded – undo
22 22
 
23 23
     public static function globalMiddlewares(array $middlewares): RouterInterface
24 24
     {
25
-        foreach($middlewares as $middleware){
26
-            if(!class_exists($middleware)){
25
+        foreach ($middlewares as $middleware) {
26
+            if (!class_exists($middleware)) {
27 27
                 throw new \RuntimeException("Middleware class {$middleware} not exists");
28 28
             }
29 29
         }
@@ -39,8 +39,8 @@  discard block
 block discarded – undo
39 39
     public static function middleware(array $middlewares): RouterInterface
40 40
     {
41 41
         $route = self::getInstance()->inSave();
42
-        $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares;
43
-        self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes()));
42
+        $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares;
43
+        self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes()));
44 44
         return self::getInstance();
45 45
     }
46 46
 
@@ -48,8 +48,8 @@  discard block
 block discarded – undo
48 48
     {
49 49
         $excepts = (is_array($excepts)) ? $excepts : [];
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
 
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 
62 62
     private static function existMiddleware(string $name): void
63 63
     {
64
-        if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){
64
+        if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) {
65 65
             throw new \RuntimeException("Middleware {$name} does not exist");
66 66
         }
67 67
     }
@@ -70,9 +70,9 @@  discard block
 block discarded – undo
70 70
     {
71 71
         $factory = new Factory();
72 72
 
73
-        $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], unserialize($this->current()['uri']),['route' => $this->current()]) : $this->serverRequest;
73
+        $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], unserialize($this->current()['uri']), ['route' => $this->current()]) : $this->serverRequest;
74 74
         
75
-        foreach ($this->current()['middlewares'] as $middleware){
75
+        foreach ($this->current()['middlewares'] as $middleware) {
76 76
             $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware]();
77 77
         }
78 78
 
@@ -87,20 +87,20 @@  discard block
 block discarded – undo
87 87
 
88 88
     public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
89 89
     {
90
-        if(!$this->getInstance()->loaded()){
90
+        if (!$this->getInstance()->loaded()) {
91 91
             $this->getInstance()->load();
92 92
         }
93 93
 
94 94
         $route = $this->getInstance()->current();
95
-        if(!empty($route)){
96
-            $route['action'] = function(){
95
+        if (!empty($route)) {
96
+            $route['action'] = function() {
97 97
                 $this->getInstance()->executeBefore();
98 98
                 $this->getInstance()->executeRouteAction($this->getInstance()->current()['action']);
99 99
                 $this->getInstance()->executeAfter();
100 100
             };
101 101
         }
102 102
 
103
-        return $this->next($handler)->handle($request->withAttribute('route',$route));
103
+        return $this->next($handler)->handle($request->withAttribute('route', $route));
104 104
     }
105 105
 
106 106
     private function next(RequestHandlerInterface $defaultHandler): RequestHandlerInterface
Please login to merge, or discard this patch.
src/RunInTrait.php 1 patch
Spacing   +26 added lines, -26 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
 
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
         $this->beforeAll = (!isset($this->beforeAll)) ? function() {} : $this->beforeAll;
21 21
         $this->afterAll = (!isset($this->afterAll)) ? function() {} : $this->afterAll;
22 22
         
23
-        if($state === 'before'){
23
+        if ($state === 'before') {
24 24
             return ($except) ? $this->beforeExcepts : $this->beforeAll;
25 25
         }
26 26
 
@@ -29,8 +29,8 @@  discard block
 block discarded – undo
29 29
 
30 30
     protected function setState(string $state, $settable, bool $except = false): bool
31 31
     {
32
-        if($state === 'before'){
33
-            if($except){
32
+        if ($state === 'before') {
33
+            if ($except) {
34 34
                 $this->beforeExcepts = $settable;
35 35
                 return true;
36 36
             }
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
             return true;
40 40
         }
41 41
 
42
-        if($except){
42
+        if ($except) {
43 43
             $this->afterExcepts = $settable;
44 44
             return true;
45 45
         }
@@ -60,14 +60,14 @@  discard block
 block discarded – undo
60 60
 
61 61
     public static function beforeAll($closure, ?array $excepts = null): RouterInterface
62 62
     {
63
-        self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [] ,true);
63
+        self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [], true);
64 64
         self::getInstance()->setState('before', $closure, false);
65 65
         return self::getInstance();
66 66
     }
67 67
 
68 68
     public static function afterAll($closure, ?array $excepts = null): RouterInterface
69 69
     {
70
-        self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [] ,true);
70
+        self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [], true);
71 71
         self::getInstance()->setState('after', $closure, false);
72 72
         return self::getInstance();
73 73
     }
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 
85 85
     protected function executeRouteAction($action): bool
86 86
     {
87
-        if(is_callable($action)){        
87
+        if (is_callable($action)) {        
88 88
             call_user_func_array($action, $_REQUEST);
89 89
             return true;
90 90
         }
@@ -99,9 +99,9 @@  discard block
 block discarded – undo
99 99
         $excepts = (is_array($excepts)) ? $excepts : [];
100 100
         $group = self::getInstance()->inSave()['group'];
101 101
 
102
-        foreach(self::getInstance()->getRoutes() as $r => $route){
103
-            if($route['group'] === $group && !in_array($r, $excepts)){
104
-                self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); 
102
+        foreach (self::getInstance()->getRoutes() as $r => $route) {
103
+            if ($route['group'] === $group && !in_array($r, $excepts)) {
104
+                self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); 
105 105
             }
106 106
         }
107 107
 
@@ -111,14 +111,14 @@  discard block
 block discarded – undo
111 111
     private static function addInRoute(string $state, $closure): RouterInterface
112 112
     {
113 113
         $route = self::getInstance()->inSave();
114
-        $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]);
115
-        self::updateRoute($route,array_key_last(self::getInstance()->getRoutes()));
114
+        $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]);
115
+        self::updateRoute($route, array_key_last(self::getInstance()->getRoutes()));
116 116
         return self::getInstance();
117 117
     }
118 118
 
119 119
     protected function executeBefore(): void
120 120
     {
121
-        if(!in_array($this->currentName(), (array) $this->getState('before', true))){
121
+        if (!in_array($this->currentName(), (array) $this->getState('before', true))) {
122 122
             ($this->getState('before', false))();
123 123
         }
124 124
 
@@ -129,15 +129,15 @@  discard block
 block discarded – undo
129 129
     {
130 130
         $this->executeState('after');
131 131
 
132
-        if(!in_array($this->currentName(), (array) $this->getState('after', true))){
132
+        if (!in_array($this->currentName(), (array) $this->getState('after', true))) {
133 133
             ($this->getState('after', false))();
134 134
         }
135 135
     }
136 136
 
137 137
     private function executeState(string $stage): void
138 138
     {
139
-        foreach($this->current()[$stage] as $state){
140
-            if(is_callable($state)){
139
+        foreach ($this->current()[$stage] as $state) {
140
+            if (is_callable($state)) {
141 141
                 $state();
142 142
                 continue;
143 143
             }
@@ -148,31 +148,31 @@  discard block
 block discarded – undo
148 148
 
149 149
     private function executeController(string $controllerMeth): void
150 150
     {
151
-        if(count(explode('@',$controllerMeth)) !== 2){
151
+        if (count(explode('@', $controllerMeth)) !== 2) {
152 152
             $path = urldecode($this->current()['uri']->getPath());
153 153
             throw new \RuntimeException("Misconfigured route action ({$path})");
154 154
         }
155 155
 
156
-        $controller = (string) explode('@',$controllerMeth)[0];
157
-        $method = (string) explode('@',$controllerMeth)[1];
156
+        $controller = (string) explode('@', $controllerMeth)[0];
157
+        $method = (string) explode('@', $controllerMeth)[1];
158 158
 
159 159
         $this->checkControllerMeth($controllerMeth);
160 160
 
161
-        call_user_func_array([(new $controller()),$method], $_REQUEST);
161
+        call_user_func_array([(new $controller()), $method], $_REQUEST);
162 162
     }
163 163
 
164 164
     private function checkControllerMeth(string $controllerMeth): void
165 165
     {
166
-        $routeURI = str_replace(['{:','{','}'],'',urldecode(unserialize($this->current()['uri'])->getPath()));
166
+        $routeURI = str_replace(['{:', '{', '}'], '', urldecode(unserialize($this->current()['uri'])->getPath()));
167 167
 
168
-        $controller = (string) explode('@',$controllerMeth)[0];
169
-        $method = (string) explode('@',$controllerMeth)[1];
168
+        $controller = (string) explode('@', $controllerMeth)[0];
169
+        $method = (string) explode('@', $controllerMeth)[1];
170 170
 
171
-        if(!class_exists($controller)){
171
+        if (!class_exists($controller)) {
172 172
             throw new \RuntimeException("Controller not found in route URI {$routeURI}");
173 173
         }
174 174
 
175
-        if(!method_exists($controller, $method)){
175
+        if (!method_exists($controller, $method)) {
176 176
             throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}");
177 177
         }
178 178
         
Please login to merge, or discard this patch.