diff --git a/.gitignore b/.gitignore index 668b8eabc..508319b9f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store .idea .phpunit.result.cache +.phpunit.cache/ composer.lock phpunit.xml clover.xml diff --git a/Slim/Exception/HttpSpecializedException.php b/Slim/Exception/HttpSpecializedException.php index 945e1ad44..9768bf5b5 100644 --- a/Slim/Exception/HttpSpecializedException.php +++ b/Slim/Exception/HttpSpecializedException.php @@ -17,8 +17,8 @@ abstract class HttpSpecializedException extends HttpException { /** * @param ServerRequestInterface $request - * @param string|null $message - * @param Throwable|null $previous + * @param string|null $message + * @param Throwable|null $previous */ public function __construct(ServerRequestInterface $request, ?string $message = null, ?Throwable $previous = null) { @@ -26,6 +26,7 @@ public function __construct(ServerRequestInterface $request, ?string $message = $this->message = $message; } + // @phpstan-ignore-next-line parent::__construct($request, $this->message, $this->code, $previous); } } diff --git a/Slim/Factory/Psr17/ServerRequestCreator.php b/Slim/Factory/Psr17/ServerRequestCreator.php index 0fc6c0663..4f17c0958 100644 --- a/Slim/Factory/Psr17/ServerRequestCreator.php +++ b/Slim/Factory/Psr17/ServerRequestCreator.php @@ -39,6 +39,8 @@ public function createServerRequestFromGlobals(): ServerRequestInterface { /** @var callable $callable */ $callable = [$this->serverRequestCreator, $this->serverRequestCreatorMethod]; + + /** @var ServerRequestInterface */ return (Closure::fromCallable($callable))(); } } diff --git a/Slim/Handlers/ErrorHandler.php b/Slim/Handlers/ErrorHandler.php index 689b4630b..a6b99ceac 100644 --- a/Slim/Handlers/ErrorHandler.php +++ b/Slim/Handlers/ErrorHandler.php @@ -189,6 +189,7 @@ protected function determineContentType(ServerRequestInterface $request): ?strin } } + // @phpstan-ignore-next-line if (is_string($current)) { return $current; } @@ -259,11 +260,15 @@ public function setLogErrorRenderer($logErrorRenderer): void protected function writeToErrorLog(): void { $renderer = $this->callableResolver->resolve($this->logErrorRenderer); + + /** @var string $error */ $error = $renderer($this->exception, $this->logErrorDetails); + if ($this->logErrorRenderer === PlainTextErrorRenderer::class && !$this->displayErrorDetails) { $error .= "\nTips: To display error details in HTTP response "; $error .= 'set "displayErrorDetails" to true in the ErrorHandler constructor.'; } + $this->logError($error); } diff --git a/Slim/Handlers/Strategies/RequestHandler.php b/Slim/Handlers/Strategies/RequestHandler.php index ea88a5f12..079200163 100644 --- a/Slim/Handlers/Strategies/RequestHandler.php +++ b/Slim/Handlers/Strategies/RequestHandler.php @@ -43,6 +43,7 @@ public function __invoke( } } + /** @var ResponseInterface */ return $callable($request); } } diff --git a/Slim/Handlers/Strategies/RequestResponse.php b/Slim/Handlers/Strategies/RequestResponse.php index 45b2c05a4..53326a2d7 100644 --- a/Slim/Handlers/Strategies/RequestResponse.php +++ b/Slim/Handlers/Strategies/RequestResponse.php @@ -35,6 +35,7 @@ public function __invoke( $request = $request->withAttribute($k, $v); } + /** @var ResponseInterface */ return $callable($request, $response, $routeArguments); } } diff --git a/Slim/Handlers/Strategies/RequestResponseArgs.php b/Slim/Handlers/Strategies/RequestResponseArgs.php index 6eab63a25..d61decbdb 100644 --- a/Slim/Handlers/Strategies/RequestResponseArgs.php +++ b/Slim/Handlers/Strategies/RequestResponseArgs.php @@ -34,6 +34,7 @@ public function __invoke( ResponseInterface $response, array $routeArguments ): ResponseInterface { + /** @var ResponseInterface */ return $callable($request, $response, ...array_values($routeArguments)); } } diff --git a/Slim/Handlers/Strategies/RequestResponseNamedArgs.php b/Slim/Handlers/Strategies/RequestResponseNamedArgs.php index 4153e113d..69f473ba1 100644 --- a/Slim/Handlers/Strategies/RequestResponseNamedArgs.php +++ b/Slim/Handlers/Strategies/RequestResponseNamedArgs.php @@ -33,6 +33,7 @@ public function __invoke( ResponseInterface $response, array $routeArguments ): ResponseInterface { + /** @var ResponseInterface */ return $callable($request, $response, ...$routeArguments); } } diff --git a/Slim/Middleware/BodyParsingMiddleware.php b/Slim/Middleware/BodyParsingMiddleware.php index 94bba3713..f04c0ef22 100644 --- a/Slim/Middleware/BodyParsingMiddleware.php +++ b/Slim/Middleware/BodyParsingMiddleware.php @@ -19,7 +19,6 @@ use function count; use function explode; use function is_array; -use function is_null; use function is_object; use function is_string; use function json_decode; @@ -66,8 +65,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface } /** - * @param string $mediaType A HTTP media type (excluding content-type params). - * @param callable $callable A callable that returns parsed contents for media type. + * @param string $mediaType A HTTP media type (excluding content-type params). + * @param callable $callable A callable that returns parsed contents for media type. */ public function registerBodyParser(string $mediaType, callable $callable): self { @@ -76,7 +75,7 @@ public function registerBodyParser(string $mediaType, callable $callable): self } /** - * @param string $mediaType A HTTP media type (excluding content-type params). + * @param string $mediaType A HTTP media type (excluding content-type params). */ public function hasBodyParser(string $mediaType): bool { @@ -84,7 +83,7 @@ public function hasBodyParser(string $mediaType): bool } /** - * @param string $mediaType A HTTP media type (excluding content-type params). + * @param string $mediaType A HTTP media type (excluding content-type params). * @throws RuntimeException */ public function getBodyParser(string $mediaType): callable @@ -98,6 +97,7 @@ public function getBodyParser(string $mediaType): callable protected function registerDefaultBodyParsers(): void { $this->registerBodyParser('application/json', static function ($input) { + /** @var string $input */ $result = json_decode($input, true); if (!is_array($result)) { @@ -108,11 +108,15 @@ protected function registerDefaultBodyParsers(): void }); $this->registerBodyParser('application/x-www-form-urlencoded', static function ($input) { + /** @var string $input */ parse_str($input, $data); + return $data; }); $xmlCallable = static function ($input) { + /** @var string $input */ + $backup = self::disableXmlEntityLoader(true); $backup_errors = libxml_use_internal_errors(true); $result = simplexml_load_string($input); diff --git a/Slim/Middleware/ErrorMiddleware.php b/Slim/Middleware/ErrorMiddleware.php index 39b1e585d..d6ac64149 100644 --- a/Slim/Middleware/ErrorMiddleware.php +++ b/Slim/Middleware/ErrorMiddleware.php @@ -89,6 +89,7 @@ public function handleException(ServerRequestInterface $request, Throwable $exce $exceptionType = get_class($exception); $handler = $this->getErrorHandler($exceptionType); + /** @var ResponseInterface */ return $handler($request, $exception, $this->displayErrorDetails, $this->logErrors, $this->logErrorDetails); } @@ -141,7 +142,8 @@ public function getDefaultErrorHandler() * * The callable signature MUST match the ErrorHandlerInterface * - * @see \Slim\Interfaces\ErrorHandlerInterface + * @param string|callable|ErrorHandler $handler + * @see ErrorHandlerInterface * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Throwable @@ -152,7 +154,6 @@ public function getDefaultErrorHandler() * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * - * @param string|callable|ErrorHandler $handler */ public function setDefaultErrorHandler($handler): self { @@ -169,7 +170,12 @@ public function setDefaultErrorHandler($handler): self * Pass true to $handleSubclasses to make the handler handle all subclasses of * the type as well. Pass an array of classes to make the same function handle multiple exceptions. * - * @see \Slim\Interfaces\ErrorHandlerInterface + * @param string|string[] $typeOrTypes Exception/Throwable name. + * ie: RuntimeException::class or an array of classes + * ie: [HttpNotFoundException::class, HttpMethodNotAllowedException::class] + * @param string|callable|ErrorHandlerInterface $handler + * + * @see ErrorHandlerInterface * * 1. Instance of \Psr\Http\Message\ServerRequestInterface * 2. Instance of \Throwable @@ -180,10 +186,6 @@ public function setDefaultErrorHandler($handler): self * The callable MUST return an instance of * \Psr\Http\Message\ResponseInterface. * - * @param string|string[] $typeOrTypes Exception/Throwable name. - * ie: RuntimeException::class or an array of classes - * ie: [HttpNotFoundException::class, HttpMethodNotAllowedException::class] - * @param string|callable|ErrorHandlerInterface $handler */ public function setErrorHandler($typeOrTypes, $handler, bool $handleSubclasses = false): self { diff --git a/Slim/Middleware/MethodOverrideMiddleware.php b/Slim/Middleware/MethodOverrideMiddleware.php index 553463e2d..61b1dcbb6 100644 --- a/Slim/Middleware/MethodOverrideMiddleware.php +++ b/Slim/Middleware/MethodOverrideMiddleware.php @@ -30,7 +30,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface } elseif (strtoupper($request->getMethod()) === 'POST') { $body = $request->getParsedBody(); - if (is_array($body) && !empty($body['_METHOD'])) { + if (is_array($body) && !empty($body['_METHOD']) && is_string($body['_METHOD'])) { $request = $request->withMethod($body['_METHOD']); } diff --git a/Slim/MiddlewareDispatcher.php b/Slim/MiddlewareDispatcher.php index 1d4c668ff..def93234a 100644 --- a/Slim/MiddlewareDispatcher.php +++ b/Slim/MiddlewareDispatcher.php @@ -174,6 +174,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface { if ($this->callableResolver instanceof AdvancedCallableResolverInterface) { $callable = $this->callableResolver->resolveMiddleware($this->middleware); + /** @var ResponseInterface */ return $callable($request, $this->next); } @@ -234,6 +235,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface ); } + /** @var ResponseInterface */ return $callable($request, $this->next); } }; @@ -277,6 +279,7 @@ public function __construct(callable $middleware, RequestHandlerInterface $next) public function handle(ServerRequestInterface $request): ResponseInterface { + /** @var ResponseInterface */ return ($this->middleware)($request, $this->next); } }; diff --git a/Slim/Routing/Route.php b/Slim/Routing/Route.php index 642c766d6..b5caa09cd 100644 --- a/Slim/Routing/Route.php +++ b/Slim/Routing/Route.php @@ -75,7 +75,7 @@ class Route implements RouteInterface, RequestHandlerInterface /** * Route arguments parameters * - * @var string[] + * @var array */ protected array $savedArguments = []; diff --git a/Slim/Routing/RouteCollector.php b/Slim/Routing/RouteCollector.php index 85a65fd41..07d85cfb9 100644 --- a/Slim/Routing/RouteCollector.php +++ b/Slim/Routing/RouteCollector.php @@ -253,7 +253,7 @@ protected function createGroup(string $pattern, $callable): RouteGroupInterface */ protected function createProxy(string $pattern): RouteCollectorProxyInterface { - /** @var RouteCollectorProxyInterface */ + /** @var RouteCollectorProxy */ return new RouteCollectorProxy( $this->responseFactory, $this->callableResolver, diff --git a/composer.json b/composer.json index 486833ad5..c52c88bbd 100644 --- a/composer.json +++ b/composer.json @@ -64,7 +64,7 @@ "nyholm/psr7-server": "^1.1", "phpspec/prophecy": "^1.19", "phpspec/prophecy-phpunit": "^2.1", - "phpstan/phpstan": "^1.12", + "phpstan/phpstan": "^1 || ^2", "phpunit/phpunit": "^9.6", "slim/http": "^1.3", "slim/psr7": "^1.6",