From 345e1c3b9c3b17008d8d90ef67e97fe7c88132f6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 21 Dec 2024 18:40:05 +0700 Subject: [PATCH] refactor: enable code quality level 34 for rector (#9311) * refactor: enable code quality level 34 for rector * refactor: run cs fix * refactor: fix phpstan and regenerate baseline * refactor: cs fix * refactor: Cors: before never returns string * refactor: return EXIT_ERROR on check verify command * refactor: returns null on DebugToolbar and HoneyPot --- rector.php | 4 +--- system/CLI/Commands.php | 4 ++-- system/CodeIgniter.php | 8 ++++++-- system/Commands/Database/MigrateRollback.php | 4 +++- system/Database/BaseBuilder.php | 6 ++++-- system/Filters/CSRF.php | 9 +++++---- system/Filters/Cors.php | 10 +++++----- system/Filters/DebugToolbar.php | 3 +++ system/Filters/FilterInterface.php | 4 ++-- system/Filters/ForceHTTPS.php | 9 +++++---- system/Filters/Honeypot.php | 6 +++++- system/Filters/InvalidChars.php | 9 ++++----- system/Filters/PageCache.php | 10 +++++++--- system/Filters/PerformanceMetrics.php | 7 +++++-- system/Filters/SecureHeaders.php | 7 +++---- system/Images/Handlers/BaseHandler.php | 2 ++ system/Language/Language.php | 4 +++- system/Security/CheckPhpIni.php | 4 ++-- system/Session/Session.php | 8 ++++---- system/Test/CIUnitTestCase.php | 4 +++- tests/_support/Filters/Customfilter.php | 3 ++- tests/_support/Filters/RedirectFilter.php | 3 ++- tests/system/Filters/fixtures/GoogleCurious.php | 3 ++- tests/system/Filters/fixtures/GoogleEmpty.php | 3 ++- tests/system/Filters/fixtures/GoogleYou.php | 3 ++- tests/system/Filters/fixtures/Multiple1.php | 3 ++- tests/system/Filters/fixtures/Multiple2.php | 3 ++- utils/phpstan-baseline/missingType.iterableValue.neon | 7 +------ utils/phpstan-baseline/return.missing.neon | 7 +------ 29 files changed, 90 insertions(+), 67 deletions(-) diff --git a/rector.php b/rector.php index e7c98746eb86..e2df4f9b63e5 100644 --- a/rector.php +++ b/rector.php @@ -19,7 +19,6 @@ use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector; use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector; use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector; -use Rector\CodeQuality\Rector\If_\ShortenElseIfRector; use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector; use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector; use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector; @@ -186,7 +185,6 @@ ChangeIfElseValueAssignToEarlyReturnRector::class, InlineIfToExplicitIfRector::class, PreparedValueToEarlyReturnRector::class, - ShortenElseIfRector::class, UnusedForeachValueToArrayKeysRector::class, ChangeArrayPushToArrayAssignRector::class, RemoveErrorSuppressInTryCatchStmtsRector::class, @@ -210,4 +208,4 @@ // keep '\\' prefix string on string '\Foo\Bar' StringClassNameToClassConstantRector::SHOULD_KEEP_PRE_SLASH => true, ]) - ->withCodeQualityLevel(31); + ->withCodeQualityLevel(34); diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index 2115714106bc..732cb370382d 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -52,12 +52,12 @@ public function __construct($logger = null) /** * Runs a command given * - * @return int|void Exit code + * @return int Exit code */ public function run(string $command, array $params) { if (! $this->verifyCommand($command, $this->commands)) { - return; + return EXIT_ERROR; } // The file would have already been loaded during the diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 8ff5d2ae5b9b..ef4db424a5f0 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -318,7 +318,7 @@ private function configureKint(): void * * @param bool $returnResponse Used for testing purposes only. * - * @return ResponseInterface|void + * @return ResponseInterface|null */ public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false) { @@ -379,6 +379,8 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon } $this->sendResponse(); + + return null; } /** @@ -863,7 +865,7 @@ protected function determinePath() * controller method and make the script go. If it's not able to, will * show the appropriate Page Not Found error. * - * @return ResponseInterface|string|void + * @return ResponseInterface|string|null */ protected function startController() { @@ -889,6 +891,8 @@ protected function startController() ) { throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); } + + return null; } /** diff --git a/system/Commands/Database/MigrateRollback.php b/system/Commands/Database/MigrateRollback.php index e1c788e7702e..cb6cbe3a7adb 100644 --- a/system/Commands/Database/MigrateRollback.php +++ b/system/Commands/Database/MigrateRollback.php @@ -74,7 +74,7 @@ public function run(array $params) $force = array_key_exists('f', $params) || CLI::getOption('f'); if (! $force && CLI::prompt(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'n') { - return; + return null; } // @codeCoverageIgnoreEnd } @@ -115,5 +115,7 @@ public function run(array $params) $this->showError($e); // @codeCoverageIgnoreEnd } + + return null; } } diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index ad1967120af5..73cc2677274f 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -3019,7 +3019,7 @@ protected function _delete(string $table): string * * @param array|string $table The table to inspect * - * @return string|void + * @return string|null */ protected function trackAliases($table) { @@ -3028,7 +3028,7 @@ protected function trackAliases($table) $this->trackAliases($t); } - return; + return null; } // Does the string contain a comma? If so, we need to separate @@ -3048,6 +3048,8 @@ protected function trackAliases($table) // Store the alias, if it doesn't already exist $this->db->addTableAlias($alias); } + + return null; } /** diff --git a/system/Filters/CSRF.php b/system/Filters/CSRF.php index beb15f62b03a..ea9a9939f2de 100644 --- a/system/Filters/CSRF.php +++ b/system/Filters/CSRF.php @@ -35,14 +35,14 @@ class CSRF implements FilterInterface * * @param list|null $arguments * - * @return RedirectResponse|void + * @return RedirectResponse|null * * @throws SecurityException */ public function before(RequestInterface $request, $arguments = null) { if (! $request instanceof IncomingRequest) { - return; + return null; } /** @var Security $security */ @@ -57,16 +57,17 @@ public function before(RequestInterface $request, $arguments = null) throw $e; } + + return null; } /** * We don't have anything to do here. * * @param list|null $arguments - * - * @return void */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/system/Filters/Cors.php b/system/Filters/Cors.php index 93ca551b42f2..9a9bbb208152 100644 --- a/system/Filters/Cors.php +++ b/system/Filters/Cors.php @@ -48,18 +48,18 @@ public function __construct(array $config = []) /** * @param list|null $arguments * - * @return ResponseInterface|string|void + * @return ResponseInterface|null */ public function before(RequestInterface $request, $arguments = null) { if (! $request instanceof IncomingRequest) { - return; + return null; } $this->createCorsService($arguments); if (! $this->cors->isPreflightRequest($request)) { - return; + return null; } /** @var ResponseInterface $response */ @@ -88,12 +88,12 @@ private function createCorsService(?array $arguments): void /** * @param list|null $arguments * - * @return ResponseInterface|void + * @return ResponseInterface|null */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { if (! $request instanceof IncomingRequest) { - return; + return null; } $this->createCorsService($arguments); diff --git a/system/Filters/DebugToolbar.php b/system/Filters/DebugToolbar.php index 9f864ee572c5..7199b85f9d62 100644 --- a/system/Filters/DebugToolbar.php +++ b/system/Filters/DebugToolbar.php @@ -30,6 +30,7 @@ class DebugToolbar implements FilterInterface */ public function before(RequestInterface $request, $arguments = null) { + return null; } /** @@ -41,5 +42,7 @@ public function before(RequestInterface $request, $arguments = null) public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { service('toolbar')->prepare($request, $response); + + return null; } } diff --git a/system/Filters/FilterInterface.php b/system/Filters/FilterInterface.php index 0353fb2ab9d0..6df33e912b87 100644 --- a/system/Filters/FilterInterface.php +++ b/system/Filters/FilterInterface.php @@ -33,7 +33,7 @@ interface FilterInterface * * @param list|null $arguments * - * @return RequestInterface|ResponseInterface|string|void + * @return RequestInterface|ResponseInterface|string|null */ public function before(RequestInterface $request, $arguments = null); @@ -45,7 +45,7 @@ public function before(RequestInterface $request, $arguments = null); * * @param list|null $arguments * - * @return ResponseInterface|void + * @return ResponseInterface|null */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null); } diff --git a/system/Filters/ForceHTTPS.php b/system/Filters/ForceHTTPS.php index f415bd00e3b8..aa9cb3716d08 100644 --- a/system/Filters/ForceHTTPS.php +++ b/system/Filters/ForceHTTPS.php @@ -32,14 +32,14 @@ class ForceHTTPS implements FilterInterface * * @param array|null $arguments * - * @return ResponseInterface|void + * @return ResponseInterface|null */ public function before(RequestInterface $request, $arguments = null) { $config = config(App::class); if ($config->forceGlobalSecureRequests !== true) { - return; + return null; } $response = service('response'); @@ -49,16 +49,17 @@ public function before(RequestInterface $request, $arguments = null) } catch (RedirectException $e) { return $e->getResponse(); } + + return null; } /** * We don't have anything to do here. * * @param array|null $arguments - * - * @return void */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/system/Filters/Honeypot.php b/system/Filters/Honeypot.php index cdff293aacb4..4b24691ef905 100644 --- a/system/Filters/Honeypot.php +++ b/system/Filters/Honeypot.php @@ -36,12 +36,14 @@ class Honeypot implements FilterInterface public function before(RequestInterface $request, $arguments = null) { if (! $request instanceof IncomingRequest) { - return; + return null; } if (service('honeypot')->hasContent($request)) { throw HoneypotException::isBot(); } + + return null; } /** @@ -52,5 +54,7 @@ public function before(RequestInterface $request, $arguments = null) public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { service('honeypot')->attachHoneypot($response); + + return null; } } diff --git a/system/Filters/InvalidChars.php b/system/Filters/InvalidChars.php index 542b12d7ffbe..1cd775153c0e 100644 --- a/system/Filters/InvalidChars.php +++ b/system/Filters/InvalidChars.php @@ -48,13 +48,11 @@ class InvalidChars implements FilterInterface * Check invalid characters. * * @param list|null $arguments - * - * @return void */ public function before(RequestInterface $request, $arguments = null) { if (! $request instanceof IncomingRequest) { - return; + return null; } $data = [ @@ -69,17 +67,18 @@ public function before(RequestInterface $request, $arguments = null) $this->checkEncoding($values); $this->checkControl($values); } + + return null; } /** * We don't have anything to do here. * * @param list|null $arguments - * - * @return void */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } /** diff --git a/system/Filters/PageCache.php b/system/Filters/PageCache.php index a3d3af832a65..b0013c81ce37 100644 --- a/system/Filters/PageCache.php +++ b/system/Filters/PageCache.php @@ -38,7 +38,7 @@ public function __construct() * * @param array|null $arguments * - * @return ResponseInterface|void + * @return ResponseInterface|null */ public function before(RequestInterface $request, $arguments = null) { @@ -51,14 +51,14 @@ public function before(RequestInterface $request, $arguments = null) if ($cachedResponse instanceof ResponseInterface) { return $cachedResponse; } + + return null; } /** * Cache the page. * * @param array|null $arguments - * - * @return void */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { @@ -72,6 +72,10 @@ public function after(RequestInterface $request, ResponseInterface $response, $a // so that we can have live speed updates along the way. // Must be run after filters to preserve the Response headers. $this->pageCache->make($request, $response); + + return $response; } + + return null; } } diff --git a/system/Filters/PerformanceMetrics.php b/system/Filters/PerformanceMetrics.php index f2371c7a8d6d..0a9db2078500 100644 --- a/system/Filters/PerformanceMetrics.php +++ b/system/Filters/PerformanceMetrics.php @@ -28,14 +28,13 @@ class PerformanceMetrics implements FilterInterface */ public function before(RequestInterface $request, $arguments = null) { + return null; } /** * Replaces the performance metrics. * * @param array|null $arguments - * - * @return void */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { @@ -57,6 +56,10 @@ public function after(RequestInterface $request, ResponseInterface $response, $a ); $response->setBody($output); + + return $response; } + + return null; } } diff --git a/system/Filters/SecureHeaders.php b/system/Filters/SecureHeaders.php index b8bd6e05f8eb..42e5842384db 100644 --- a/system/Filters/SecureHeaders.php +++ b/system/Filters/SecureHeaders.php @@ -52,24 +52,23 @@ class SecureHeaders implements FilterInterface * We don't have anything to do here. * * @param list|null $arguments - * - * @return void */ public function before(RequestInterface $request, $arguments = null) { + return null; } /** * Add security headers. * * @param list|null $arguments - * - * @return void */ public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { foreach ($this->headers as $header => $value) { $response->setHeader($header, $value); } + + return $response; } } diff --git a/system/Images/Handlers/BaseHandler.php b/system/Images/Handlers/BaseHandler.php index a6607e04d160..033c4e6beda7 100644 --- a/system/Images/Handlers/BaseHandler.php +++ b/system/Images/Handlers/BaseHandler.php @@ -702,6 +702,8 @@ public function __call(string $name, array $args = []) if (method_exists($this->image(), $name)) { return $this->image()->{$name}(...$args); } + + return null; } /** diff --git a/system/Language/Language.php b/system/Language/Language.php index 1ab2c5269195..c0591328f5ef 100644 --- a/system/Language/Language.php +++ b/system/Language/Language.php @@ -232,7 +232,7 @@ protected function formatMessage($message, array $args = []) * will return the file's contents, otherwise will merge with * the existing language lines. * - * @return array|void + * @return list|null */ protected function load(string $file, string $locale, bool $return = false) { @@ -265,6 +265,8 @@ protected function load(string $file, string $locale, bool $return = false) // Merge our string $this->language[$locale][$file] = $lang; + + return null; } /** diff --git a/system/Security/CheckPhpIni.php b/system/Security/CheckPhpIni.php index 59cd587360d5..1293e1c1db87 100644 --- a/system/Security/CheckPhpIni.php +++ b/system/Security/CheckPhpIni.php @@ -27,7 +27,7 @@ class CheckPhpIni /** * @param bool $isCli Set false if you run via Web * - * @return string|void HTML string or void in CLI + * @return string|null HTML string or void in CLI */ public static function run(bool $isCli = true) { @@ -40,7 +40,7 @@ public static function run(bool $isCli = true) if ($isCli) { self::outputForCli($output, $thead, $tbody); - return; + return null; } // Web diff --git a/system/Session/Session.php b/system/Session/Session.php index 44f0841932d0..61dd2b6aff20 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -205,7 +205,7 @@ public function __construct(SessionHandlerInterface $driver, SessionConfig $conf /** * Initialize the session container and starts up the session. * - * @return $this|void + * @return $this|null */ public function start() { @@ -213,20 +213,20 @@ public function start() // @codeCoverageIgnoreStart $this->logger->debug('Session: Initialization under CLI aborted.'); - return; + return null; // @codeCoverageIgnoreEnd } if ((bool) ini_get('session.auto_start')) { $this->logger->error('Session: session.auto_start is enabled in php.ini. Aborting.'); - return; + return null; } if (session_status() === PHP_SESSION_ACTIVE) { $this->logger->warning('Session: Sessions is enabled, and one exists. Please don\'t $session->start();'); - return; + return null; } $this->configure(); diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index 62c0fff83844..f237ab862a64 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -461,7 +461,7 @@ public function assertCloseEnough(int $expected, $actual, string $message = '', * @param mixed $expected * @param mixed $actual * - * @return bool|void + * @return bool|null * * @throws Exception */ @@ -482,6 +482,8 @@ public function assertCloseEnoughString($expected, $actual, string $message = '' } catch (Exception) { return false; } + + return null; } // -------------------------------------------------------------------- diff --git a/tests/_support/Filters/Customfilter.php b/tests/_support/Filters/Customfilter.php index ac8bce0f0271..7cfb89f60d48 100644 --- a/tests/_support/Filters/Customfilter.php +++ b/tests/_support/Filters/Customfilter.php @@ -26,7 +26,8 @@ public function before(RequestInterface $request, $arguments = null) return $request; } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/tests/_support/Filters/RedirectFilter.php b/tests/_support/Filters/RedirectFilter.php index 8ab62eb665ca..b2e9289d6b6b 100644 --- a/tests/_support/Filters/RedirectFilter.php +++ b/tests/_support/Filters/RedirectFilter.php @@ -24,7 +24,8 @@ public function before(RequestInterface $request, $arguments = null) return redirect()->to('login'); } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/tests/system/Filters/fixtures/GoogleCurious.php b/tests/system/Filters/fixtures/GoogleCurious.php index f00bc065f1d7..6a6871fcc8d4 100644 --- a/tests/system/Filters/fixtures/GoogleCurious.php +++ b/tests/system/Filters/fixtures/GoogleCurious.php @@ -24,7 +24,8 @@ public function before(RequestInterface $request, $arguments = null) return 'This is curious'; } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/tests/system/Filters/fixtures/GoogleEmpty.php b/tests/system/Filters/fixtures/GoogleEmpty.php index 6b0003e8de16..9de00d28f514 100644 --- a/tests/system/Filters/fixtures/GoogleEmpty.php +++ b/tests/system/Filters/fixtures/GoogleEmpty.php @@ -24,7 +24,8 @@ public function before(RequestInterface $request, $arguments = null) return ''; } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/tests/system/Filters/fixtures/GoogleYou.php b/tests/system/Filters/fixtures/GoogleYou.php index 7d2913866b30..5911940b2954 100644 --- a/tests/system/Filters/fixtures/GoogleYou.php +++ b/tests/system/Filters/fixtures/GoogleYou.php @@ -27,7 +27,8 @@ public function before(RequestInterface $request, $arguments = null) return $response; } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/tests/system/Filters/fixtures/Multiple1.php b/tests/system/Filters/fixtures/Multiple1.php index e9f4f49a10ab..6d2cee231808 100644 --- a/tests/system/Filters/fixtures/Multiple1.php +++ b/tests/system/Filters/fixtures/Multiple1.php @@ -26,7 +26,8 @@ public function before(RequestInterface $request, $arguments = null) return $request; } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/tests/system/Filters/fixtures/Multiple2.php b/tests/system/Filters/fixtures/Multiple2.php index 5520ce549930..d86f8fa79399 100644 --- a/tests/system/Filters/fixtures/Multiple2.php +++ b/tests/system/Filters/fixtures/Multiple2.php @@ -26,7 +26,8 @@ public function before(RequestInterface $request, $arguments = null) return $request; } - public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { + return null; } } diff --git a/utils/phpstan-baseline/missingType.iterableValue.neon b/utils/phpstan-baseline/missingType.iterableValue.neon index d2c0fdf7c22c..60bb03868b8a 100644 --- a/utils/phpstan-baseline/missingType.iterableValue.neon +++ b/utils/phpstan-baseline/missingType.iterableValue.neon @@ -1,4 +1,4 @@ -# total 1673 errors +# total 1672 errors parameters: ignoreErrors: @@ -4707,11 +4707,6 @@ parameters: count: 1 path: ../../system/Language/Language.php - - - message: '#^Method CodeIgniter\\Language\\Language\:\:load\(\) return type has no value type specified in iterable type array\.$#' - count: 1 - path: ../../system/Language/Language.php - - message: '#^Method CodeIgniter\\Language\\Language\:\:parseLine\(\) return type has no value type specified in iterable type array\.$#' count: 1 diff --git a/utils/phpstan-baseline/return.missing.neon b/utils/phpstan-baseline/return.missing.neon index 151fdaa34156..d6cf55ba8d46 100644 --- a/utils/phpstan-baseline/return.missing.neon +++ b/utils/phpstan-baseline/return.missing.neon @@ -1,12 +1,7 @@ -# total 2 errors +# total 1 error parameters: ignoreErrors: - - - message: '#^Method CodeIgniter\\Images\\Handlers\\BaseHandler\:\:__call\(\) should return mixed but return statement is missing\.$#' - count: 1 - path: ../../system/Images/Handlers/BaseHandler.php - - message: '#^Method CodeIgniter\\Test\\Mock\\MockResult\:\:fetchAssoc\(\) should return mixed but return statement is missing\.$#' count: 1