From 3cbe21e6aac909038d7023557ca8098ea961693d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Fri, 30 Aug 2024 12:02:43 +0200 Subject: [PATCH] IBX-8784: Added `AbstractExceptionVisitor` template --- phpstan-baseline.neon | 15 --------------- .../Exceptions/AbstractExceptionVisitor.php | 13 ++++++++----- .../Output/ValueObjectVisitor/Exception.php | 2 +- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index cf9f00da..0fcc5a72 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2890,11 +2890,6 @@ parameters: count: 1 path: src/lib/Server/Output/ValueObjectVisitor/ContentFieldValidationException.php - - - message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\ContentFieldValidationException\\:\\:visit\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/Server/Output/ValueObjectVisitor/ContentFieldValidationException.php - - message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\ContentList\\:\\:visit\\(\\) has no return type specified\\.$#" count: 1 @@ -3175,16 +3170,6 @@ parameters: count: 1 path: src/lib/Server/Output/ValueObjectVisitor/DeletedUserSession.php - - - message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Exception\\:\\:visit\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/Server/Output/ValueObjectVisitor/Exception.php - - - - message: "#^Property Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Exception\\:\\:\\$httpStatusCodes type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/Server/Output/ValueObjectVisitor/Exception.php - - message: "#^Property Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Exception\\:\\:\\$translator \\(Symfony\\\\Contracts\\\\Translation\\\\TranslatorInterface\\) does not accept Symfony\\\\Contracts\\\\Translation\\\\TranslatorInterface\\|null\\.$#" count: 1 diff --git a/src/contracts/Output/Exceptions/AbstractExceptionVisitor.php b/src/contracts/Output/Exceptions/AbstractExceptionVisitor.php index 4de28c93..47ebac40 100644 --- a/src/contracts/Output/Exceptions/AbstractExceptionVisitor.php +++ b/src/contracts/Output/Exceptions/AbstractExceptionVisitor.php @@ -19,7 +19,7 @@ abstract class AbstractExceptionVisitor extends ValueObjectVisitor /** * Mapping of HTTP status codes to their respective error messages. * - * @var array + * @var array */ protected static $httpStatusCodes = [ 400 => 'Bad Request', @@ -71,6 +71,8 @@ protected function getStatus() /** * @param \Exception $data + * + * @return void */ public function visit(Visitor $visitor, Generator $generator, $data) { @@ -119,16 +121,17 @@ protected function getErrorMessage(\Exception $data, int $statusCode): string protected function getErrorDescription(\Exception $data, int $statusCode): string { + $translator = $this->getTranslator(); if ($statusCode < 500 || $this->canDisplayExceptionMessage()) { - $errorDescription = $data instanceof Translatable && $this->translator + $errorDescription = $data instanceof Translatable && $translator ? /** @Ignore */ - $this->translator->trans($data->getMessageTemplate(), $data->getParameters(), 'ibexa_repository_exceptions') + $translator->trans($data->getMessageTemplate(), $data->getParameters(), 'ibexa_repository_exceptions') : $data->getMessage(); } else { // Do not leak any file paths and sensitive data on production environments - $errorDescription = $this->translator + $errorDescription = $translator ? /** @Desc("An error has occurred. Please try again later or contact your Administrator.") */ - $this->translator->trans('non_verbose_error', [], 'ibexa_repository_exceptions') + $translator->trans('non_verbose_error', [], 'ibexa_repository_exceptions') : 'An error has occurred. Please try again later or contact your Administrator.'; } diff --git a/src/lib/Server/Output/ValueObjectVisitor/Exception.php b/src/lib/Server/Output/ValueObjectVisitor/Exception.php index a91bea70..520ff2ce 100644 --- a/src/lib/Server/Output/ValueObjectVisitor/Exception.php +++ b/src/lib/Server/Output/ValueObjectVisitor/Exception.php @@ -21,7 +21,7 @@ class Exception extends AbstractExceptionVisitor */ protected $debug = false; - /** @var \Symfony\Contracts\Translation\TranslatorInterface|null */ + /** @var \Symfony\Contracts\Translation\TranslatorInterface */ protected $translator; /**