Skip to content

refactor: deprecate $request and $response in Exceptions::__construct() #7232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2023
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
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