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,36 +68,34 @@ 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;
// It is RECOMMENDED that any application using middleware includes a if (count($this->queue) > 0) {
// component that catches exceptions and converts them into responses. $middleware = $this->queue->dequeue();
// This middleware SHOULD be the first component executed and wrap all // It is RECOMMENDED that any application using middleware includes a
// further processing to ensure that a response is always generated. // component that catches exceptions and converts them into responses.
try { // This middleware SHOULD be the first component executed and wrap all
if (count($this->queue) > 0) { // further processing to ensure that a response is always generated.
$middleware = $this->queue->dequeue(); try {
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) {
$options = [
'options' => [
'default' => 500,
'min_range' => 100,
'max_range' => 599
]
];
$statusCode = filter_var($exception->getCode(), FILTER_VALIDATE_INT, $options);
$this->response = new Response(
$statusCode,
[],
sprintf(
'Exception thrown in middleware %s: %s',
get_debug_type($middleware),
$exception->getMessage()
)
);
$this->respond(1);
} }
} catch(Exception $exception) {
$options = [
'options' => [
'default' => 500,
'min_range' => 100,
'max_range' => 599
]
];
$statusCode = filter_var($exception->getCode(), FILTER_VALIDATE_INT, $options);
$this->response = new Response($statusCode, [], $exception->getMessage());
$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);