| Total Lines | 83 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 62 | 7 | private function createHandler(Iterator $iterator, ?EventDispatcherInterface $dispatcher): RequestHandlerInterface |
|
| 63 | { |
||
| 64 | 7 | return new class(null, $iterator, $dispatcher) implements RequestHandlerInterface { |
|
| 65 | private ?self $root; |
||
| 66 | private ?self $nextHandler = null; |
||
| 67 | |||
| 68 | /** @var Iterator<mixed, MiddlewareInterface>|null */ |
||
| 69 | private ?Iterator $iterator; |
||
| 70 | private ?MiddlewareInterface $middleware = null; |
||
| 71 | private ?EventDispatcherInterface $dispatcher; |
||
| 72 | /** @psalm-suppress PropertyNotSetInConstructor */ |
||
| 73 | private RequestHandlerInterface $fallbackHandler; |
||
| 74 | |||
| 75 | public function __clone() |
||
| 76 | { |
||
| 77 | 7 | if ($this->root !== null) { |
|
| 78 | return; |
||
| 79 | } |
||
| 80 | 7 | $current = $this; |
|
| 81 | 7 | while ($current->nextHandler !== null) { |
|
| 82 | $current->nextHandler = clone $current->nextHandler; |
||
| 83 | $current->nextHandler->root = $this; |
||
| 84 | $current = $current->nextHandler; |
||
| 85 | } |
||
| 86 | 7 | } |
|
| 87 | |||
| 88 | final public function withRequestHandler(RequestHandlerInterface $handler): self |
||
| 89 | { |
||
| 90 | 7 | $new = clone $this; |
|
| 91 | 7 | $new->fallbackHandler = $handler; |
|
| 92 | 7 | return $new; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @psalm-param Iterator<mixed, MiddlewareInterface> $iterator |
||
| 97 | */ |
||
| 98 | public function __construct(?self $root, Iterator $iterator, ?EventDispatcherInterface $dispatcher) |
||
| 99 | { |
||
| 100 | 7 | $this->iterator = $iterator; |
|
| 101 | 7 | $this->dispatcher = $dispatcher; |
|
| 102 | 7 | $this->root = $root; |
|
| 103 | 7 | } |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @psalm-suppress PossiblyNullReference |
||
| 107 | * @psalm-suppress PossiblyNullArgument |
||
| 108 | */ |
||
| 109 | final public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 110 | { |
||
| 111 | // If Middleware is not cached |
||
| 112 | 7 | if ($this->middleware === null) { |
|
| 113 | 7 | $this->middleware = $this->iterator->current(); |
|
| 114 | 7 | $this->iterator->next(); |
|
| 115 | 7 | if ($this->iterator->valid()) { |
|
| 116 | 3 | $this->nextHandler = new self($this->root ?? $this, $this->iterator, $this->dispatcher); |
|
| 117 | } |
||
| 118 | 7 | $this->iterator = null; |
|
| 119 | } |
||
| 120 | 7 | return $this->processMiddleware($request); |
|
| 121 | } |
||
| 122 | |||
| 123 | private function processMiddleware(ServerRequestInterface $request): ResponseInterface |
||
| 124 | { |
||
| 125 | 7 | $nextHandler = $this->nextHandler ?? ($this->root ?? $this)->fallbackHandler; |
|
| 126 | 7 | if ($this->middleware === null) { |
|
| 127 | 1 | return $nextHandler->handle($request); |
|
| 128 | } |
||
| 129 | // If the event dispatcher not exists |
||
| 130 | 7 | if ($this->dispatcher === null) { |
|
| 131 | 5 | return $this->middleware->process($request, $nextHandler); |
|
| 132 | } |
||
| 133 | 2 | $this->dispatcher->dispatch(new BeforeMiddleware($this->middleware, $request)); |
|
| 134 | try { |
||
| 135 | 2 | return $response = $this->middleware->process($request, $nextHandler); |
|
| 136 | } finally { |
||
| 137 | 2 | $this->dispatcher->dispatch(new AfterMiddleware($this->middleware, $response ?? null)); |
|
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | public function __destruct() |
||
| 142 | { |
||
| 143 | 6 | $this->root = null; |
|
| 144 | 6 | $this->nextHandler = null; |
|
| 145 | 6 | } |
|
| 177 |