| Total Lines | 107 |
| Code Lines | 52 |
| 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 |
||
| 64 | 15 | private function wrapCallable($callback): MiddlewareInterface |
|
| 65 | { |
||
| 66 | 15 | if (is_array($callback)) { |
|
| 67 | 6 | return new class($this->container, $callback) implements MiddlewareInterface { |
|
| 68 | private string $class; |
||
| 69 | private string $method; |
||
| 70 | private ContainerInterface $container; |
||
| 71 | private array $callback; |
||
| 72 | |||
| 73 | public function __construct(ContainerInterface $container, array $callback) |
||
| 74 | { |
||
| 75 | 6 | [$this->class, $this->method] = $callback; |
|
| 76 | 6 | $this->container = $container; |
|
| 77 | 6 | $this->callback = $callback; |
|
| 78 | 6 | } |
|
| 79 | |||
| 80 | public function process( |
||
| 81 | ServerRequestInterface $request, |
||
| 82 | RequestHandlerInterface $handler |
||
| 83 | ): ResponseInterface { |
||
| 84 | /** @var mixed $controller */ |
||
| 85 | 6 | $controller = $this->container->get($this->class); |
|
| 86 | |||
| 87 | /** @var mixed $response */ |
||
| 88 | 6 | $response = (new Injector($this->container)) |
|
| 89 | 6 | ->invoke([$controller, $this->method], $this->resolveHandlerArguments($request, $handler)); |
|
| 90 | 6 | if ($response instanceof ResponseInterface) { |
|
| 91 | 5 | return $response; |
|
| 92 | } |
||
| 93 | |||
| 94 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 95 | } |
||
| 96 | |||
| 97 | private function resolveHandlerArguments( |
||
| 98 | ServerRequestInterface $request, |
||
| 99 | RequestHandlerInterface $handler |
||
| 100 | ): array { |
||
| 101 | 6 | $parameters = (new \ReflectionClass($this->class))->getMethod($this->method)->getParameters(); |
|
| 102 | 6 | $arguments = []; |
|
| 103 | |||
| 104 | 6 | foreach ($parameters as $parameter) { |
|
| 105 | 2 | if ($parameter->getType()->getName() === ServerRequestInterface::class) { |
|
|
|
|||
| 106 | 2 | $arguments[$parameter->getName()] = $request; |
|
| 107 | 2 | } else if ($parameter->getType()->getName() === RequestHandlerInterface::class) { |
|
| 108 | 2 | $arguments[$parameter->getName()] = $handler; |
|
| 109 | } else if ( |
||
| 110 | 1 | (!$parameter->hasType() || $parameter->getType()->isBuiltin()) |
|
| 111 | 1 | && array_key_exists($parameter->getName(), $request->getAttributes()) |
|
| 112 | ) { |
||
| 113 | 1 | $arguments[$parameter->getName()] = $request->getAttribute($parameter->getName()); |
|
| 114 | } |
||
| 115 | } |
||
| 116 | 6 | return $arguments; |
|
| 117 | } |
||
| 118 | }; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** @var callable $callback */ |
||
| 122 | |||
| 123 | 9 | return new class($callback, $this->container) implements MiddlewareInterface { |
|
| 124 | private ContainerInterface $container; |
||
| 125 | private $callback; |
||
| 126 | |||
| 127 | public function __construct(callable $callback, ContainerInterface $container) |
||
| 128 | { |
||
| 129 | 9 | $this->callback = $callback; |
|
| 130 | 9 | $this->container = $container; |
|
| 131 | 9 | } |
|
| 132 | |||
| 133 | public function process( |
||
| 134 | ServerRequestInterface $request, |
||
| 135 | RequestHandlerInterface $handler |
||
| 136 | ): ResponseInterface { |
||
| 137 | /** @var mixed $response */ |
||
| 138 | 9 | $response = (new Injector($this->container))->invoke( |
|
| 139 | 9 | $this->callback, |
|
| 140 | 9 | $this->resolveHandlerArguments($request, $handler) |
|
| 141 | ); |
||
| 142 | 9 | if ($response instanceof ResponseInterface) { |
|
| 143 | 6 | return $response; |
|
| 144 | } |
||
| 145 | 3 | if ($response instanceof MiddlewareInterface) { |
|
| 146 | 2 | return $response->process($request, $handler); |
|
| 147 | } |
||
| 148 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 149 | } |
||
| 150 | |||
| 151 | private function resolveHandlerArguments( |
||
| 152 | ServerRequestInterface $request, |
||
| 153 | RequestHandlerInterface $handler |
||
| 154 | ): array { |
||
| 155 | 9 | $parameters = (new \ReflectionFunction($this->callback))->getParameters(); |
|
| 156 | 9 | $arguments = []; |
|
| 157 | |||
| 158 | 9 | foreach ($parameters as $parameter) { |
|
| 159 | 3 | if ($parameter->hasType() && $parameter->getType()->getName() === ServerRequestInterface::class) { |
|
| 160 | 3 | $arguments[$parameter->getName()] = $request; |
|
| 161 | 3 | } else if ($parameter->hasType() && $parameter->getType()->getName() === RequestHandlerInterface::class) { |
|
| 162 | 3 | $arguments[$parameter->getName()] = $handler; |
|
| 163 | } else if ( |
||
| 164 | 1 | (!$parameter->hasType() || $parameter->getType()->isBuiltin()) |
|
| 165 | 1 | && array_key_exists($parameter->getName(), $request->getAttributes()) |
|
| 166 | ) { |
||
| 167 | 1 | $arguments[$parameter->getName()] = $request->getAttribute($parameter->getName()); |
|
| 168 | } |
||
| 169 | } |
||
| 170 | 9 | return $arguments; |
|
| 171 | } |
||
| 218 |