| Total Lines | 82 |
| Code Lines | 39 |
| 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 |
||
| 56 | 7 | private function createHandler(Iterator $iterator, ?EventDispatcherInterface $dispatcher): RequestHandlerInterface |
|
| 57 | { |
||
| 58 | 7 | return new class(null, $iterator, $dispatcher) implements RequestHandlerInterface { |
|
| 59 | private ?self $root; |
||
| 60 | private ?self $nextHandler = null; |
||
| 61 | |||
| 62 | /** @var Iterator<mixed, MiddlewareInterface>|null */ |
||
| 63 | private ?Iterator $iterator; |
||
| 64 | private ?MiddlewareInterface $middleware = null; |
||
| 65 | private ?EventDispatcherInterface $dispatcher; |
||
| 66 | private RequestHandlerInterface $fallbackHandler; |
||
| 67 | |||
| 68 | public function __clone() |
||
| 69 | { |
||
| 70 | 7 | if ($this->root !== null) { |
|
| 71 | return; |
||
| 72 | } |
||
| 73 | 7 | $current = $this; |
|
| 74 | 7 | while ($current->nextHandler !== null) { |
|
| 75 | $current->nextHandler = clone $current->nextHandler; |
||
| 76 | $current->nextHandler->root = $this; |
||
| 77 | $current = $current->nextHandler; |
||
| 78 | } |
||
| 79 | 7 | } |
|
| 80 | |||
| 81 | final public function withRequestHandler(RequestHandlerInterface $handler): self |
||
| 82 | { |
||
| 83 | 7 | $new = clone $this; |
|
| 84 | 7 | $new->fallbackHandler = $handler; |
|
| 85 | 7 | return $new; |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @psalm-param Iterator<mixed, MiddlewareInterface> $iterator |
||
| 90 | */ |
||
| 91 | public function __construct(?self $root, Iterator $iterator, ?EventDispatcherInterface $dispatcher) |
||
| 92 | { |
||
| 93 | 7 | $this->iterator = $iterator; |
|
| 94 | 7 | $this->dispatcher = $dispatcher; |
|
| 95 | 7 | $this->root = $root; |
|
| 96 | 7 | } |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @psalm-suppress PossiblyNullReference |
||
| 100 | * @psalm-suppress PossiblyNullArgument |
||
| 101 | */ |
||
| 102 | final public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 103 | { |
||
| 104 | // If Middleware is not cached |
||
| 105 | 7 | if ($this->middleware === null) { |
|
| 106 | 7 | $this->middleware = $this->iterator->current(); |
|
| 107 | 7 | $this->iterator->next(); |
|
| 108 | 7 | if ($this->iterator->valid()) { |
|
| 109 | 3 | $this->nextHandler = new self($this->root ?? $this, $this->iterator, $this->dispatcher); |
|
| 110 | } |
||
| 111 | 7 | $this->iterator = null; |
|
| 112 | } |
||
| 113 | 7 | return $this->processMiddleware($request); |
|
| 114 | } |
||
| 115 | |||
| 116 | private function processMiddleware(ServerRequestInterface $request): ResponseInterface |
||
| 117 | { |
||
| 118 | 7 | $nextHandler = $this->nextHandler ?? ($this->root ?? $this)->fallbackHandler; |
|
| 119 | 7 | if ($this->middleware === null) { |
|
| 120 | 1 | return $nextHandler->handle($request); |
|
| 121 | } |
||
| 122 | // If the event dispatcher not exists |
||
| 123 | 7 | if ($this->dispatcher === null) { |
|
| 124 | 5 | return $this->middleware->process($request, $nextHandler); |
|
| 125 | } |
||
| 126 | 2 | $this->dispatcher->dispatch(new BeforeMiddleware($this->middleware, $request)); |
|
| 127 | try { |
||
| 128 | 2 | return $response = $this->middleware->process($request, $nextHandler); |
|
| 129 | } finally { |
||
| 130 | 2 | $this->dispatcher->dispatch(new AfterMiddleware($this->middleware, $response ?? null)); |
|
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public function __destruct() |
||
| 135 | { |
||
| 136 | 6 | $this->root = null; |
|
| 137 | 6 | $this->nextHandler = null; |
|
| 138 | 6 | } |
|
| 170 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.