Skip to content

Commit

Permalink
Merge pull request #7232 from kenjis/fix-Exceptions-construct
Browse files Browse the repository at this point in the history
refactor: deprecate $request and $response in Exceptions::__construct()
  • Loading branch information
kenjis authored Feb 17, 2023
2 parents 0230c8b + 08a6a78 commit 98300a4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 98300a4

Please sign in to comment.