| Conditions | 8 |
| Paths | 36 |
| Total Lines | 100 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | #[Override] |
||
| 63 | public function getRoutes(string ...$classes): array |
||
| 64 | { |
||
| 65 | $routes = []; |
||
| 66 | $attributes = []; |
||
| 67 | |||
| 68 | foreach ($classes as $class) { |
||
| 69 | /** @var Route[] $memberAttributes */ |
||
| 70 | $memberAttributes = $this->attributes->forClassMembers($class, Route::class); |
||
| 71 | |||
| 72 | // Iterate through all the members' attributes |
||
| 73 | foreach ($memberAttributes as $routeAttribute) { |
||
| 74 | $routeDispatch = $routeAttribute->getDispatch(); |
||
| 75 | |||
| 76 | $method = $routeDispatch->getMethod(); |
||
| 77 | $routeParameters = $this->attributes->forMethod($class, $method, Parameter::class); |
||
| 78 | $routeMiddleware = $this->attributes->forMethod($class, $method, Middleware::class); |
||
| 79 | $routeRequestStruct = $this->attributes->forMethod($class, $method, RequestStruct::class); |
||
| 80 | $routeResponseStruct = $this->attributes->forMethod($class, $method, ResponseStruct::class); |
||
| 81 | $requestMethods = $this->attributes->forMethod($class, $method, RequestMethod::class); |
||
| 82 | |||
| 83 | /** @var class-string[] $middlewareClasses */ |
||
| 84 | $middlewareClasses = array_column( |
||
| 85 | $routeMiddleware, |
||
| 86 | 'name' |
||
| 87 | ); |
||
| 88 | |||
| 89 | foreach ($middlewareClasses as $middlewareClass) { |
||
| 90 | $routeAttribute = match (true) { |
||
| 91 | is_a($middlewareClass, RouteMatchedMiddleware::class, true) => $routeAttribute->withAddedRouteMatchedMiddleware( |
||
| 92 | $middlewareClass |
||
| 93 | ), |
||
| 94 | is_a($middlewareClass, RouteDispatchedMiddleware::class, true) => $routeAttribute->withAddedRouteDispatchedMiddleware( |
||
| 95 | $middlewareClass |
||
| 96 | ), |
||
| 97 | is_a($middlewareClass, ThrowableCaughtMiddleware::class, true) => $routeAttribute->withAddedThrowableCaughtMiddleware( |
||
| 98 | $middlewareClass |
||
| 99 | ), |
||
| 100 | is_a($middlewareClass, SendingResponseMiddleware::class, true) => $routeAttribute->withAddedSendingResponseMiddleware( |
||
| 101 | $middlewareClass |
||
| 102 | ), |
||
| 103 | is_a($middlewareClass, TerminatedMiddleware::class, true) => $routeAttribute->withAddedTerminatedMiddleware( |
||
| 104 | $middlewareClass |
||
| 105 | ), |
||
| 106 | default => throw new InvalidArgumentException( |
||
| 107 | "Unsupported middleware class `$middlewareClass`" |
||
| 108 | ), |
||
| 109 | }; |
||
| 110 | } |
||
| 111 | |||
| 112 | foreach ($requestMethods as $requestMethod) { |
||
| 113 | /** @psalm-suppress MixedArgument Unsure why Psalm doesn't realize that the requestMethods property is an array of RequestMethod enums */ |
||
| 114 | $routeAttribute = $routeAttribute->withAddedRequestMethods(...$requestMethod->requestMethods); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** @var class-string<RequestStructContract>[] $requestStruct */ |
||
| 118 | $requestStruct = array_column($routeRequestStruct, 'name'); |
||
| 119 | |||
| 120 | if ($requestStruct !== []) { |
||
| 121 | $routeAttribute = $routeAttribute->withRequestStruct($requestStruct[0]); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** @var class-string<ResponseStructContract>[] $responseStruct */ |
||
| 125 | $responseStruct = array_column($routeResponseStruct, 'name'); |
||
| 126 | |||
| 127 | if ($responseStruct !== []) { |
||
| 128 | $routeAttribute = $routeAttribute->withResponseStruct($responseStruct[0]); |
||
| 129 | } |
||
| 130 | |||
| 131 | $routeAttribute = $routeAttribute->withParameters( |
||
| 132 | ...$this->attributes->forMethodParameters($class, $method, Parameter::class), |
||
| 133 | ...$routeParameters |
||
| 134 | ); |
||
| 135 | |||
| 136 | // And set a new route with the controller defined annotation additions |
||
| 137 | $attributes[] = $routeAttribute; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach ($attributes as $attribute) { |
||
| 142 | $attribute = $this->setRouteProperties($attribute); |
||
| 143 | |||
| 144 | $routes[] = new \Valkyrja\Http\Routing\Data\Route( |
||
| 145 | path: $attribute->getPath(), |
||
| 146 | name: $attribute->getName(), |
||
| 147 | dispatch: $attribute->getDispatch(), |
||
| 148 | requestMethods: $attribute->getRequestMethods(), |
||
| 149 | regex: $attribute->getRegex(), |
||
| 150 | parameters: $attribute->getParameters(), |
||
| 151 | routeMatchedMiddleware: $attribute->getRouteMatchedMiddleware(), |
||
| 152 | routeDispatchedMiddleware: $attribute->getRouteDispatchedMiddleware(), |
||
| 153 | throwableCaughtMiddleware: $attribute->getThrowableCaughtMiddleware(), |
||
| 154 | sendingResponseMiddleware: $attribute->getSendingResponseMiddleware(), |
||
| 155 | terminatedMiddleware: $attribute->getTerminatedMiddleware(), |
||
| 156 | requestStruct: $attribute->getRequestStruct(), |
||
| 157 | responseStruct: $attribute->getResponseStruct() |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $routes; |
||
| 162 | } |
||
| 186 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths