| Total Lines | 61 |
| Code Lines | 27 |
| 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 |
||
| 51 | 14 | private function wrapCallable($callback): MiddlewareInterface |
|
| 52 | { |
||
| 53 | 14 | if (is_array($callback)) { |
|
| 54 | /** |
||
| 55 | * @var string $controller |
||
| 56 | * @var string $action |
||
| 57 | */ |
||
| 58 | 6 | [$controller, $action] = $callback; |
|
| 59 | 6 | return new class($controller, $action, $this->container, $callback) implements MiddlewareInterface { |
|
| 60 | private string $class; |
||
| 61 | private string $method; |
||
| 62 | private ContainerInterface $container; |
||
| 63 | private array $callback; |
||
| 64 | |||
| 65 | public function __construct(string $class, string $method, ContainerInterface $container, array $callback) |
||
| 66 | { |
||
| 67 | 6 | $this->class = $class; |
|
| 68 | 6 | $this->method = $method; |
|
| 69 | 6 | $this->container = $container; |
|
| 70 | 6 | $this->callback = $callback; |
|
| 71 | 6 | } |
|
| 72 | |||
| 73 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 74 | { |
||
| 75 | /** @var mixed $controller */ |
||
| 76 | 6 | $controller = $this->container->get($this->class); |
|
| 77 | |||
| 78 | /** @var mixed $response */ |
||
| 79 | 6 | $response = (new Injector($this->container))->invoke([$controller, $this->method], [$request, $handler]); |
|
| 80 | 6 | if ($response instanceof ResponseInterface) { |
|
| 81 | 5 | return $response; |
|
| 82 | } |
||
| 83 | |||
| 84 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 85 | } |
||
| 86 | }; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** @var callable $callback */ |
||
| 90 | |||
| 91 | 8 | return new class($callback, $this->container) implements MiddlewareInterface { |
|
| 92 | private ContainerInterface $container; |
||
| 93 | private $callback; |
||
| 94 | |||
| 95 | public function __construct(callable $callback, ContainerInterface $container) |
||
| 96 | { |
||
| 97 | 8 | $this->callback = $callback; |
|
| 98 | 8 | $this->container = $container; |
|
| 99 | 8 | } |
|
| 100 | |||
| 101 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 102 | { |
||
| 103 | /** @var mixed $response */ |
||
| 104 | 8 | $response = (new Injector($this->container))->invoke($this->callback, [$request, $handler]); |
|
| 105 | 8 | if ($response instanceof ResponseInterface) { |
|
| 106 | 6 | return $response; |
|
| 107 | } |
||
| 108 | 2 | if ($response instanceof MiddlewareInterface) { |
|
| 109 | 1 | return $response->process($request, $handler); |
|
| 110 | } |
||
| 111 | 1 | throw new InvalidMiddlewareDefinitionException($this->callback); |
|
| 112 | } |
||
| 159 |