| Total Lines | 65 |
| Code Lines | 28 |
| 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 |
||
| 64 | 13 | private function wrapCallable($callback): MiddlewareInterface |
|
| 65 | { |
||
| 66 | 13 | if (is_array($callback)) { |
|
| 67 | 5 | 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 | 5 | [$this->class, $this->method] = $callback; |
|
| 76 | 5 | $this->container = $container; |
|
| 77 | 5 | $this->callback = $callback; |
|
| 78 | 5 | } |
|
| 79 | |||
| 80 | public function process( |
||
| 81 | ServerRequestInterface $request, |
||
| 82 | RequestHandlerInterface $handler |
||
| 83 | ): ResponseInterface { |
||
| 84 | /** @var mixed $controller */ |
||
| 85 | 5 | $controller = $this->container->get($this->class); |
|
| 86 | |||
| 87 | /** @var mixed $response */ |
||
| 88 | 5 | $response = (new Injector($this->container)) |
|
| 89 | 5 | ->invoke([$controller, $this->method], [$request, $handler]); |
|
| 90 | 5 | if ($response instanceof ResponseInterface) { |
|
| 91 | 4 | return $response; |
|
| 92 | } |
||
| 93 | |||
| 94 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 95 | } |
||
| 96 | |||
| 97 | public function __debugInfo() |
||
| 98 | { |
||
| 99 | return [ |
||
| 100 | 1 | 'callback' => $this->callback, |
|
| 101 | ]; |
||
| 102 | } |
||
| 103 | }; |
||
| 104 | } |
||
| 105 | |||
| 106 | 8 | return new class ($callback, $this->container) implements MiddlewareInterface { |
|
| 107 | private ContainerInterface $container; |
||
| 108 | private $callback; |
||
| 109 | |||
| 110 | public function __construct(callable $callback, ContainerInterface $container) |
||
| 111 | { |
||
| 112 | 8 | $this->callback = $callback; |
|
| 113 | 8 | $this->container = $container; |
|
| 114 | 8 | } |
|
| 115 | |||
| 116 | public function process( |
||
| 117 | ServerRequestInterface $request, |
||
| 118 | RequestHandlerInterface $handler |
||
| 119 | ): ResponseInterface { |
||
| 120 | /** @var mixed $response */ |
||
| 121 | 8 | $response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]); |
|
| 122 | 8 | if ($response instanceof ResponseInterface) { |
|
| 123 | 5 | return $response; |
|
| 124 | } |
||
| 125 | 3 | if ($response instanceof MiddlewareInterface) { |
|
| 126 | 2 | return $response->process($request, $handler); |
|
| 127 | } |
||
| 128 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 129 | } |
||
| 176 |