Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.idea
.phpunit.result.cache
.phpunit.cache/
composer.lock
phpunit.xml
clover.xml
Expand Down
5 changes: 3 additions & 2 deletions Slim/Exception/HttpSpecializedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ 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)
{
if ($message !== null) {
$this->message = $message;
}

// @phpstan-ignore-next-line
parent::__construct($request, $this->message, $this->code, $previous);
}
}
2 changes: 2 additions & 0 deletions Slim/Factory/Psr17/ServerRequestCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function createServerRequestFromGlobals(): ServerRequestInterface
{
/** @var callable $callable */
$callable = [$this->serverRequestCreator, $this->serverRequestCreatorMethod];

/** @var ServerRequestInterface */
return (Closure::fromCallable($callable))();
}
}
5 changes: 5 additions & 0 deletions Slim/Handlers/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected function determineContentType(ServerRequestInterface $request): ?strin
}
}

// @phpstan-ignore-next-line
if (is_string($current)) {
return $current;
}
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions Slim/Handlers/Strategies/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __invoke(
}
}

/** @var ResponseInterface */
return $callable($request);
}
}
1 change: 1 addition & 0 deletions Slim/Handlers/Strategies/RequestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __invoke(
$request = $request->withAttribute($k, $v);
}

/** @var ResponseInterface */
return $callable($request, $response, $routeArguments);
}
}
1 change: 1 addition & 0 deletions Slim/Handlers/Strategies/RequestResponseArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __invoke(
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
/** @var ResponseInterface */
return $callable($request, $response, ...array_values($routeArguments));
}
}
1 change: 1 addition & 0 deletions Slim/Handlers/Strategies/RequestResponseNamedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __invoke(
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
/** @var ResponseInterface */
return $callable($request, $response, ...$routeArguments);
}
}
14 changes: 9 additions & 5 deletions Slim/Middleware/BodyParsingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -76,15 +75,15 @@ 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
{
return isset($this->bodyParsers[$mediaType]);
}

/**
* @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
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions Slim/Middleware/ErrorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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
{
Expand All @@ -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
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion Slim/Middleware/MethodOverrideMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
3 changes: 3 additions & 0 deletions Slim/MiddlewareDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -234,6 +235,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

/** @var ResponseInterface */
return $callable($request, $this->next);
}
};
Expand Down Expand Up @@ -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);
}
};
Expand Down
2 changes: 1 addition & 1 deletion Slim/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Route implements RouteInterface, RequestHandlerInterface
/**
* Route arguments parameters
*
* @var string[]
* @var array<string, string>
*/
protected array $savedArguments = [];

Expand Down
2 changes: 1 addition & 1 deletion Slim/Routing/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ protected function createGroup(string $pattern, $callable): RouteGroupInterface
*/
protected function createProxy(string $pattern): RouteCollectorProxyInterface
{
/** @var RouteCollectorProxyInterface<TContainerInterface> */
/** @var RouteCollectorProxy<TContainerInterface> */
return new RouteCollectorProxy(
$this->responseFactory,
$this->callableResolver,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down