Fix PHPStan issues

This commit is contained in:
Sebastian Meyer 2023-11-21 23:05:39 +01:00
parent 5522936e20
commit 7c6e95a42f
2 changed files with 30 additions and 30 deletions

View File

@ -31,6 +31,8 @@ use Psr\Http\Server\MiddlewareInterface;
* *
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com> * @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package opencultureconsulting/psr15 * @package opencultureconsulting/psr15
*
* @extends StrictQueue<MiddlewareInterface>
*/ */
class MiddlewareQueue extends StrictQueue class MiddlewareQueue extends StrictQueue
{ {
@ -42,7 +44,7 @@ class MiddlewareQueue extends StrictQueue
* *
* @param iterable<MiddlewareInterface> $middlewares Initial set of PSR-15 middlewares * @param iterable<MiddlewareInterface> $middlewares Initial set of PSR-15 middlewares
*/ */
public function __construct(mixed ...$middlewares) public function __construct(iterable $middlewares = [])
{ {
parent::__construct($middlewares, [MiddlewareInterface::class]); parent::__construct($middlewares, [MiddlewareInterface::class]);
} }

View File

@ -68,25 +68,14 @@ class QueueRequestHandler implements RequestHandlerInterface
public function handle(?ServerRequestInterface $request = null): ResponseInterface public function handle(?ServerRequestInterface $request = null): ResponseInterface
{ {
$this->request = $request ?? $this->request; $this->request = $request ?? $this->request;
if (count($this->queue) > 0) {
$middleware = $this->queue->dequeue();
// It is RECOMMENDED that any application using middleware includes a // It is RECOMMENDED that any application using middleware includes a
// component that catches exceptions and converts them into responses. // component that catches exceptions and converts them into responses.
// This middleware SHOULD be the first component executed and wrap all // This middleware SHOULD be the first component executed and wrap all
// further processing to ensure that a response is always generated. // further processing to ensure that a response is always generated.
try { try {
if (count($this->queue) > 0) {
$middleware = $this->queue->dequeue();
if (!is_object($middleware) || !is_a($middleware, MiddlewareInterface::class)) {
throw new DomainException(
sprintf(
'Middleware must be of type %s, %s given.',
MiddlewareInterface::class,
get_debug_type($middleware)
),
500
);
}
$this->response = $middleware->process($this->request, $this); $this->response = $middleware->process($this->request, $this);
}
} catch(Exception $exception) { } catch(Exception $exception) {
$options = [ $options = [
'options' => [ 'options' => [
@ -96,9 +85,18 @@ class QueueRequestHandler implements RequestHandlerInterface
] ]
]; ];
$statusCode = filter_var($exception->getCode(), FILTER_VALIDATE_INT, $options); $statusCode = filter_var($exception->getCode(), FILTER_VALIDATE_INT, $options);
$this->response = new Response($statusCode, [], $exception->getMessage()); $this->response = new Response(
$statusCode,
[],
sprintf(
'Exception thrown in middleware %s: %s',
get_debug_type($middleware),
$exception->getMessage()
)
);
$this->respond(1); $this->respond(1);
} }
}
return $this->response; return $this->response;
} }
@ -168,7 +166,7 @@ class QueueRequestHandler implements RequestHandlerInterface
* *
* @param iterable<MiddlewareInterface> $middlewares Initial set of middlewares * @param iterable<MiddlewareInterface> $middlewares Initial set of middlewares
*/ */
public function __construct(?iterable $middlewares = []) public function __construct(iterable $middlewares = [])
{ {
$this->request = ServerRequest::fromGlobals(); $this->request = ServerRequest::fromGlobals();
$this->queue = MiddlewareQueue::getInstance($middlewares); $this->queue = MiddlewareQueue::getInstance($middlewares);