Skip to content

Commit

Permalink
support callable error rederers
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 21, 2023
1 parent c1c0dd6 commit 6e1b3b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ parameters:
ignoreErrors:
- message: '/^Call to function is_subclass_of\(\) with Whoops\\Handler\\HandlerInterface and .Jgut\\\\Slim\\\\Exception\\\\Whoops\\\\Renderer\\\\HtmlRenderer. will always evaluate to false\.$/'
path: src/Whoops/Handler/ErrorHandler.php
- messages:
- '/^PHPDoc type \(callable\(Throwable, bool\): string\)\|class-string<Slim\\Interfaces\\ErrorRendererInterface> of property Jgut\\Slim\\Exception\\Handler\\ErrorHandler::\$(log|default)ErrorRenderer is not the same as PHPDoc type \(callable\(\): mixed\)\|string of overridden property Slim\\Handlers\\ErrorHandler::\$(log|default)ErrorRenderer\.$/'
- '/^PHPDoc type array<string, \(callable\(Throwable, bool\): string\)\|class-string<Slim\\Interfaces\\ErrorRendererInterface>> of property Jgut\\Slim\\Exception\\Handler\\ErrorHandler::\$errorRenderers is not the same as PHPDoc type array<\(callable\(\): mixed\)\|string> of overridden property Slim\\Handlers\\ErrorHandler::\$errorRenderers\.$/'
path: src/Handler/ErrorHandler.php
- messages:
- '/^Property Jgut\\Slim\\Exception\\Whoops\\Handler\\ErrorHandler::\$(log|default)ErrorRenderer \(\(callable\(Throwable, bool\): string\)\|class-string<Slim\\Interfaces\\ErrorRendererInterface>\) does not accept default value of type .Jgut\\\\Slim\\\\Exception\\\\Whoops\\\\Renderer\\\\(PlainText|Html)Renderer.\.$/'
- '/^Property Jgut\\Slim\\Exception\\Whoops\\Handler\\ErrorHandler::\$errorRenderers \(array<string, \(callable\(Throwable, bool\): string\)\|class-string<Slim\\Interfaces\\ErrorRendererInterface>>\) does not accept default value of type/'
path: src/Whoops/Handler/ErrorHandler.php
- message: '/^Expression on left side of \?\? is not nullable\.$/'
path: src/Whoops/Renderer/RendererTrait.php
20 changes: 10 additions & 10 deletions src/Handler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Psr\Log\LogLevel;
use Slim\Handlers\ErrorHandler as SlimErrorHandler;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\ErrorRendererInterface;
use Slim\Interfaces\ErrorRendererInterface as ErrorRenderer;
use Throwable;

/**
Expand All @@ -36,7 +36,7 @@
class ErrorHandler extends SlimErrorHandler
{
/**
* @var ErrorRendererInterface|string|callable(Throwable, bool): string
* @var ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string
*/
protected $logErrorRenderer = PlainTextRenderer::class;

Expand All @@ -62,12 +62,12 @@ class ErrorHandler extends SlimErrorHandler
];

/**
* @var ErrorRendererInterface|string|callable(Throwable, bool): string
* @var ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string
*/
protected $defaultErrorRenderer = HtmlRenderer::class;

/**
* @var array<string|callable(Throwable, bool): string>
* @var array<string, ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string>
*/
protected array $errorRenderers = [
'text/html' => HtmlRenderer::class,
Expand All @@ -93,21 +93,21 @@ public function __construct(
}

/**
* @param array<string, ErrorRendererInterface|class-string<ErrorRendererInterface>> $errorRenderers
* @param array<string, ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string> $renderers
*/
public function setErrorRenderers(array $errorRenderers): void
public function setErrorRenderers(array $renderers): void
{
$this->errorRenderers = [];

foreach ($errorRenderers as $contentType => $errorRenderer) {
$this->setErrorRenderer($contentType, $errorRenderer);
foreach ($renderers as $contentType => $renderer) {
$this->setErrorRenderer($contentType, $renderer);
}
}

/**
* @param ErrorRendererInterface|class-string<ErrorRendererInterface> $errorRenderer
* @param ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string $errorRenderer
*/
public function setErrorRenderer(string $contentType, ErrorRendererInterface|string $errorRenderer): void
public function setErrorRenderer(string $contentType, ErrorRenderer|string|callable $errorRenderer): void
{
$this->errorRenderers[$contentType] = $errorRenderer;
}
Expand Down
13 changes: 6 additions & 7 deletions src/Whoops/Handler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
use RuntimeException;
use Slim\App;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\ErrorRendererInterface;
use Slim\Interfaces\ErrorRendererInterface as ErrorRenderer;
use Throwable;
use Whoops\Exception\Frame;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\HandlerInterface as WhoopsHandler;
use Whoops\Run as Whoops;

/**
Expand All @@ -40,17 +39,17 @@ class ErrorHandler extends BaseErrorHandler
protected const REQUEST_DATA_TABLE_LABEL = 'Slim Application (Request)';

/**
* @var ErrorRendererInterface|string|callable(Throwable, bool): string
* @var ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string
*/
protected $logErrorRenderer = PlainTextRenderer::class;

/**
* @var ErrorRendererInterface|string|callable(Throwable, bool): string
* @var ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string
*/
protected $defaultErrorRenderer = HtmlRenderer::class;

/**
* @var array<string|callable(Throwable, bool): string>
* @var array<string, ErrorRenderer|class-string<ErrorRenderer>|callable(Throwable, bool): string>
*/
protected array $errorRenderers = [
'text/html' => HtmlRenderer::class,
Expand Down Expand Up @@ -123,11 +122,11 @@ protected function determineLogRenderer(): callable
*/
protected function getRenderer(mixed $renderer): callable
{
if (!$renderer instanceof WhoopsHandler) {
if (!$renderer instanceof HandlerInterface) {
throw new InvalidArgumentException(sprintf(
'Renderer "%s" for Whoops error handler should implement "%s".',
\is_object($renderer) ? $renderer::class : \gettype($renderer),
WhoopsHandler::class,
HandlerInterface::class,
));
}

Expand Down

0 comments on commit 6e1b3b1

Please sign in to comment.