| Conditions | 16 |
| Paths | 806 |
| Total Lines | 174 |
| Code Lines | 101 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 62 | public function getRoutes(string ...$classes): array |
||
| 63 | { |
||
| 64 | $routes = []; |
||
| 65 | $attributes = []; |
||
| 66 | |||
| 67 | foreach ($classes as $class) { |
||
| 68 | /** @var Route[] $classAttributes */ |
||
| 69 | $classAttributes = $this->attributes->forClass($class, Route::class); |
||
| 70 | /** @var Route[] $memberAttributes */ |
||
| 71 | $memberAttributes = $this->attributes->forClassMembers($class, Route::class); |
||
| 72 | |||
| 73 | // If this class has attributes |
||
| 74 | if ($classAttributes !== []) { |
||
| 75 | // Iterate through all the class attributes |
||
| 76 | foreach ($classAttributes as $classAttribute) { |
||
| 77 | $classAttribute = $classAttribute->withParameters( |
||
| 78 | ...$classAttribute->getParameters(), |
||
| 79 | ...$this->attributes->forClass($class, Parameter::class) |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** @var class-string[] $middlewareClasses */ |
||
| 83 | $middlewareClasses = array_column( |
||
| 84 | $this->attributes->forClass($class, Middleware::class), |
||
| 85 | 'name' |
||
| 86 | ); |
||
| 87 | |||
| 88 | foreach ($middlewareClasses as $middlewareClass) { |
||
| 89 | $classAttribute = match (true) { |
||
| 90 | is_a($middlewareClass, RouteMatchedMiddleware::class, true) => $classAttribute->withAddedRouteMatchedMiddleware( |
||
| 91 | $middlewareClass |
||
| 92 | ), |
||
| 93 | is_a($middlewareClass, RouteDispatchedMiddleware::class, true) => $classAttribute->withAddedRouteDispatchedMiddleware( |
||
| 94 | $middlewareClass |
||
| 95 | ), |
||
| 96 | is_a($middlewareClass, ThrowableCaughtMiddleware::class, true) => $classAttribute->withAddedThrowableCaughtMiddleware( |
||
| 97 | $middlewareClass |
||
| 98 | ), |
||
| 99 | is_a($middlewareClass, SendingResponseMiddleware::class, true) => $classAttribute->withAddedSendingResponseMiddleware( |
||
| 100 | $middlewareClass |
||
| 101 | ), |
||
| 102 | is_a($middlewareClass, TerminatedMiddleware::class, true) => $classAttribute->withAddedTerminatedMiddleware( |
||
| 103 | $middlewareClass |
||
| 104 | ), |
||
| 105 | }; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** @var class-string<RequestStructContract>[] $requestStruct */ |
||
| 109 | $requestStruct = array_column( |
||
| 110 | $this->attributes->forClass($class, RequestStruct::class), |
||
| 111 | 'name' |
||
| 112 | ); |
||
| 113 | |||
| 114 | if ($requestStruct !== []) { |
||
| 115 | $classAttribute = $classAttribute->withRequestStruct($requestStruct[0]); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** @var class-string<ResponseStructContract>[] $responseStruct */ |
||
| 119 | $responseStruct = array_column( |
||
| 120 | $this->attributes->forClass($class, ResponseStruct::class), |
||
| 121 | 'name' |
||
| 122 | ); |
||
| 123 | |||
| 124 | if ($responseStruct !== []) { |
||
| 125 | $classAttribute = $classAttribute->withResponseStruct($responseStruct[0]); |
||
| 126 | } |
||
| 127 | |||
| 128 | // If the class' members' had attributes |
||
| 129 | if ($memberAttributes !== []) { |
||
| 130 | // Iterate through all the members' attributes |
||
| 131 | foreach ($memberAttributes as $routeAttribute) { |
||
| 132 | $routeParameters = []; |
||
| 133 | $routeMiddleware = []; |
||
| 134 | $routeRequestStruct = []; |
||
| 135 | $routeResponseStruct = []; |
||
| 136 | $requestMethods = []; |
||
| 137 | |||
| 138 | $routeDispatch = $routeAttribute->getDispatch(); |
||
| 139 | |||
| 140 | if ($routeDispatch instanceof PropertyDispatch) { |
||
| 141 | $property = $routeDispatch->getProperty(); |
||
| 142 | $routeParameters = $this->attributes->forProperty($class, $property, Parameter::class); |
||
| 143 | $routeMiddleware = $this->attributes->forProperty($class, $property, Middleware::class); |
||
| 144 | $routeRequestStruct = $this->attributes->forProperty($class, $property, RequestStruct::class); |
||
| 145 | $routeResponseStruct = $this->attributes->forProperty($class, $property, ResponseStruct::class); |
||
| 146 | $requestMethods = $this->attributes->forProperty($class, $property, RequestMethod::class); |
||
| 147 | } elseif ($routeDispatch instanceof MethodDispatch) { |
||
| 148 | $method = $routeDispatch->getMethod(); |
||
| 149 | $routeParameters = $this->attributes->forMethod($class, $method, Parameter::class); |
||
| 150 | $routeMiddleware = $this->attributes->forMethod($class, $method, Middleware::class); |
||
| 151 | $routeRequestStruct = $this->attributes->forMethod($class, $method, RequestStruct::class); |
||
| 152 | $routeResponseStruct = $this->attributes->forMethod($class, $method, ResponseStruct::class); |
||
| 153 | $requestMethods = $this->attributes->forMethod($class, $method, RequestMethod::class); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** @var class-string[] $middlewareClasses */ |
||
| 157 | $middlewareClasses = array_column( |
||
| 158 | $routeMiddleware, |
||
| 159 | 'name' |
||
| 160 | ); |
||
| 161 | |||
| 162 | foreach ($middlewareClasses as $middlewareClass) { |
||
| 163 | $routeAttribute = match (true) { |
||
| 164 | is_a($middlewareClass, RouteMatchedMiddleware::class, true) => $routeAttribute->withAddedRouteMatchedMiddleware( |
||
| 165 | $middlewareClass |
||
| 166 | ), |
||
| 167 | is_a($middlewareClass, RouteDispatchedMiddleware::class, true) => $routeAttribute->withAddedRouteDispatchedMiddleware( |
||
| 168 | $middlewareClass |
||
| 169 | ), |
||
| 170 | is_a($middlewareClass, ThrowableCaughtMiddleware::class, true) => $routeAttribute->withAddedThrowableCaughtMiddleware( |
||
| 171 | $middlewareClass |
||
| 172 | ), |
||
| 173 | is_a($middlewareClass, SendingResponseMiddleware::class, true) => $routeAttribute->withAddedSendingResponseMiddleware( |
||
| 174 | $middlewareClass |
||
| 175 | ), |
||
| 176 | is_a($middlewareClass, TerminatedMiddleware::class, true) => $routeAttribute->withAddedTerminatedMiddleware( |
||
| 177 | $middlewareClass |
||
| 178 | ), |
||
| 179 | }; |
||
| 180 | } |
||
| 181 | |||
| 182 | foreach ($requestMethods as $requestMethod) { |
||
| 183 | /** @psalm-suppress MixedArgument Unsure why Psalm doesn't realize that the requestMethods property is an array of RequestMethod enums */ |
||
| 184 | $routeAttribute = $routeAttribute->withAddedRequestMethods(...$requestMethod->requestMethods); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** @var class-string<RequestStructContract>[] $requestStruct */ |
||
| 188 | $requestStruct = array_column($routeRequestStruct, 'name'); |
||
| 189 | |||
| 190 | if ($requestStruct !== []) { |
||
| 191 | $routeAttribute = $routeAttribute->withRequestStruct($requestStruct[0]); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** @var class-string<ResponseStructContract>[] $responseStruct */ |
||
| 195 | $responseStruct = array_column($routeResponseStruct, 'name'); |
||
| 196 | |||
| 197 | if ($responseStruct !== []) { |
||
| 198 | $routeAttribute = $routeAttribute->withResponseStruct($responseStruct[0]); |
||
| 199 | } |
||
| 200 | |||
| 201 | $routeAttribute = $routeAttribute->withParameters( |
||
| 202 | ...$routeAttribute->getParameters(), |
||
| 203 | ...$routeParameters |
||
| 204 | ); |
||
| 205 | |||
| 206 | // And set a new route with the controller defined annotation additions |
||
| 207 | $attributes[] = $this->getControllerBuiltRoute($classAttribute, $routeAttribute); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | // Figure out if there should be an else that automatically sets routes from the class attributes |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | foreach ($attributes as $attribute) { |
||
| 216 | $attribute = $this->setRouteProperties($attribute); |
||
| 217 | |||
| 218 | $routes[] = new \Valkyrja\Http\Routing\Data\Route( |
||
| 219 | path: $attribute->getPath(), |
||
| 220 | name: $attribute->getName(), |
||
| 221 | dispatch: $attribute->getDispatch(), |
||
| 222 | requestMethods: $attribute->getRequestMethods(), |
||
| 223 | regex: $attribute->getRegex(), |
||
| 224 | parameters: $attribute->getParameters(), |
||
| 225 | routeMatchedMiddleware: $attribute->getRouteMatchedMiddleware(), |
||
| 226 | routeDispatchedMiddleware: $attribute->getRouteDispatchedMiddleware(), |
||
| 227 | throwableCaughtMiddleware: $attribute->getThrowableCaughtMiddleware(), |
||
| 228 | sendingResponseMiddleware: $attribute->getSendingResponseMiddleware(), |
||
| 229 | terminatedMiddleware: $attribute->getTerminatedMiddleware(), |
||
| 230 | requestStruct: $attribute->getRequestStruct(), |
||
| 231 | responseStruct: $attribute->getResponseStruct() |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | return $routes; |
||
| 236 | } |
||
| 344 |