| Total Lines | 78 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 184 | private function createCallableWrapper(callable $callback): MiddlewareInterface |
||
| 185 | 1 | { |
|
| 186 | return new class ( |
||
| 187 | 1 | $callback, |
|
| 188 | $this->container, |
||
| 189 | 11 | $this, |
|
| 190 | $this->eventDispatcher, |
||
| 191 | $this->parametersResolver, |
||
| 192 | ) implements MiddlewareInterface { |
||
| 193 | /** @var callable */ |
||
| 194 | private $callback; |
||
| 195 | /** |
||
| 196 | 6 | * @var ReflectionParameter[] |
|
| 197 | * @psalm-var array<string,ReflectionParameter> |
||
| 198 | 6 | */ |
|
| 199 | private array $callableParameters = []; |
||
| 200 | public array $middlewares = []; |
||
| 201 | |||
| 202 | public function __construct( |
||
| 203 | callable $callback, |
||
| 204 | private ContainerInterface $container, |
||
| 205 | private MiddlewareFactory $middlewareFactory, |
||
| 206 | private ?EventDispatcherInterface $eventDispatcher, |
||
| 207 | 6 | private ?ParametersResolverInterface $parametersResolver |
|
| 208 | ) { |
||
| 209 | $this->callback = $callback; |
||
| 210 | |||
| 211 | $reflectionFunction = new ReflectionFunction(Closure::fromCallable($callback)); |
||
| 212 | |||
| 213 | foreach ($reflectionFunction->getAttributes(Middleware::class) as $attribute) { |
||
| 214 | 6 | $this->middlewares[] = $attribute->newInstance()->getDefinition(); |
|
| 215 | 6 | } |
|
| 216 | 6 | foreach ($reflectionFunction->getParameters() as $parameter) { |
|
| 217 | 1 | $this->callableParameters[$parameter->getName()] = $parameter; |
|
| 218 | 1 | } |
|
| 219 | 1 | } |
|
| 220 | 1 | ||
| 221 | public function process( |
||
| 222 | ServerRequestInterface $request, |
||
| 223 | RequestHandlerInterface $handler |
||
| 224 | 6 | ): ResponseInterface { |
|
| 225 | 6 | $parameters = [$request, $handler]; |
|
| 226 | 5 | if ($this->parametersResolver !== null) { |
|
| 227 | $parameters = array_merge( |
||
| 228 | $parameters, |
||
| 229 | 1 | $this->parametersResolver->resolve($this->callableParameters, $request) |
|
| 230 | ); |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($this->middlewares !== []) { |
||
| 234 | $middlewares = [...$this->middlewares, fn() => ($this->callback)()]; |
||
| 235 | $middlewareDispatcher = new MiddlewareDispatcher($this->middlewareFactory, $this->eventDispatcher); |
||
| 236 | $middlewareDispatcher = $middlewareDispatcher->withMiddlewares($middlewares); |
||
| 237 | $response = $middlewareDispatcher->dispatch($request, $handler); |
||
| 238 | } else { |
||
| 239 | 1 | /** @var MiddlewareInterface|mixed|ResponseInterface $response */ |
|
| 240 | $response = (new Injector($this->container))->invoke($this->callback, $parameters); |
||
| 241 | } |
||
| 242 | if ($response instanceof ResponseInterface) { |
||
| 243 | return $response; |
||
| 244 | 1 | } |
|
| 245 | 1 | if ($response instanceof MiddlewareInterface) { |
|
| 246 | 1 | return $response->process($request, $handler); |
|
| 247 | } |
||
| 248 | 6 | ||
| 249 | throw new InvalidMiddlewareDefinitionException($this->callback); |
||
| 250 | } |
||
| 251 | |||
| 252 | public function __debugInfo(): array |
||
| 253 | { |
||
| 254 | if (is_array($this->callback) |
||
| 255 | && isset($this->callback[0], $this->callback[1]) |
||
| 256 | && is_object($this->callback[0]) |
||
| 257 | && is_string($this->callback[1]) |
||
| 258 | ) { |
||
| 259 | return ['callback' => [$this->callback[0]::class, $this->callback[1]]]; |
||
| 260 | } |
||
| 261 | return ['callback' => $this->callback]; |
||
| 262 | } |
||
| 266 |