| Total Lines | 69 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 14 | ||
| Bugs | 7 | 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 |
||
| 65 | 15 | private function wrapCallable($callback): MiddlewareInterface |
|
| 66 | { |
||
| 67 | 15 | if (is_array($callback)) { |
|
| 68 | 6 | return new class ($this->container, $callback) implements MiddlewareInterface { |
|
| 69 | /** |
||
| 70 | * @psalm-var class-string |
||
| 71 | */ |
||
| 72 | private string $class; |
||
| 73 | private string $method; |
||
| 74 | private ContainerInterface $container; |
||
| 75 | private array $callback; |
||
| 76 | |||
| 77 | public function __construct(ContainerInterface $container, array $callback) |
||
| 78 | { |
||
| 79 | 6 | [$this->class, $this->method] = $callback; |
|
| 80 | 6 | $this->container = $container; |
|
| 81 | 6 | $this->callback = $callback; |
|
| 82 | 6 | } |
|
| 83 | |||
| 84 | public function process( |
||
| 85 | ServerRequestInterface $request, |
||
| 86 | RequestHandlerInterface $handler |
||
| 87 | ): ResponseInterface { |
||
| 88 | /** @var mixed $controller */ |
||
| 89 | 6 | $controller = $this->container->get($this->class); |
|
| 90 | |||
| 91 | /** @var mixed $response */ |
||
| 92 | 6 | $response = (new Injector($this->container)) |
|
| 93 | 6 | ->invoke( |
|
| 94 | 6 | [$controller, $this->method], |
|
| 95 | 6 | MiddlewareFactory::resolveHandlerArguments($this->callback, $request, $handler) |
|
| 96 | ); |
||
| 97 | 6 | if ($response instanceof ResponseInterface) { |
|
| 98 | 5 | return $response; |
|
| 99 | } |
||
| 100 | |||
| 101 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 102 | } |
||
| 103 | }; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** @var callable|Closure $callback */ |
||
| 107 | |||
| 108 | 9 | return new class ($callback, $this->container) implements MiddlewareInterface { |
|
| 109 | private ContainerInterface $container; |
||
| 110 | private $callback; |
||
| 111 | |||
| 112 | public function __construct(callable $callback, ContainerInterface $container) |
||
| 113 | { |
||
| 114 | 9 | $this->callback = $callback; |
|
| 115 | 9 | $this->container = $container; |
|
| 116 | 9 | } |
|
| 117 | |||
| 118 | public function process( |
||
| 119 | ServerRequestInterface $request, |
||
| 120 | RequestHandlerInterface $handler |
||
| 121 | ): ResponseInterface { |
||
| 122 | /** @var mixed $response */ |
||
| 123 | 9 | $response = (new Injector($this->container))->invoke( |
|
| 124 | 9 | $this->callback, |
|
| 125 | 9 | MiddlewareFactory::resolveHandlerArguments($this->callback, $request, $handler) |
|
| 126 | ); |
||
| 127 | 9 | if ($response instanceof ResponseInterface) { |
|
| 128 | 6 | return $response; |
|
| 129 | } |
||
| 130 | 3 | if ($response instanceof MiddlewareInterface) { |
|
| 131 | 2 | return $response->process($request, $handler); |
|
| 132 | } |
||
| 133 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 134 | } |
||
| 224 |