From 2f71c7a43619b27b041df0a28f8f257edba3b1c9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 9 Feb 2023 08:21:22 +0900 Subject: [PATCH 1/3] fix: deprecate $request and $response in Exceptions::__construct() When registering Exception Handler, Request object was generated. But it was too early, and difficult to understand. --- system/Config/Services.php | 6 +++++- system/Debug/Exceptions.php | 12 ++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/system/Config/Services.php b/system/Config/Services.php index a2d13c40e80c..2358e87bb1a5 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -250,6 +250,8 @@ public static function encrypter(?EncryptionConfig $config = null, $getShared = * - register_shutdown_function * * @return Exceptions + * + * @deprecated The parameter $request and $response are deprecated. */ public static function exceptions( ?ExceptionsConfig $config = null, @@ -262,7 +264,9 @@ public static function exceptions( } $config ??= config('Exceptions'); - $request ??= AppServices::request(); + /** @var ExceptionsConfig $config */ + + // @TODO remove instantiation of Response in the future. $response ??= AppServices::response(); return new Exceptions($config, $request, $response); diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index 6dc098d4af10..d4e80f28f2e9 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -21,6 +21,7 @@ use CodeIgniter\HTTP\ResponseInterface; use Config\Exceptions as ExceptionsConfig; use Config\Paths; +use Config\Services; use ErrorException; use Psr\Log\LogLevel; use Throwable; @@ -71,15 +72,15 @@ class Exceptions private ?Throwable $exceptionCaughtByExceptionHandler = null; /** - * @param CLIRequest|IncomingRequest $request + * @param CLIRequest|IncomingRequest|null $request + * + * @deprecated The parameter $request and $response are deprecated. No longer used. */ - public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response) + public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response) /** @phpstan-ignore-line */ { $this->ob_level = ob_get_level(); $this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR; $this->config = $config; - $this->request = $request; - $this->response = $response; // workaround for upgraded users // This causes "Deprecated: Creation of dynamic property" in PHP 8.2. @@ -119,6 +120,9 @@ public function exceptionHandler(Throwable $exception) [$statusCode, $exitCode] = $this->determineCodes($exception); + $this->request = Services::request(); + $this->response = Services::response(); + if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) { log_message('critical', "{message}\nin {exFile} on line {exLine}.\n{trace}", [ 'message' => $exception->getMessage(), From 044460855855ba9df308a520c5e6cb16f759b8d1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 9 Feb 2023 08:46:23 +0900 Subject: [PATCH 2/3] chore: add rule to skip --- rector.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rector.php b/rector.php index 797387ba4aba..0473a5d8cfdb 100644 --- a/rector.php +++ b/rector.php @@ -28,6 +28,7 @@ use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector; use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector; use Rector\Config\RectorConfig; +use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector; use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector; use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector; use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector; @@ -89,6 +90,11 @@ __DIR__ . '/tests/system/Test/ReflectionHelperTest.php', ], + RemoveUnusedConstructorParamRector::class => [ + // there are deprecated parameters + __DIR__ . '/system/Debug/Exceptions.php', + ], + // call on purpose for nothing happen check RemoveEmptyMethodCallRector::class => [ __DIR__ . '/tests', From 08a6a7852dbe4aacafb206524b68c0c940889059 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 9 Feb 2023 09:23:23 +0900 Subject: [PATCH 3/3] test: fix incorrect setup baseURL should not be empty. --- tests/system/Helpers/FormHelperTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 9a525adf7ad2..b45329c8663c 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -39,7 +39,6 @@ private function setRequest(): void Services::injectMock('uri', $uri); $config = new App(); - $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config);