| Total Lines | 65 |
| Code Lines | 28 |
| 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 |
||
| 83 | 13 | private function wrapCallableDefinition($callback): MiddlewareInterface |
|
| 84 | { |
||
| 85 | 13 | if (is_array($callback)) { |
|
| 86 | 5 | return new class ($this->container, $callback) implements MiddlewareInterface { |
|
| 87 | private string $class; |
||
| 88 | private string $method; |
||
| 89 | private ContainerInterface $container; |
||
| 90 | private array $callback; |
||
| 91 | |||
| 92 | public function __construct(ContainerInterface $container, array $callback) |
||
| 93 | { |
||
| 94 | 5 | [$this->class, $this->method] = $callback; |
|
| 95 | 5 | $this->container = $container; |
|
| 96 | 5 | $this->callback = $callback; |
|
| 97 | } |
||
| 98 | |||
| 99 | public function process( |
||
| 100 | ServerRequestInterface $request, |
||
| 101 | RequestHandlerInterface $handler |
||
| 102 | ): ResponseInterface { |
||
| 103 | /** @var mixed $controller */ |
||
| 104 | 5 | $controller = $this->container->get($this->class); |
|
| 105 | |||
| 106 | /** @var mixed $response */ |
||
| 107 | 5 | $response = (new Injector($this->container)) |
|
| 108 | 5 | ->invoke([$controller, $this->method], [$request, $handler]); |
|
| 109 | 5 | if ($response instanceof ResponseInterface) { |
|
| 110 | 4 | return $response; |
|
| 111 | } |
||
| 112 | |||
| 113 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 114 | } |
||
| 115 | |||
| 116 | public function __debugInfo() |
||
| 117 | { |
||
| 118 | return [ |
||
| 119 | 1 | 'callback' => $this->callback, |
|
| 120 | ]; |
||
| 121 | } |
||
| 122 | }; |
||
| 123 | } |
||
| 124 | |||
| 125 | 8 | return new class ($callback, $this->container) implements MiddlewareInterface { |
|
| 126 | private ContainerInterface $container; |
||
| 127 | private $callback; |
||
| 128 | |||
| 129 | public function __construct(callable $callback, ContainerInterface $container) |
||
| 130 | { |
||
| 131 | 8 | $this->callback = $callback; |
|
| 132 | 8 | $this->container = $container; |
|
| 133 | } |
||
| 134 | |||
| 135 | public function process( |
||
| 136 | ServerRequestInterface $request, |
||
| 137 | RequestHandlerInterface $handler |
||
| 138 | ): ResponseInterface { |
||
| 139 | /** @var mixed $response */ |
||
| 140 | 8 | $response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]); |
|
| 141 | 8 | if ($response instanceof ResponseInterface) { |
|
| 142 | 5 | return $response; |
|
| 143 | } |
||
| 144 | 3 | if ($response instanceof MiddlewareInterface) { |
|
| 145 | 2 | return $response->process($request, $handler); |
|
| 146 | } |
||
| 147 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 148 | } |
||
| 205 |