From 71599deaba8b6a23522a1515872a9b2715029746 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Nov 2022 09:21:56 +0900 Subject: [PATCH 1/8] Change min PHP version to 8.0 --- public/index.php | 2 +- spark | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/index.php b/public/index.php index 96e7f45ff4ac..eb3abd0fb42b 100644 --- a/public/index.php +++ b/public/index.php @@ -1,7 +1,7 @@ Date: Tue, 29 Nov 2022 09:25:02 +0900 Subject: [PATCH 2/8] chore: require ^8.0 in composer.json --- admin/framework/composer.json | 2 +- admin/starter/composer.json | 2 +- admin/userguide/composer.json | 2 +- composer.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/admin/framework/composer.json b/admin/framework/composer.json index 982204b8c83b..b0953a9f8d07 100644 --- a/admin/framework/composer.json +++ b/admin/framework/composer.json @@ -5,7 +5,7 @@ "homepage": "https://codeigniter.com", "license": "MIT", "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "ext-intl": "*", "ext-json": "*", "ext-mbstring": "*", diff --git a/admin/starter/composer.json b/admin/starter/composer.json index e52218478b04..be95dfc15b26 100644 --- a/admin/starter/composer.json +++ b/admin/starter/composer.json @@ -5,7 +5,7 @@ "homepage": "https://codeigniter.com", "license": "MIT", "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "codeigniter4/framework": "^4.0" }, "require-dev": { diff --git a/admin/userguide/composer.json b/admin/userguide/composer.json index 007664b9b2ec..35860af8d02b 100644 --- a/admin/userguide/composer.json +++ b/admin/userguide/composer.json @@ -5,7 +5,7 @@ "homepage": "https://codeigniter.com", "license": "MIT", "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "codeigniter4/framework": "^4" }, "support": { diff --git a/composer.json b/composer.json index 5e12de117047..8569419f338c 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "homepage": "https://codeigniter.com", "license": "MIT", "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "ext-intl": "*", "ext-json": "*", "ext-mbstring": "*", From 202cb8466311ebf6c991e68e0bb183daff42a3e3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Nov 2022 09:25:55 +0900 Subject: [PATCH 3/8] chore: drop PHP 7.4 in workflows --- .github/workflows/test-coding-standards.yml | 2 +- .github/workflows/test-phpunit.yml | 6 +----- .github/workflows/test-rector.yml | 2 +- admin/starter/.github/workflows/phpunit.yml | 4 ++-- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test-coding-standards.yml b/.github/workflows/test-coding-standards.yml index 99e6bd4a3b14..699b03524de5 100644 --- a/.github/workflows/test-coding-standards.yml +++ b/.github/workflows/test-coding-standards.yml @@ -25,8 +25,8 @@ jobs: fail-fast: false matrix: php-version: - - '7.4' - '8.0' + - '8.1' steps: - name: Checkout diff --git a/.github/workflows/test-phpunit.yml b/.github/workflows/test-phpunit.yml index 7c6182cac13c..a82a9ef073a4 100644 --- a/.github/workflows/test-phpunit.yml +++ b/.github/workflows/test-phpunit.yml @@ -50,7 +50,6 @@ jobs: strategy: matrix: php-version: - - '7.4' - '8.0' - '8.1' - '8.2' @@ -79,7 +78,6 @@ jobs: fail-fast: false matrix: php-version: - - '7.4' - '8.0' - '8.1' - '8.2' @@ -92,7 +90,7 @@ jobs: mysql-version: - '5.7' include: - - php-version: '7.4' + - php-version: '8.0' db-platform: MySQLi mysql-version: '8.0' - php-version: '8.2' @@ -120,7 +118,6 @@ jobs: strategy: matrix: php-version: - - '7.4' - '8.0' - '8.1' - '8.2' @@ -148,7 +145,6 @@ jobs: strategy: matrix: php-version: - - '7.4' - '8.0' - '8.1' - '8.2' diff --git a/.github/workflows/test-rector.yml b/.github/workflows/test-rector.yml index 7d8c11d61362..8288cd9725f1 100644 --- a/.github/workflows/test-rector.yml +++ b/.github/workflows/test-rector.yml @@ -40,7 +40,7 @@ jobs: strategy: fail-fast: false matrix: - php-versions: ['7.4', '8.0'] + php-versions: ['8.0', '8.1'] paths: - app - system diff --git a/admin/starter/.github/workflows/phpunit.yml b/admin/starter/.github/workflows/phpunit.yml index 73d413cad92f..9b54b83b8d22 100644 --- a/admin/starter/.github/workflows/phpunit.yml +++ b/admin/starter/.github/workflows/phpunit.yml @@ -2,7 +2,7 @@ name: PHPUnit on: pull_request: - branches: + branches: - develop jobs: @@ -11,7 +11,7 @@ jobs: strategy: matrix: - php-versions: ['7.4', '8.0'] + php-versions: ['8.0', '8.1'] runs-on: ubuntu-latest From 7142f27c1cc93ab1daba9079d423097e27b3df9a Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Nov 2022 09:26:53 +0900 Subject: [PATCH 4/8] docs: require min PHP version 8.0 --- README.md | 2 +- admin/framework/README.md | 2 +- admin/starter/README.md | 2 +- contributing/pull_request.md | 2 +- user_guide_src/source/installation/upgrade_4xx.rst | 2 +- user_guide_src/source/intro/requirements.rst | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7d2940a21246..512456e364a9 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ Made with [contrib.rocks](https://contrib.rocks). ## Server Requirements -PHP version 7.4 or higher is required, with the following extensions installed: +PHP version 8.0 or higher is required, with the following extensions installed: - [intl](http://php.net/manual/en/intl.requirements.php) - [mbstring](http://php.net/manual/en/mbstring.installation.php) diff --git a/admin/framework/README.md b/admin/framework/README.md index 870e5f96adff..914d9a17427c 100644 --- a/admin/framework/README.md +++ b/admin/framework/README.md @@ -42,7 +42,7 @@ Please read the [*Contributing to CodeIgniter*](https://github.com/codeigniter4/ ## Server Requirements -PHP version 7.4 or higher is required, with the following extensions installed: +PHP version 8.0 or higher is required, with the following extensions installed: - [intl](http://php.net/manual/en/intl.requirements.php) - [mbstring](http://php.net/manual/en/mbstring.installation.php) diff --git a/admin/starter/README.md b/admin/starter/README.md index 461e949f2f26..8062a2cb6c13 100644 --- a/admin/starter/README.md +++ b/admin/starter/README.md @@ -50,7 +50,7 @@ Problems with it can be raised on our forum, or as issues in the main repository ## Server Requirements -PHP version 7.4 or higher is required, with the following extensions installed: +PHP version 8.0 or higher is required, with the following extensions installed: - [intl](http://php.net/manual/en/intl.requirements.php) - [mbstring](http://php.net/manual/en/mbstring.installation.php) diff --git a/contributing/pull_request.md b/contributing/pull_request.md index a28e5e75d171..85dd044ec4c1 100644 --- a/contributing/pull_request.md +++ b/contributing/pull_request.md @@ -136,7 +136,7 @@ See [Contribution CSS](./css.md). ### Compatibility -CodeIgniter4 requires [PHP 7.4](https://php.net/releases/7_4_0.php). +CodeIgniter4 requires [PHP 8.0](https://php.net/releases/8_0_0.php). ### Backwards Compatibility diff --git a/user_guide_src/source/installation/upgrade_4xx.rst b/user_guide_src/source/installation/upgrade_4xx.rst index 1a35121d8f7d..cced3913097e 100644 --- a/user_guide_src/source/installation/upgrade_4xx.rst +++ b/user_guide_src/source/installation/upgrade_4xx.rst @@ -41,7 +41,7 @@ Downloads Namespaces ========== -- CI4 is built for PHP 7.4+, and everything in the framework is namespaced, except for the helpers. +- CI4 is built for PHP 8.0+, and everything in the framework is namespaced, except for the helpers. Application Structure ===================== diff --git a/user_guide_src/source/intro/requirements.rst b/user_guide_src/source/intro/requirements.rst index 191e264733ec..b1b473192d22 100644 --- a/user_guide_src/source/intro/requirements.rst +++ b/user_guide_src/source/intro/requirements.rst @@ -2,7 +2,7 @@ Server Requirements ################### -`PHP `_ version 7.4 or newer is required, with the following PHP extensions are enabled: +`PHP `_ version 8.0 or newer is required, with the following PHP extensions are enabled: - `intl `_ - `mbstring `_ From 2e9b5cb22dfebb8c4393ded27e93d722adf13f51 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Nov 2022 09:28:14 +0900 Subject: [PATCH 5/8] refactor: remove version check `version_compare(PHP_VERSION, '8.0.0', '>=')` --- system/Autoloader/Autoloader.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index ad4de0329d66..f8b678a5e0c7 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -344,11 +344,7 @@ public function sanitizeFilename(string $filename): string ); } if ($result === false) { - if (version_compare(PHP_VERSION, '8.0.0', '>=')) { - $message = preg_last_error_msg(); - } else { - $message = 'Regex error. error code: ' . preg_last_error(); - } + $message = preg_last_error_msg(); throw new RuntimeException($message . '. filename: "' . $filename . '"'); } From c482999c6f0549a2ee84a91873af686e1a059008 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Nov 2022 09:32:35 +0900 Subject: [PATCH 6/8] docs: add changelog --- user_guide_src/source/changelogs/v4.3.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index 47c156120a2f..2ec05dd9522b 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -12,6 +12,7 @@ Release Date: Unreleased Highlights ********** +- Update minimal PHP requirement to 8.0. - TBD BREAKING @@ -305,6 +306,7 @@ Message Changes Changes ******* +- Update minimal PHP requirement to 8.0. - Config - All atomic type properties in ``Config`` classes have been typed. - Changed the processing of Spark commands: From 6c5297ea95e40c283336a143471e5b4c6df18afc Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Nov 2022 12:26:03 +0900 Subject: [PATCH 7/8] refactor: upgrade to PHP 8.0 with rector Applied rules: * StrEndsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562 https://github.com/php/php-src/pull/5179) --- system/Autoloader/Autoloader.php | 6 +- system/Autoloader/FileLocator.php | 12 ++-- system/BaseModel.php | 2 +- system/CLI/CLI.php | 2 +- system/CLI/Commands.php | 2 +- system/CLI/GeneratorTrait.php | 2 +- system/Cache/Handlers/PredisHandler.php | 21 ++---- system/Cache/Handlers/RedisHandler.php | 21 ++---- system/CodeIgniter.php | 4 +- system/Commands/Database/CreateDatabase.php | 2 +- system/Commands/Encryption/GenerateKey.php | 2 +- system/Commands/Generators/CellGenerator.php | 2 +- system/Commands/Utilities/Publish.php | 4 +- .../ControllerMethodReader.php | 2 +- .../Utilities/Routes/FilterFinder.php | 4 +- system/Common.php | 66 +++++++----------- system/ComposerScripts.php | 2 +- system/Config/BaseConfig.php | 4 +- system/Config/DotEnv.php | 8 +-- system/Config/Factories.php | 8 +-- system/Cookie/Cookie.php | 17 ++--- system/Cookie/CookieStore.php | 2 +- system/Database/BaseBuilder.php | 20 +++--- system/Database/BaseConnection.php | 16 ++--- system/Database/Database.php | 4 +- system/Database/Forge.php | 4 +- system/Database/MigrationRunner.php | 2 +- system/Database/OCI8/Connection.php | 12 ++-- system/Database/OCI8/Forge.php | 4 +- system/Database/Postgre/Connection.php | 6 +- system/Database/Query.php | 4 +- system/Database/RawSql.php | 4 +- system/Database/SQLSRV/Builder.php | 6 +- system/Database/SQLSRV/Connection.php | 6 +- system/Database/SQLite3/Connection.php | 2 +- system/Database/SQLite3/Forge.php | 2 +- system/Database/SQLite3/Table.php | 2 +- system/Database/Seeder.php | 2 +- system/Debug/Exceptions.php | 67 ++++++------------- system/Debug/Toolbar.php | 4 +- system/Debug/Toolbar/Collectors/Database.php | 2 +- system/Debug/Toolbar/Collectors/Files.php | 2 +- system/Debug/Toolbar/Collectors/Routes.php | 2 +- system/Email/Email.php | 22 +++--- system/Entity/Cast/ArrayCast.php | 2 +- system/Entity/Entity.php | 4 +- system/Entity/Exceptions/CastException.php | 27 +++----- system/Exceptions/CastException.php | 27 +++----- system/Exceptions/FrameworkException.php | 2 +- system/Files/File.php | 17 ++--- system/Files/FileCollection.php | 4 +- system/Filters/Filters.php | 4 +- system/HTTP/CURLRequest.php | 8 +-- system/HTTP/ContentSecurityPolicy.php | 2 +- system/HTTP/Files/FileCollection.php | 6 +- system/HTTP/Files/UploadedFile.php | 2 +- system/HTTP/Header.php | 4 +- system/HTTP/IncomingRequest.php | 47 +++++-------- system/HTTP/RedirectResponse.php | 2 +- system/HTTP/RequestTrait.php | 4 +- system/HTTP/ResponseTrait.php | 15 ++--- system/HTTP/URI.php | 23 ++++--- system/HTTP/UserAgent.php | 3 +- system/Helpers/filesystem_helper.php | 10 +-- system/Helpers/form_helper.php | 6 +- system/Helpers/html_helper.php | 2 +- system/Helpers/number_helper.php | 4 +- system/Helpers/text_helper.php | 2 +- system/Helpers/url_helper.php | 9 +-- system/Honeypot/Honeypot.php | 2 +- system/Images/Handlers/BaseHandler.php | 35 +++------- system/Images/Handlers/ImageMagickHandler.php | 35 +++------- system/Language/Language.php | 2 +- system/Log/Handlers/ChromeLoggerHandler.php | 2 +- system/Log/Handlers/FileHandler.php | 2 +- system/Log/Logger.php | 4 +- system/Model.php | 6 +- system/Pager/Pager.php | 2 +- system/Publisher/Publisher.php | 4 +- system/RESTful/BaseResource.php | 2 +- system/Router/AutoRouter.php | 2 +- system/Router/AutoRouterImproved.php | 4 +- system/Router/RouteCollection.php | 14 ++-- system/Router/Router.php | 12 ++-- system/Security/Security.php | 2 +- system/Test/CIUnitTestCase.php | 4 +- system/Test/ControllerTestTrait.php | 2 +- system/Test/DOMParser.php | 6 +- system/Test/DatabaseTestTrait.php | 2 +- system/Test/Fabricator.php | 2 +- system/Test/FilterTestTrait.php | 4 +- system/Test/TestLogger.php | 2 +- system/Typography/Typography.php | 8 +-- system/Validation/CreditCardRules.php | 2 +- system/Validation/FormatRules.php | 19 ++---- system/Validation/Rules.php | 10 +-- system/Validation/Validation.php | 12 ++-- system/View/Cell.php | 8 +-- system/View/Cells/Cell.php | 5 +- system/View/Filters.php | 19 ++---- system/View/Parser.php | 10 +-- tests/system/AutoReview/FrameworkCodeTest.php | 4 +- .../system/Commands/CommandGeneratorTest.php | 2 +- tests/system/CommonFunctionsTest.php | 4 +- tests/system/CommonHelperTest.php | 4 +- tests/system/CommonSingleServiceTest.php | 6 +- tests/system/Database/Live/DeleteTest.php | 2 +- tests/system/Database/Live/GetTest.php | 2 +- .../Database/Live/PreparedQueryTest.php | 2 +- tests/system/Database/Live/UpdateTest.php | 4 +- .../Migrations/MigrationRunnerTest.php | 6 +- tests/system/Debug/ExceptionsTest.php | 2 +- .../system/HTTP/ContentSecurityPolicyTest.php | 4 +- tests/system/Helpers/ArrayHelperTest.php | 8 +-- tests/system/Models/DeleteModelTest.php | 2 +- 115 files changed, 378 insertions(+), 545 deletions(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index f8b678a5e0c7..f0a824dc1ea8 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -275,7 +275,7 @@ public function loadClass(string $class) */ protected function loadInNamespace(string $class) { - if (strpos($class, '\\') === false) { + if (! str_contains($class, '\\')) { return false; } @@ -283,7 +283,7 @@ protected function loadInNamespace(string $class) foreach ($directories as $directory) { $directory = rtrim($directory, '\\/'); - if (strpos($class, $namespace) === 0) { + if (str_starts_with($class, $namespace)) { $filePath = $directory . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen($namespace))) . '.php'; $filename = $this->includeFile($filePath); @@ -400,7 +400,7 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa foreach ($srcPaths as $path) { foreach ($installPaths as $installPath) { - if ($installPath === substr($path, 0, strlen($installPath))) { + if (str_starts_with($path, $installPath)) { $add = true; break 2; } diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index 14d7982c49b4..2129bd6924b3 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -44,12 +44,12 @@ public function locateFile(string $file, ?string $folder = null, string $ext = ' $file = $this->ensureExt($file, $ext); // Clears the folder name if it is at the beginning of the filename - if (! empty($folder) && strpos($file, $folder) === 0) { + if (! empty($folder) && str_starts_with($file, $folder)) { $file = substr($file, strlen($folder . '/')); } // Is not namespaced? Try the application folder. - if (strpos($file, '\\') === false) { + if (! str_contains($file, '\\')) { return $this->legacyLocate($file, $folder); } @@ -71,7 +71,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = ' $namespaces = $this->autoloader->getNamespace(); foreach (array_keys($namespaces) as $namespace) { - if (substr($file, 0, strlen($namespace)) === $namespace) { + if (str_starts_with($file, $namespace)) { // There may be sub-namespaces of the same vendor, // so overwrite them with namespaces found later. $paths = $namespaces[$namespace]; @@ -94,7 +94,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = ' // If we have a folder name, then the calling function // expects this file to be within that folder, like 'Views', // or 'libraries'. - if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false) { + if (! empty($folder) && ! str_contains($path . $filename, '/' . $folder . '/')) { $path .= trim($folder, '/') . '/'; } @@ -177,7 +177,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp = if ($prioritizeApp) { $foundPaths[] = $fullPath; - } elseif (strpos($fullPath, APPPATH) === 0) { + } elseif (str_starts_with($fullPath, APPPATH)) { $appPaths[] = $fullPath; } else { $foundPaths[] = $fullPath; @@ -201,7 +201,7 @@ protected function ensureExt(string $path, string $ext): string if ($ext) { $ext = '.' . $ext; - if (substr($path, -strlen($ext)) !== $ext) { + if (! str_ends_with($path, $ext)) { $path .= $ext; } } diff --git a/system/BaseModel.php b/system/BaseModel.php index 98d875300cfe..58458a65b037 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -581,7 +581,7 @@ public function find($id = null) */ public function findColumn(string $columnName) { - if (strpos($columnName, ',') !== false) { + if (str_contains($columnName, ',')) { throw DataException::forFindColumnHaveMultipleColumns(); } diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index 503e09b51edc..8b5d61e6ca98 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -579,7 +579,7 @@ public static function color(string $text, string $foreground, ?string $backgrou $newText = ''; // Detect if color method was already in use with this text - if (strpos($text, "\033[0m") !== false) { + if (str_contains($text, "\033[0m")) { $pattern = '/\\033\\[0;.+?\\033\\[0m/u'; preg_match_all($pattern, $text, $matches); diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index 0358e8ad2e18..7ee7f7d4e27b 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -171,7 +171,7 @@ protected function getCommandAlternatives(string $name, array $collection): arra foreach (array_keys($collection) as $commandName) { $lev = levenshtein($name, $commandName); - if ($lev <= strlen($commandName) / 3 || strpos($commandName, $name) !== false) { + if ($lev <= strlen($commandName) / 3 || str_contains($commandName, $name)) { $alternatives[$commandName] = $lev; } } diff --git a/system/CLI/GeneratorTrait.php b/system/CLI/GeneratorTrait.php index 97f011f80ca6..0cb7fcda3463 100644 --- a/system/CLI/GeneratorTrait.php +++ b/system/CLI/GeneratorTrait.php @@ -253,7 +253,7 @@ protected function qualifyClassName(): string // Gets the namespace from input. Don't forget the ending backslash! $namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\') . '\\'; - if (strncmp($class, $namespace, strlen($namespace)) === 0) { + if (str_starts_with($class, $namespace)) { return $class; // @codeCoverageIgnore } diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index cf44f634e86b..fed1c83692c4 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -81,22 +81,11 @@ public function get(string $key) return null; } - switch ($data['__ci_type']) { - case 'array': - case 'object': - return unserialize($data['__ci_value']); - - case 'boolean': - case 'integer': - case 'double': // Yes, 'double' is returned and NOT 'float' - case 'string': - case 'NULL': - return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null; - - case 'resource': - default: - return null; - } + return match ($data['__ci_type']) { + 'array', 'object' => unserialize($data['__ci_value']), + 'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null, + default => null, + }; } /** diff --git a/system/Cache/Handlers/RedisHandler.php b/system/Cache/Handlers/RedisHandler.php index 8b3af5856319..8d052bc50ee4 100644 --- a/system/Cache/Handlers/RedisHandler.php +++ b/system/Cache/Handlers/RedisHandler.php @@ -107,22 +107,11 @@ public function get(string $key) return null; } - switch ($data['__ci_type']) { - case 'array': - case 'object': - return unserialize($data['__ci_value']); - - case 'boolean': - case 'integer': - case 'double': // Yes, 'double' is returned and NOT 'float' - case 'string': - case 'NULL': - return settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null; - - case 'resource': - default: - return null; - } + return match ($data['__ci_type']) { + 'array', 'object' => unserialize($data['__ci_value']), + 'boolean', 'integer', 'double', 'string', 'NULL' => settype($data['__ci_value'], $data['__ci_type']) ? $data['__ci_value'] : null, + default => null, + }; } /** diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index cece29793aaf..b6e9f22ab2fb 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -877,7 +877,7 @@ protected function startController() $this->benchmark->start('controller_constructor'); // Is it routed to a Closure? - if (is_object($this->controller) && (get_class($this->controller) === 'Closure')) { + if (is_object($this->controller) && ($this->controller::class === 'Closure')) { $controller = $this->controller; return $controller(...$this->router->params()); @@ -1069,7 +1069,7 @@ public function storePreviousURL($uri) } // Ignore non-HTML responses - if (strpos($this->response->getHeaderLine('Content-Type'), 'text/html') === false) { + if (! str_contains($this->response->getHeaderLine('Content-Type'), 'text/html')) { return; } diff --git a/system/Commands/Database/CreateDatabase.php b/system/Commands/Database/CreateDatabase.php index a3922bd66dee..5039e8489755 100644 --- a/system/Commands/Database/CreateDatabase.php +++ b/system/Commands/Database/CreateDatabase.php @@ -110,7 +110,7 @@ public function run(array $params) $config->{$group}['database'] = $name; if ($name !== ':memory:') { - $dbName = strpos($name, DIRECTORY_SEPARATOR) === false ? WRITEPATH . $name : $name; + $dbName = ! str_contains($name, DIRECTORY_SEPARATOR) ? WRITEPATH . $name : $name; if (is_file($dbName)) { CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red'); diff --git a/system/Commands/Encryption/GenerateKey.php b/system/Commands/Encryption/GenerateKey.php index 5837bfb02636..664fc9f40ade 100644 --- a/system/Commands/Encryption/GenerateKey.php +++ b/system/Commands/Encryption/GenerateKey.php @@ -166,7 +166,7 @@ protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey): $oldFileContents = (string) file_get_contents($envFile); $replacementKey = "\nencryption.key = {$newKey}"; - if (strpos($oldFileContents, 'encryption.key') === false) { + if (! str_contains($oldFileContents, 'encryption.key')) { return file_put_contents($envFile, $replacementKey, FILE_APPEND) !== false; } diff --git a/system/Commands/Generators/CellGenerator.php b/system/Commands/Generators/CellGenerator.php index b13700687dcb..882142325e1e 100644 --- a/system/Commands/Generators/CellGenerator.php +++ b/system/Commands/Generators/CellGenerator.php @@ -90,7 +90,7 @@ public function run(array $params) $view = array_pop($segments); $view = str_replace('Cell', '', decamelize($view)); - if (strpos($view, '_cell') === false) { + if (! str_contains($view, '_cell')) { $view .= '_cell'; } $segments[] = $view; diff --git a/system/Commands/Utilities/Publish.php b/system/Commands/Utilities/Publish.php index cfed0472c439..d7cb840bd3b1 100644 --- a/system/Commands/Utilities/Publish.php +++ b/system/Commands/Utilities/Publish.php @@ -83,13 +83,13 @@ public function run(array $params) foreach ($publishers as $publisher) { if ($publisher->publish()) { CLI::write(lang('Publisher.publishSuccess', [ - get_class($publisher), + $publisher::class, count($publisher->getPublished()), $publisher->getDestination(), ]), 'green'); } else { CLI::error(lang('Publisher.publishFailure', [ - get_class($publisher), + $publisher::class, $publisher->getDestination(), ]), 'light_gray', 'red'); diff --git a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php index 3f373c433f1b..5ca65c16c2a7 100644 --- a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php +++ b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php @@ -61,7 +61,7 @@ public function read(string $class, string $defaultController = 'Home', string $ $methodName = $method->getName(); foreach ($this->httpMethods as $httpVerb) { - if (strpos($methodName, $httpVerb) === 0) { + if (str_starts_with($methodName, $httpVerb)) { // Remove HTTP verb prefix. $methodInUri = lcfirst(substr($methodName, strlen($httpVerb))); diff --git a/system/Commands/Utilities/Routes/FilterFinder.php b/system/Commands/Utilities/Routes/FilterFinder.php index 0f25eebf47f1..03a6e55e79ae 100644 --- a/system/Commands/Utilities/Routes/FilterFinder.php +++ b/system/Commands/Utilities/Routes/FilterFinder.php @@ -63,12 +63,12 @@ public function find(string $uri): array $this->filters->initialize($uri); return $this->filters->getFilters(); - } catch (RedirectException $e) { + } catch (RedirectException) { return [ 'before' => [], 'after' => [], ]; - } catch (PageNotFoundException $e) { + } catch (PageNotFoundException) { return [ 'before' => [''], 'after' => [''], diff --git a/system/Common.php b/system/Common.php index 9256ff1cf6a0..2550b591812e 100644 --- a/system/Common.php +++ b/system/Common.php @@ -91,25 +91,14 @@ function clean_path(string $path): string // Resolve relative paths $path = realpath($path) ?: $path; - switch (true) { - case strpos($path, APPPATH) === 0: - return 'APPPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(APPPATH)); - - case strpos($path, SYSTEMPATH) === 0: - return 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(SYSTEMPATH)); - - case strpos($path, FCPATH) === 0: - return 'FCPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(FCPATH)); - - case defined('VENDORPATH') && strpos($path, VENDORPATH) === 0: - return 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(VENDORPATH)); - - case strpos($path, ROOTPATH) === 0: - return 'ROOTPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(ROOTPATH)); - - default: - return $path; - } + return match (true) { + str_starts_with($path, APPPATH) => 'APPPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(APPPATH)), + str_starts_with($path, SYSTEMPATH) => 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(SYSTEMPATH)), + str_starts_with($path, FCPATH) => 'FCPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(FCPATH)), + defined('VENDORPATH') && str_starts_with($path, VENDORPATH) => 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(VENDORPATH)), + str_starts_with($path, ROOTPATH) => 'ROOTPATH' . DIRECTORY_SEPARATOR . substr($path, strlen(ROOTPATH)), + default => $path, + }; } } @@ -372,22 +361,13 @@ function env(string $key, $default = null) return $default; } - // Handle any boolean values - switch (strtolower($value)) { - case 'true': - return true; - - case 'false': - return false; - - case 'empty': - return ''; - - case 'null': - return null; - } - - return $value; + return match (strtolower($value)) { + 'true' => true, + 'false' => false, + 'empty' => '', + 'null' => null, + default => $value, + }; } } @@ -492,9 +472,9 @@ function force_https(int $duration = 31_536_000, ?RequestInterface $request = nu $baseURL = config('App')->baseURL; - if (strpos($baseURL, 'https://') === 0) { + if (str_starts_with($baseURL, 'https://')) { $authority = substr($baseURL, strlen('https://')); - } elseif (strpos($baseURL, 'http://') === 0) { + } elseif (str_starts_with($baseURL, 'http://')) { $authority = substr($baseURL, strlen('http://')); } else { $authority = $baseURL; @@ -597,7 +577,7 @@ function helper($filenames) $appHelper = null; $localIncludes = []; - if (strpos($filename, '_helper') === false) { + if (! str_contains($filename, '_helper')) { $filename .= '_helper'; } @@ -608,7 +588,7 @@ function helper($filenames) // If the file is namespaced, we'll just grab that // file and not search for any others - if (strpos($filename, '\\') !== false) { + if (str_contains($filename, '\\')) { $path = $loader->locateFile($filename, 'Helpers'); if (empty($path)) { @@ -622,9 +602,9 @@ function helper($filenames) $paths = $loader->search('Helpers/' . $filename); foreach ($paths as $path) { - if (strpos($path, APPPATH . 'Helpers' . DIRECTORY_SEPARATOR) === 0) { + if (str_starts_with($path, APPPATH . 'Helpers' . DIRECTORY_SEPARATOR)) { $appHelper = $path; - } elseif (strpos($path, SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR) === 0) { + } elseif (str_starts_with($path, SYSTEMPATH . 'Helpers' . DIRECTORY_SEPARATOR)) { $systemHelper = $path; } else { $localIncludes[] = $path; @@ -1211,7 +1191,7 @@ function view_cell(string $library, $params = null, int $ttl = 0, ?string $cache */ function class_basename($class) { - $class = is_object($class) ? get_class($class) : $class; + $class = is_object($class) ? $class::class : $class; return basename(str_replace('\\', '/', $class)); } @@ -1230,7 +1210,7 @@ function class_basename($class) function class_uses_recursive($class) { if (is_object($class)) { - $class = get_class($class); + $class = $class::class; } $results = []; diff --git a/system/ComposerScripts.php b/system/ComposerScripts.php index b95bdf54c4ae..eaf81a45c964 100644 --- a/system/ComposerScripts.php +++ b/system/ComposerScripts.php @@ -71,7 +71,7 @@ public static function postUpdate() foreach (self::$dependencies as $key => $dependency) { // Kint may be removed. - if (! is_dir($dependency['from']) && strpos($key, 'kint') === 0) { + if (! is_dir($dependency['from']) && str_starts_with($key, 'kint')) { continue; } diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 359fcc1488ba..15fb01264d4f 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -72,10 +72,10 @@ public function __construct() $this->initEnvValue($this->{$property}, $property, $prefix, $shortPrefix); if ($this instanceof Encryption && $property === 'key') { - if (strpos($this->{$property}, 'hex2bin:') === 0) { + if (str_starts_with($this->{$property}, 'hex2bin:')) { // Handle hex2bin prefix $this->{$property} = hex2bin(substr($this->{$property}, 8)); - } elseif (strpos($this->{$property}, 'base64:') === 0) { + } elseif (str_starts_with($this->{$property}, 'base64:')) { // Handle base64 prefix $this->{$property} = base64_decode(substr($this->{$property}, 7), true); } diff --git a/system/Config/DotEnv.php b/system/Config/DotEnv.php index 724524e2d590..4e13db863bcc 100644 --- a/system/Config/DotEnv.php +++ b/system/Config/DotEnv.php @@ -66,12 +66,12 @@ public function parse(): ?array foreach ($lines as $line) { // Is it a comment? - if (strpos(trim($line), '#') === 0) { + if (str_starts_with(trim($line), '#')) { continue; } // If there is an equal sign, then we know we are assigning a variable. - if (strpos($line, '=') !== false) { + if (str_contains($line, '=')) { [$name, $value] = $this->normaliseVariable($line); $vars[$name] = $value; $this->setVariable($name, $value); @@ -108,7 +108,7 @@ protected function setVariable(string $name, string $value = '') public function normaliseVariable(string $name, string $value = ''): array { // Split our compound string into its parts. - if (strpos($name, '=') !== false) { + if (str_contains($name, '=')) { [$name, $value] = explode('=', $name, 2); } @@ -188,7 +188,7 @@ protected function sanitizeValue(string $value): string */ protected function resolveNestedVariables(string $value): string { - if (strpos($value, '$') !== false) { + if (str_contains($value, '$')) { $value = preg_replace_callback( '/\${([a-zA-Z0-9_\.]+)}/', function ($matchedPatterns) { diff --git a/system/Config/Factories.php b/system/Config/Factories.php index c6bb25de0794..209f123031af 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -165,7 +165,7 @@ protected static function locateClass(array $options, string $name): ?string $locator = Services::locator(); // Check if the class was namespaced - if (strpos($name, '\\') !== false) { + if (str_contains($name, '\\')) { if (! $file = $locator->locateFile($name, $options['path'])) { return null; } @@ -204,10 +204,10 @@ protected static function verifyPreferApp(array $options, string $name): bool // Special case for Config since its App namespace is actually \Config if ($options['component'] === 'config') { - return strpos($name, 'Config') === 0; + return str_starts_with($name, 'Config'); } - return strpos($name, APP_NAMESPACE) === 0; + return str_starts_with($name, APP_NAMESPACE); } /** @@ -313,7 +313,7 @@ public static function injectMock(string $component, string $name, object $insta $component = strtolower($component); self::getOptions($component); - $class = get_class($instance); + $class = $instance::class; $basename = self::getBasename($name); self::$instances[$component][$class] = $instance; diff --git a/system/Cookie/Cookie.php b/system/Cookie/Cookie.php index 9362994329fd..3ce8d7a0aa46 100644 --- a/system/Cookie/Cookie.php +++ b/system/Cookie/Cookie.php @@ -19,6 +19,7 @@ use InvalidArgumentException; use LogicException; use ReturnTypeWillChange; +use Stringable; /** * A `Cookie` class represents an immutable HTTP cookie value object. @@ -38,7 +39,7 @@ * $cookie2->getName(); // prod_cookie * ``` */ -class Cookie implements ArrayAccess, CloneableCookieInterface +class Cookie implements ArrayAccess, CloneableCookieInterface, Stringable { /** * @var string @@ -156,7 +157,6 @@ public static function setDefaults($config = []) // ========================================================================= // CONSTRUCTORS // ========================================================================= - /** * Create a new Cookie instance from a `Set-Cookie` header. * @@ -177,7 +177,7 @@ public static function fromHeaderString(string $cookie, bool $raw = false) unset($part); foreach ($parts as $part) { - if (strpos($part, '=') !== false) { + if (str_contains($part, '=')) { [$attr, $val] = explode('=', $part); } else { $attr = $part; @@ -242,7 +242,6 @@ final public function __construct(string $name, string $value = '', array $optio // ========================================================================= // GETTERS // ========================================================================= - /** * {@inheritDoc} */ @@ -395,7 +394,6 @@ public function getOptions(): array // ========================================================================= // CLONING // ========================================================================= - /** * {@inheritDoc} */ @@ -560,7 +558,6 @@ public function withRaw(bool $raw = true) // ========================================================================= // ARRAY ACCESS FOR BC // ========================================================================= - /** * Whether an offset exists. * @@ -618,7 +615,6 @@ public function offsetUnset($offset): void // ========================================================================= // CONVERTERS // ========================================================================= - /** * {@inheritDoc} */ @@ -630,7 +626,7 @@ public function toHeaderString(): string /** * {@inheritDoc} */ - public function __toString() + public function __toString(): string { $cookieHeader = []; @@ -720,7 +716,6 @@ protected static function convertExpiresTimestamp($expires = 0): int // ========================================================================= // VALIDATION // ========================================================================= - /** * Validates the cookie name per RFC 2616. * @@ -747,11 +742,11 @@ protected function validateName(string $name, bool $raw): void */ protected function validatePrefix(string $prefix, bool $secure, string $path, string $domain): void { - if (strpos($prefix, '__Secure-') === 0 && ! $secure) { + if (str_starts_with($prefix, '__Secure-') && ! $secure) { throw CookieException::forInvalidSecurePrefix(); } - if (strpos($prefix, '__Host-') === 0 && (! $secure || $domain !== '' || $path !== '/')) { + if (str_starts_with($prefix, '__Host-') && (! $secure || $domain !== '' || $path !== '/')) { throw CookieException::forInvalidHostPrefix(); } } diff --git a/system/Cookie/CookieStore.php b/system/Cookie/CookieStore.php index 6bf7f003194e..5f7bbfb0f350 100644 --- a/system/Cookie/CookieStore.php +++ b/system/Cookie/CookieStore.php @@ -222,7 +222,7 @@ public function getIterator(): Traversable protected function validateCookies(array $cookies): void { foreach ($cookies as $index => $cookie) { - $type = is_object($cookie) ? get_class($cookie) : gettype($cookie); + $type = get_debug_type($cookie); if (! $cookie instanceof Cookie) { throw CookieException::forInvalidCookieInstance([static::class, Cookie::class, $type, $index]); diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 5dfc45c4833f..4dd02c04670f 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -313,7 +313,7 @@ public function __construct($tableName, ConnectionInterface $db, ?array $options $this->db = $db; // If it contains `,`, it has multiple tables - if (is_string($tableName) && strpos($tableName, ',') === false) { + if (is_string($tableName) && ! str_contains($tableName, ',')) { $this->tableName = $tableName; // @TODO remove alias if exists } else { $this->tableName = ''; @@ -511,7 +511,7 @@ protected function maxMinAvgSum(string $select = '', string $alias = '', string throw DataException::forEmptyInputGiven('Select'); } - if (strpos($select, ',') !== false) { + if (str_contains($select, ',')) { throw DataException::forInvalidArgument('column name not separated by comma'); } @@ -538,7 +538,7 @@ protected function maxMinAvgSum(string $select = '', string $alias = '', string */ protected function createAliasFromTable(string $item): string { - if (strpos($item, '.') !== false) { + if (str_contains($item, '.')) { $item = explode('.', $item); return end($item); @@ -574,7 +574,7 @@ public function from($from, bool $overwrite = false): self } foreach ((array) $from as $table) { - if (strpos($table, ',') !== false) { + if (str_contains($table, ',')) { $this->from(explode(',', $table)); } else { $table = trim($table); @@ -758,7 +758,7 @@ protected function whereHaving(string $qbKey, $key, $value = null, string $type $op = trim(current($op)); - if (substr($k, -strlen($op)) === $op) { + if (str_ends_with($k, $op)) { $k = rtrim(substr($k, 0, -strlen($op))); $op = " {$op}"; } else { @@ -2225,7 +2225,7 @@ public function insert($set = null, ?bool $escape = null) */ protected function removeAlias(string $from): string { - if (strpos($from, ' ') !== false) { + if (str_contains($from, ' ')) { // if the alias is written with the AS keyword, remove it $from = preg_replace('/\s+AS\s+/i', ' ', $from); @@ -2845,12 +2845,12 @@ protected function trackAliases($table) // Does the string contain a comma? If so, we need to separate // the string into discreet statements - if (strpos($table, ',') !== false) { + if (str_contains($table, ',')) { return $this->trackAliases(explode(',', $table)); } // if a table alias is used we can recognize it by a space - if (strpos($table, ' ') !== false) { + if (str_contains($table, ' ')) { // if the alias is written with the AS keyword, remove it $table = preg_replace('/\s+AS\s+/i', ' ', $table); @@ -2986,11 +2986,11 @@ protected function compileWhereHaving(string $qbKey): string if (! empty($matches[4])) { $protectIdentifiers = false; - if (strpos($matches[4], '.') !== false) { + if (str_contains($matches[4], '.')) { $protectIdentifiers = true; } - if (strpos($matches[4], ':') === false) { + if (! str_contains($matches[4], ':')) { $matches[4] = $this->db->protectIdentifiers(trim($matches[4]), false, $protectIdentifiers); } diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index 8e20f68a9a28..453d29dda4f1 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -1032,7 +1032,7 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro // Break the string apart if it contains periods, then insert the table prefix // in the correct location, assuming the period doesn't indicate that we're dealing // with an alias. While we're at it, we will escape the components - if (strpos($item, '.') !== false) { + if (str_contains($item, '.')) { return $this->protectDotItem($item, $alias, $protectIdentifiers, $fieldExists); } @@ -1044,11 +1044,11 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro // Is there a table prefix? If not, no need to insert it if ($this->DBPrefix !== '') { // Verify table prefix and replace if necessary - if ($this->swapPre !== '' && strpos($item, $this->swapPre) === 0) { + if ($this->swapPre !== '' && str_starts_with($item, $this->swapPre)) { $item = preg_replace('/^' . $this->swapPre . '(\S+?)/', $this->DBPrefix . '\\1', $item); } // Do we prefix an item with no segments? - elseif ($prefixSingle === true && strpos($item, $this->DBPrefix) !== 0) { + elseif ($prefixSingle === true && ! str_starts_with($item, $this->DBPrefix)) { $item = $this->DBPrefix . $item; } } @@ -1110,11 +1110,11 @@ private function protectDotItem(string $item, string $alias, bool $protectIdenti } // Verify table prefix and replace if necessary - if ($this->swapPre !== '' && strpos($parts[$i], $this->swapPre) === 0) { + if ($this->swapPre !== '' && str_starts_with($parts[$i], $this->swapPre)) { $parts[$i] = preg_replace('/^' . $this->swapPre . '(\S+?)/', $this->DBPrefix . '\\1', $parts[$i]); } // We only add the table prefix if it does not already exist - elseif (strpos($parts[$i], $this->DBPrefix) !== 0) { + elseif (! str_starts_with($parts[$i], $this->DBPrefix)) { $parts[$i] = $this->DBPrefix . $parts[$i]; } @@ -1157,7 +1157,7 @@ public function escapeIdentifiers($item) if (ctype_digit($item) || $item[0] === "'" || ($this->escapeChar !== '"' && $item[0] === '"') - || strpos($item, '(') !== false) { + || str_contains($item, '(')) { return $item; } @@ -1176,7 +1176,7 @@ public function escapeIdentifiers($item) } foreach ($this->reservedIdentifiers as $id) { - if (strpos($item, '.' . $id) !== false) { + if (str_contains($item, '.' . $id)) { return preg_replace( '/' . $this->pregEscapeChar[0] . '?([^' . $this->pregEscapeChar[1] . '\.]+)' . $this->pregEscapeChar[1] . '?\./i', $this->pregEscapeChar[2] . '$1' . $this->pregEscapeChar[3] . '.', @@ -1320,7 +1320,7 @@ public function callFunction(string $functionName, ...$params): bool { $driver = $this->getDriverFunctionPrefix(); - if (strpos($driver, $functionName) === false) { + if (! str_contains($driver, $functionName)) { $functionName = $driver . $functionName; } diff --git a/system/Database/Database.php b/system/Database/Database.php index 58d2a22bad9a..0d00ee44f825 100644 --- a/system/Database/Database.php +++ b/system/Database/Database.php @@ -45,7 +45,7 @@ public function load(array $params = [], string $alias = '') throw new InvalidArgumentException('You must supply the parameter: alias.'); } - if (! empty($params['DSN']) && strpos($params['DSN'], '://') !== false) { + if (! empty($params['DSN']) && str_contains($params['DSN'], '://')) { $params = $this->parseDSN($params); } @@ -131,7 +131,7 @@ protected function initDriver(string $driver, string $class, $argument): object { $class = $driver . '\\' . $class; - if (strpos($driver, '\\') === false) { + if (! str_contains($driver, '\\')) { $class = "CodeIgniter\\Database\\{$class}"; } diff --git a/system/Database/Forge.php b/system/Database/Forge.php index a33deb3c1c4d..fbcff0f724b6 100644 --- a/system/Database/Forge.php +++ b/system/Database/Forge.php @@ -368,7 +368,7 @@ public function addField($field) ]); $this->addKey('id', true); } else { - if (strpos($field, ' ') === false) { + if (! str_contains($field, ' ')) { throw new InvalidArgumentException('Field information is required for that operation.'); } @@ -624,7 +624,7 @@ public function dropTable(string $tableName, bool $ifExists = false, bool $casca return false; } - if ($this->db->DBPrefix && strpos($tableName, $this->db->DBPrefix) === 0) { + if ($this->db->DBPrefix && str_starts_with($tableName, $this->db->DBPrefix)) { $tableName = substr($tableName, strlen($this->db->DBPrefix)); } diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index ac2dc38e6bd6..27c1dc6673a2 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -451,7 +451,7 @@ public function findNamespaceMigrations(string $namespace): array */ protected function migrationFromFile(string $path, string $namespace) { - if (substr($path, -4) !== '.php') { + if (! str_ends_with($path, '.php')) { return false; } diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index ce6554ddc63a..7e52672c0fde 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -221,7 +221,7 @@ protected function execute(string $sql) public function parseInsertTableName(string $sql): string { $commentStrippedSql = preg_replace(['/\/\*(.|\n)*?\*\//m', '/--.+/'], '', $sql); - $isInsertQuery = strpos(strtoupper(ltrim($commentStrippedSql)), 'INSERT') === 0; + $isInsertQuery = str_starts_with(strtoupper(ltrim($commentStrippedSql)), 'INSERT'); if (! $isInsertQuery) { return ''; @@ -230,7 +230,7 @@ public function parseInsertTableName(string $sql): string preg_match('/(?is)\b(?:into)\s+("?\w+"?)/', $commentStrippedSql, $match); $tableName = $match[1] ?? ''; - return strpos($tableName, '"') === 0 ? trim($tableName, '"') : strtoupper($tableName); + return str_starts_with($tableName, '"') ? trim($tableName, '"') : strtoupper($tableName); } /** @@ -267,7 +267,7 @@ protected function _listTables(bool $prefixLimit = false, ?string $tableName = n */ protected function _listColumns(string $table = ''): string { - if (strpos($table, '.') !== false) { + if (str_contains($table, '.')) { sscanf($table, '%[^.].%s', $owner, $table); } else { $owner = $this->username; @@ -287,7 +287,7 @@ protected function _listColumns(string $table = ''): string */ protected function _fieldData(string $table): array { - if (strpos($table, '.') !== false) { + if (str_contains($table, '.')) { sscanf($table, '%[^.].%s', $owner, $table); } else { $owner = $this->username; @@ -335,7 +335,7 @@ protected function _fieldData(string $table): array */ protected function _indexData(string $table): array { - if (strpos($table, '.') !== false) { + if (str_contains($table, '.')) { sscanf($table, '%[^.].%s', $owner, $table); } else { $owner = $this->username; @@ -633,7 +633,7 @@ protected function buildDSN() return; } - $isEasyConnectableHostName = $this->hostname !== '' && strpos($this->hostname, '/') === false && strpos($this->hostname, ':') === false; + $isEasyConnectableHostName = $this->hostname !== '' && ! str_contains($this->hostname, '/') && ! str_contains($this->hostname, ':'); $easyConnectablePort = ! empty($this->port) && ctype_digit($this->port) ? ':' . $this->port : ''; $easyConnectableDatabase = $this->database !== '' ? '/' . ltrim($this->database, '/') : ''; diff --git a/system/Database/OCI8/Forge.php b/system/Database/OCI8/Forge.php index 514cf99f41f4..2562795d45d5 100644 --- a/system/Database/OCI8/Forge.php +++ b/system/Database/OCI8/Forge.php @@ -120,7 +120,7 @@ protected function _alterTable(string $alterType, string $table, $field) // If a null constraint is added to a column with a null constraint, // ORA-01451 will occur, // so add null constraint is used only when it is different from the current null constraint. - $isWantToAddNull = strpos($field[$i]['null'], ' NOT') === false; + $isWantToAddNull = ! str_contains($field[$i]['null'], ' NOT'); $currentNullAddable = $nullableMap[$field[$i]['name']]; if ($isWantToAddNull === $currentNullAddable) { @@ -181,7 +181,7 @@ protected function _processColumn(array $field): string { $constraint = ''; // @todo: can’t cover multi pattern when set type. - if ($field['type'] === 'VARCHAR2' && strpos($field['length'], "('") === 0) { + if ($field['type'] === 'VARCHAR2' && str_starts_with($field['length'], "('")) { $constraint = ' CHECK(' . $this->db->escapeIdentifiers($field['name']) . ' IN ' . $field['length'] . ')'; diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php index ab4ff2ab5d1f..983ea9afcdee 100644 --- a/system/Database/Postgre/Connection.php +++ b/system/Database/Postgre/Connection.php @@ -311,10 +311,10 @@ protected function _indexData(string $table): array $_fields = explode(',', preg_replace('/^.*\((.+?)\)$/', '$1', trim($row->indexdef))); $obj->fields = array_map(static fn ($v) => trim($v), $_fields); - if (strpos($row->indexdef, 'CREATE UNIQUE INDEX pk') === 0) { + if (str_starts_with($row->indexdef, 'CREATE UNIQUE INDEX pk')) { $obj->type = 'PRIMARY'; } else { - $obj->type = (strpos($row->indexdef, 'CREATE UNIQUE') === 0) ? 'UNIQUE' : 'INDEX'; + $obj->type = (str_starts_with($row->indexdef, 'CREATE UNIQUE')) ? 'UNIQUE' : 'INDEX'; } $retVal[$obj->name] = $obj; @@ -451,7 +451,7 @@ protected function buildDSN() } // If UNIX sockets are used, we shouldn't set a port - if (strpos($this->hostname, '/') !== false) { + if (str_contains($this->hostname, '/')) { $this->port = ''; } diff --git a/system/Database/Query.php b/system/Database/Query.php index 702575ca857c..299f84a0b523 100644 --- a/system/Database/Query.php +++ b/system/Database/Query.php @@ -11,10 +11,12 @@ namespace CodeIgniter\Database; +use Stringable; + /** * Query builder */ -class Query implements QueryInterface +class Query implements QueryInterface, Stringable { /** * The query string, as provided by the user. diff --git a/system/Database/RawSql.php b/system/Database/RawSql.php index 7ecb7fd378ae..8b39be3ab1f0 100644 --- a/system/Database/RawSql.php +++ b/system/Database/RawSql.php @@ -11,7 +11,9 @@ namespace CodeIgniter\Database; -class RawSql +use Stringable; + +class RawSql implements Stringable { /** * @var string Raw SQL string diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index 98385adca335..276397b18ae4 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -69,7 +69,7 @@ protected function _fromTables(): string $from = []; foreach ($this->QBFrom as $value) { - $from[] = strpos($value, '(SELECT') === 0 ? $value : $this->getFullName($value); + $from[] = str_starts_with($value, '(SELECT') ? $value : $this->getFullName($value); } return implode(', ', $from); @@ -280,7 +280,7 @@ private function getFullName(string $table): string { $alias = ''; - if (strpos($table, ' ') !== false) { + if (str_contains($table, ' ')) { $alias = explode(' ', $table); $table = array_shift($alias); $alias = ' ' . implode(' ', $alias); @@ -439,7 +439,7 @@ protected function maxMinAvgSum(string $select = '', string $alias = '', string throw DataException::forEmptyInputGiven('Select'); } - if (strpos($select, ',') !== false) { + if (str_contains($select, ',')) { throw DataException::forInvalidArgument('Column name not separated by comma'); } diff --git a/system/Database/SQLSRV/Connection.php b/system/Database/SQLSRV/Connection.php index b74435036c18..a2b0a5e0b5d0 100755 --- a/system/Database/SQLSRV/Connection.php +++ b/system/Database/SQLSRV/Connection.php @@ -121,7 +121,7 @@ public function connect(bool $persistent = false) unset($connection['UID'], $connection['PWD']); } - if (strpos($this->hostname, ',') === false && $this->port !== '') { + if (! str_contains($this->hostname, ',') && $this->port !== '') { $this->hostname .= ', ' . $this->port; } @@ -253,10 +253,10 @@ protected function _indexData(string $table): array $_fields = explode(',', trim($row->index_keys)); $obj->fields = array_map(static fn ($v) => trim($v), $_fields); - if (strpos($row->index_description, 'primary key located on') !== false) { + if (str_contains($row->index_description, 'primary key located on')) { $obj->type = 'PRIMARY'; } else { - $obj->type = (strpos($row->index_description, 'nonclustered, unique') !== false) ? 'UNIQUE' : 'INDEX'; + $obj->type = (str_contains($row->index_description, 'nonclustered, unique')) ? 'UNIQUE' : 'INDEX'; } $retVal[$obj->name] = $obj; diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php index ba561cb948cb..69aff4158ebe 100644 --- a/system/Database/SQLite3/Connection.php +++ b/system/Database/SQLite3/Connection.php @@ -68,7 +68,7 @@ public function connect(bool $persistent = false) } try { - if ($this->database !== ':memory:' && strpos($this->database, DIRECTORY_SEPARATOR) === false) { + if ($this->database !== ':memory:' && ! str_contains($this->database, DIRECTORY_SEPARATOR)) { $this->database = WRITEPATH . $this->database; } diff --git a/system/Database/SQLite3/Forge.php b/system/Database/SQLite3/Forge.php index b1dcb1dd599b..6487c94bbc4c 100644 --- a/system/Database/SQLite3/Forge.php +++ b/system/Database/SQLite3/Forge.php @@ -143,7 +143,7 @@ protected function _alterTable(string $alterType, string $table, $field) */ protected function _processColumn(array $field): string { - if ($field['type'] === 'TEXT' && strpos($field['length'], "('") === 0) { + if ($field['type'] === 'TEXT' && str_starts_with($field['length'], "('")) { $field['type'] .= ' CHECK(' . $this->db->escapeIdentifiers($field['name']) . ' IN ' . $field['length'] . ')'; } diff --git a/system/Database/SQLite3/Table.php b/system/Database/SQLite3/Table.php index e0367f16e49d..8c413a35aa44 100644 --- a/system/Database/SQLite3/Table.php +++ b/system/Database/SQLite3/Table.php @@ -97,7 +97,7 @@ public function fromTable(string $table) $prefix = $this->db->DBPrefix; - if (! empty($prefix) && strpos($table, $prefix) === 0) { + if (! empty($prefix) && str_starts_with($table, $prefix)) { $table = substr($table, strlen($prefix)); } diff --git a/system/Database/Seeder.php b/system/Database/Seeder.php index f51fa417b4ed..ef7f7c746157 100644 --- a/system/Database/Seeder.php +++ b/system/Database/Seeder.php @@ -124,7 +124,7 @@ public function call(string $class) throw new InvalidArgumentException('No seeder was specified.'); } - if (strpos($class, '\\') === false) { + if (! str_contains($class, '\\')) { $path = $this->seedPath . str_replace('.php', '', $class) . '.php'; if (! is_file($path)) { diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index 830bdaafa097..3253bf284a68 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -125,7 +125,7 @@ public function exceptionHandler(Throwable $exception) if (! is_cli()) { try { $this->response->setStatusCode($statusCode); - } catch (HTTPException $e) { + } catch (HTTPException) { // Workaround for invalid HTTP status code. $statusCode = 500; $this->response->setStatusCode($statusCode); @@ -135,7 +135,7 @@ public function exceptionHandler(Throwable $exception) header(sprintf('HTTP/%s %s %s', $this->request->getProtocolVersion(), $this->response->getStatusCode(), $this->response->getReasonPhrase()), true, $statusCode); } - if (strpos($this->request->getHeaderLine('accept'), 'text/html') === false) { + if (! str_contains($this->request->getHeaderLine('accept'), 'text/html')) { $this->respond(ENVIRONMENT === 'development' ? $this->collectVars($exception, $statusCode) : '', $statusCode)->send(); exit($exitCode); @@ -186,7 +186,7 @@ public function errorHandler(int $severity, string $message, ?string $file = nul private function isFakerDeprecationError(string $message, ?string $file = null, ?int $line = null) { if ( - strpos($file, VENDORPATH . 'fakerphp/faker/') !== false + str_contains($file, VENDORPATH . 'fakerphp/faker/') && $message === 'Use of "static" in callables is deprecated' ) { log_message( @@ -311,8 +311,8 @@ protected function collectVars(Throwable $exception, int $statusCode): array } return [ - 'title' => get_class($exception), - 'type' => get_class($exception), + 'title' => $exception::class, + 'type' => $exception::class, 'code' => $statusCode, 'message' => $exception->getMessage(), 'file' => $exception->getFile(), @@ -332,7 +332,7 @@ protected function maskSensitiveData(&$trace, array $keysToMask, string $path = $explode = explode('/', $keyToMask); $index = end($explode); - if (strpos(strrev($path . '/' . $index), strrev($keyToMask)) === 0) { + if (str_starts_with(strrev($path . '/' . $index), strrev($keyToMask))) { if (is_array($trace) && array_key_exists($index, $trace)) { $trace[$index] = '******************'; } elseif (is_object($trace) && property_exists($trace, $index) && isset($trace->{$index})) { @@ -413,25 +413,13 @@ private function handleDeprecationError(string $message, ?string $file = null, ? */ public static function cleanPath(string $file): string { - switch (true) { - case strpos($file, APPPATH) === 0: - $file = 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)); - break; - - case strpos($file, SYSTEMPATH) === 0: - $file = 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH)); - break; - - case strpos($file, FCPATH) === 0: - $file = 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH)); - break; - - case defined('VENDORPATH') && strpos($file, VENDORPATH) === 0: - $file = 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH)); - break; - } - - return $file; + return match (true) { + str_starts_with($file, APPPATH) => 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)), + str_starts_with($file, SYSTEMPATH) => 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH)), + str_starts_with($file, FCPATH) => 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH)), + defined('VENDORPATH') && str_starts_with($file, VENDORPATH) => 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH)), + default => $file, + }; } /** @@ -473,7 +461,7 @@ public static function highlightFile(string $file, int $lineNumber, int $lines = try { $source = file_get_contents($file); - } catch (Throwable $e) { + } catch (Throwable) { return false; } @@ -538,26 +526,13 @@ private static function renderBacktrace(array $backtrace): string $idx = $index; $idx = str_pad((string) ++$idx, 2, ' ', STR_PAD_LEFT); - $args = implode(', ', array_map(static function ($value): string { - switch (true) { - case is_object($value): - return sprintf('Object(%s)', get_class($value)); - - case is_array($value): - return $value !== [] ? '[...]' : '[]'; - - case $value === null: - return 'null'; - - case is_resource($value): - return sprintf('resource (%s)', get_resource_type($value)); - - case is_string($value): - return var_export(clean_path($value), true); - - default: - return var_export($value, true); - } + $args = implode(', ', array_map(static fn ($value): string => match (true) { + is_object($value) => sprintf('Object(%s)', $value::class), + is_array($value) => $value !== [] ? '[...]' : '[]', + $value === null => 'null', + is_resource($value) => sprintf('resource (%s)', get_resource_type($value)), + is_string($value) => var_export(clean_path($value), true), + default => var_export($value, true), }, $frame['args'])); $backtraces[] = sprintf( diff --git a/system/Debug/Toolbar.php b/system/Debug/Toolbar.php index 10dad0f4a947..b2f006efa90d 100644 --- a/system/Debug/Toolbar.php +++ b/system/Debug/Toolbar.php @@ -389,7 +389,7 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r // Non-HTML formats should not include the debugbar // then we send headers saying where to find the debug data // for this response - if ($request->isAJAX() || strpos($format, 'html') === false) { + if ($request->isAJAX() || ! str_contains($format, 'html')) { $response->setHeader('Debugbar-Time', "{$time}") ->setHeader('Debugbar-Link', site_url("?debugbar_time={$time}")); @@ -412,7 +412,7 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r . $kintScript . PHP_EOL; - if (strpos($response->getBody(), '') !== false) { + if (str_contains($response->getBody(), '')) { $response->setBody( preg_replace( '//', diff --git a/system/Debug/Toolbar/Collectors/Database.php b/system/Debug/Toolbar/Collectors/Database.php index 2991840c8de1..f794b4a8c938 100644 --- a/system/Debug/Toolbar/Collectors/Database.php +++ b/system/Debug/Toolbar/Collectors/Database.php @@ -155,7 +155,7 @@ public function display(): array } // find the first trace line that does not originate from `system/` - if ($firstNonSystemLine === '' && strpos($line['file'], 'SYSTEMPATH') === false) { + if ($firstNonSystemLine === '' && ! str_contains($line['file'], 'SYSTEMPATH')) { $firstNonSystemLine = $line['file']; } diff --git a/system/Debug/Toolbar/Collectors/Files.php b/system/Debug/Toolbar/Collectors/Files.php index ea14dcfd1ec3..bc57975e73c6 100644 --- a/system/Debug/Toolbar/Collectors/Files.php +++ b/system/Debug/Toolbar/Collectors/Files.php @@ -60,7 +60,7 @@ public function display(): array foreach ($rawFiles as $file) { $path = clean_path($file); - if (strpos($path, 'SYSTEMPATH') !== false) { + if (str_contains($path, 'SYSTEMPATH')) { $coreFiles[] = [ 'name' => basename($file), 'path' => $path, diff --git a/system/Debug/Toolbar/Collectors/Routes.php b/system/Debug/Toolbar/Collectors/Routes.php index 5ea88c0411c8..e53cc20becc3 100644 --- a/system/Debug/Toolbar/Collectors/Routes.php +++ b/system/Debug/Toolbar/Collectors/Routes.php @@ -65,7 +65,7 @@ public function display(): array } else { try { $method = new ReflectionMethod($router->controllerName(), $router->methodName()); - } catch (ReflectionException $e) { + } catch (ReflectionException) { // If we're here, the method doesn't exist // and is likely calculated in _remap. $method = new ReflectionMethod($router->controllerName(), '_remap'); diff --git a/system/Email/Email.php b/system/Email/Email.php index 6451838ab939..5b082a2b7d36 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -654,7 +654,7 @@ public function setMessage($body) public function attach($file, $disposition = '', $newname = null, $mime = '') { if ($mime === '') { - if (strpos($file, '://') === false && ! is_file($file)) { + if (! str_contains($file, '://') && ! is_file($file)) { $this->setErrorMessage(lang('Email.attachmentMissing', [$file])); return false; @@ -734,7 +734,7 @@ public function setHeader($header, $value) protected function stringToArray($email) { if (! is_array($email)) { - return (strpos($email, ',') !== false) ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY) : (array) trim($email); + return (str_contains($email, ',')) ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY) : (array) trim($email); } return $email; @@ -858,7 +858,7 @@ protected function getEncoding() } foreach ($this->baseCharsets as $charset) { - if (strpos($this->charset, $charset) === 0) { + if (str_starts_with($this->charset, $charset)) { $this->encoding = '7bit'; break; @@ -1007,7 +1007,7 @@ public function wordWrap($str, $charlim = null) $charlim = empty($this->wrapChars) ? 76 : $this->wrapChars; } - if (strpos($str, "\r") !== false) { + if (str_contains($str, "\r")) { $str = str_replace(["\r\n", "\r"], "\n", $str); } @@ -1397,7 +1397,7 @@ protected function prepQuotedPrintable($str) $str = preg_replace(['| +|', '/\x00+/'], [' ', ''], $str); // Standardize newlines - if (strpos($str, "\r") !== false) { + if (str_contains($str, "\r")) { $str = str_replace(["\r\n", "\r"], "\n", $str); } @@ -1641,7 +1641,7 @@ protected function unwrapSpecials() */ protected function removeNLCallback($matches) { - if (strpos($matches[1], "\r") !== false || strpos($matches[1], "\n") !== false) { + if (str_contains($matches[1], "\r") || str_contains($matches[1], "\n")) { $matches[1] = str_replace(["\r\n", "\r", "\n"], '', $matches[1]); } @@ -1822,7 +1822,7 @@ protected function sendWithSmtp() $this->setErrorMessage($reply); $this->SMTPEnd(); - if (strpos($reply, '250') !== 0) { + if (! str_starts_with($reply, '250')) { $this->setErrorMessage(lang('Email.SMTPError', [$reply])); return false; @@ -1985,11 +1985,11 @@ protected function SMTPAuthenticate() $this->sendData('AUTH LOGIN'); $reply = $this->getSMTPData(); - if (strpos($reply, '503') === 0) { // Already authenticated + if (str_starts_with($reply, '503')) { // Already authenticated return true; } - if (strpos($reply, '334') !== 0) { + if (! str_starts_with($reply, '334')) { $this->setErrorMessage(lang('Email.failedSMTPLogin', [$reply])); return false; @@ -1998,7 +1998,7 @@ protected function SMTPAuthenticate() $this->sendData(base64_encode($this->SMTPUser)); $reply = $this->getSMTPData(); - if (strpos($reply, '334') !== 0) { + if (! str_starts_with($reply, '334')) { $this->setErrorMessage(lang('Email.SMTPAuthUsername', [$reply])); return false; @@ -2007,7 +2007,7 @@ protected function SMTPAuthenticate() $this->sendData(base64_encode($this->SMTPPass)); $reply = $this->getSMTPData(); - if (strpos($reply, '235') !== 0) { + if (! str_starts_with($reply, '235')) { $this->setErrorMessage(lang('Email.SMTPAuthPassword', [$reply])); return false; diff --git a/system/Entity/Cast/ArrayCast.php b/system/Entity/Cast/ArrayCast.php index 315f2e582fec..d68d17c187de 100644 --- a/system/Entity/Cast/ArrayCast.php +++ b/system/Entity/Cast/ArrayCast.php @@ -21,7 +21,7 @@ class ArrayCast extends BaseCast */ public static function get($value, array $params = []): array { - if (is_string($value) && (strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) { + if (is_string($value) && (str_starts_with($value, 'a:') || str_starts_with($value, 's:'))) { $value = unserialize($value); } diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 92cedeff213f..753ebb035e50 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -156,7 +156,7 @@ public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recu { $this->_cast = $cast; - $keys = array_filter(array_keys($this->attributes), static fn ($key) => strpos($key, '_') !== 0); + $keys = array_filter(array_keys($this->attributes), static fn ($key) => ! str_starts_with($key, '_')); if (is_array($this->datamap)) { $keys = array_unique( @@ -345,7 +345,7 @@ protected function castAs($value, string $attribute, string $method = 'get') $isNullable = false; - if (strpos($type, '?') === 0) { + if (str_starts_with($type, '?')) { $isNullable = true; if ($value === null) { diff --git a/system/Entity/Exceptions/CastException.php b/system/Entity/Exceptions/CastException.php index e259447b3f76..4bcb62be8067 100644 --- a/system/Entity/Exceptions/CastException.php +++ b/system/Entity/Exceptions/CastException.php @@ -41,25 +41,14 @@ public static function forInvalidInterface(string $class) */ public static function forInvalidJsonFormat(int $error) { - switch ($error) { - case JSON_ERROR_DEPTH: - return new static(lang('Cast.jsonErrorDepth')); - - case JSON_ERROR_STATE_MISMATCH: - return new static(lang('Cast.jsonErrorStateMismatch')); - - case JSON_ERROR_CTRL_CHAR: - return new static(lang('Cast.jsonErrorCtrlChar')); - - case JSON_ERROR_SYNTAX: - return new static(lang('Cast.jsonErrorSyntax')); - - case JSON_ERROR_UTF8: - return new static(lang('Cast.jsonErrorUtf8')); - - default: - return new static(lang('Cast.jsonErrorUnknown')); - } + return match ($error) { + JSON_ERROR_DEPTH => new static(lang('Cast.jsonErrorDepth')), + JSON_ERROR_STATE_MISMATCH => new static(lang('Cast.jsonErrorStateMismatch')), + JSON_ERROR_CTRL_CHAR => new static(lang('Cast.jsonErrorCtrlChar')), + JSON_ERROR_SYNTAX => new static(lang('Cast.jsonErrorSyntax')), + JSON_ERROR_UTF8 => new static(lang('Cast.jsonErrorUtf8')), + default => new static(lang('Cast.jsonErrorUnknown')), + }; } /** diff --git a/system/Exceptions/CastException.php b/system/Exceptions/CastException.php index e1f4e1231eef..da87e3fbe379 100644 --- a/system/Exceptions/CastException.php +++ b/system/Exceptions/CastException.php @@ -29,24 +29,13 @@ public function getExitCode(): int public static function forInvalidJsonFormatException(int $error) { - switch ($error) { - case JSON_ERROR_DEPTH: - return new static(lang('Cast.jsonErrorDepth')); - - case JSON_ERROR_STATE_MISMATCH: - return new static(lang('Cast.jsonErrorStateMismatch')); - - case JSON_ERROR_CTRL_CHAR: - return new static(lang('Cast.jsonErrorCtrlChar')); - - case JSON_ERROR_SYNTAX: - return new static(lang('Cast.jsonErrorSyntax')); - - case JSON_ERROR_UTF8: - return new static(lang('Cast.jsonErrorUtf8')); - - default: - return new static(lang('Cast.jsonErrorUnknown')); - } + return match ($error) { + JSON_ERROR_DEPTH => new static(lang('Cast.jsonErrorDepth')), + JSON_ERROR_STATE_MISMATCH => new static(lang('Cast.jsonErrorStateMismatch')), + JSON_ERROR_CTRL_CHAR => new static(lang('Cast.jsonErrorCtrlChar')), + JSON_ERROR_SYNTAX => new static(lang('Cast.jsonErrorSyntax')), + JSON_ERROR_UTF8 => new static(lang('Cast.jsonErrorUtf8')), + default => new static(lang('Cast.jsonErrorUnknown')), + }; } } diff --git a/system/Exceptions/FrameworkException.php b/system/Exceptions/FrameworkException.php index 744f4f906d89..c0c65a8f08b1 100644 --- a/system/Exceptions/FrameworkException.php +++ b/system/Exceptions/FrameworkException.php @@ -40,7 +40,7 @@ public static function forCopyError(string $path) public static function forMissingExtension(string $extension) { - if (strpos($extension, 'intl') !== false) { + if (str_contains($extension, 'intl')) { // @codeCoverageIgnoreStart $message = sprintf( 'The framework needs the following extension(s) installed and loaded: %s.', diff --git a/system/Files/File.php b/system/Files/File.php index 67e098da1a93..bc9ced45c7db 100644 --- a/system/Files/File.php +++ b/system/Files/File.php @@ -72,16 +72,11 @@ public function getSize() */ public function getSizeByUnit(string $unit = 'b') { - switch (strtolower($unit)) { - case 'kb': - return number_format($this->getSize() / 1024, 3); - - case 'mb': - return number_format(($this->getSize() / 1024) / 1024, 3); - - default: - return $this->getSize(); - } + return match (strtolower($unit)) { + 'kb' => number_format($this->getSize() / 1024, 3), + 'mb' => number_format(($this->getSize() / 1024) / 1024, 3), + default => $this->getSize(), + }; } /** @@ -171,7 +166,7 @@ public function getDestination(string $destination, string $delimiter = '_', int $info = pathinfo($destination); $extension = isset($info['extension']) ? '.' . $info['extension'] : ''; - if (strpos($info['filename'], $delimiter) !== false) { + if (str_contains($info['filename'], $delimiter)) { $parts = explode($delimiter, $info['filename']); if (is_numeric(end($parts))) { diff --git a/system/Files/FileCollection.php b/system/Files/FileCollection.php index c1204ad22bc5..1f2b4ddfa44a 100644 --- a/system/Files/FileCollection.php +++ b/system/Files/FileCollection.php @@ -80,7 +80,7 @@ final protected static function filterFiles(array $files, string $directory): ar { $directory = self::resolveDirectory($directory); - return array_filter($files, static fn (string $value): bool => strpos($value, $directory) === 0); + return array_filter($files, static fn (string $value): bool => str_starts_with($value, $directory)); } /** @@ -177,7 +177,7 @@ public function add($paths, bool $recursive = true) try { // Test for a directory self::resolveDirectory($path); - } catch (FileException $e) { + } catch (FileException) { $this->addFile($path); continue; diff --git a/system/Filters/Filters.php b/system/Filters/Filters.php index 30e36a32c65f..0f748fefd444 100644 --- a/system/Filters/Filters.php +++ b/system/Filters/Filters.php @@ -166,7 +166,7 @@ public function run(string $uri, string $position = 'before') $class = new $className(); if (! $class instanceof FilterInterface) { - throw FilterException::forIncorrectInterface(get_class($class)); + throw FilterException::forIncorrectInterface($class::class); } if ($position === 'before') { @@ -323,7 +323,7 @@ public function addFilter(string $class, ?string $alias = null, string $when = ' public function enableFilter(string $name, string $when = 'before') { // Get parameters and clean name - if (strpos($name, ':') !== false) { + if (str_contains($name, ':')) { [$name, $params] = explode(':', $name); $params = explode(',', $params); diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index f8d9a7318380..284896905c62 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -308,7 +308,7 @@ protected function parseOptions(array $options) protected function prepareURL(string $url): string { // If it's a full URI, then we have nothing to do here... - if (strpos($url, '://') !== false) { + if (str_contains($url, '://')) { return $url; } @@ -374,12 +374,12 @@ public function send(string $method, string $url) // Set the string we want to break our response from $breakString = "\r\n\r\n"; - if (strpos($output, 'HTTP/1.1 100 Continue') === 0) { + if (str_starts_with($output, 'HTTP/1.1 100 Continue')) { $output = substr($output, strpos($output, $breakString) + 4); } // If request and response have Digest - if (isset($this->config['auth'][2]) && $this->config['auth'][2] === 'digest' && strpos($output, 'WWW-Authenticate: Digest') !== false) { + if (isset($this->config['auth'][2]) && $this->config['auth'][2] === 'digest' && str_contains($output, 'WWW-Authenticate: Digest')) { $output = substr($output, strpos($output, $breakString) + 4); } @@ -475,7 +475,7 @@ protected function setResponseHeaders(array $headers = []) $value = substr($header, $pos + 1); $this->response->setHeader($title, $value); - } elseif (strpos($header, 'HTTP') === 0) { + } elseif (str_starts_with($header, 'HTTP')) { preg_match('#^HTTP\/([12](?:\.[01])?) (\d+) (.+)#', $header, $matches); if (isset($matches[1])) { diff --git a/system/HTTP/ContentSecurityPolicy.php b/system/HTTP/ContentSecurityPolicy.php index 3cc9b03a58db..1290c730e71e 100644 --- a/system/HTTP/ContentSecurityPolicy.php +++ b/system/HTTP/ContentSecurityPolicy.php @@ -783,7 +783,7 @@ protected function addToHeader(string $name, $values = null) $reportOnly = $this->reportOnly; } - if (strpos($value, 'nonce-') === 0) { + if (str_starts_with($value, 'nonce-')) { $value = "'{$value}'"; } diff --git a/system/HTTP/Files/FileCollection.php b/system/HTTP/Files/FileCollection.php index 87f31ee511ea..d6972c6a3f6c 100644 --- a/system/HTTP/Files/FileCollection.php +++ b/system/HTTP/Files/FileCollection.php @@ -55,7 +55,7 @@ public function getFile(string $name) $this->populateFiles(); if ($this->hasFile($name)) { - if (strpos($name, '.') !== false) { + if (str_contains($name, '.')) { $name = explode('.', $name); $uploadedFile = $this->getValueDotNotationSyntax($name, $this->files); @@ -82,7 +82,7 @@ public function getFileMultiple(string $name) $this->populateFiles(); if ($this->hasFile($name)) { - if (strpos($name, '.') !== false) { + if (str_contains($name, '.')) { $name = explode('.', $name); $uploadedFile = $this->getValueDotNotationSyntax($name, $this->files); @@ -111,7 +111,7 @@ public function hasFile(string $fileID): bool { $this->populateFiles(); - if (strpos($fileID, '.') !== false) { + if (str_contains($fileID, '.')) { $segments = explode('.', $fileID); $el = $this->files; diff --git a/system/HTTP/Files/UploadedFile.php b/system/HTTP/Files/UploadedFile.php index a9e81cd97ee1..d5f7e5dad792 100644 --- a/system/HTTP/Files/UploadedFile.php +++ b/system/HTTP/Files/UploadedFile.php @@ -142,7 +142,7 @@ public function move(string $targetPath, ?string $name = null, bool $overwrite = try { $this->hasMoved = move_uploaded_file($this->path, $destination); - } catch (Exception $e) { + } catch (Exception) { $error = error_get_last(); $message = strip_tags($error['message'] ?? ''); diff --git a/system/HTTP/Header.php b/system/HTTP/Header.php index 1b76bd429f20..13d29496e7ca 100644 --- a/system/HTTP/Header.php +++ b/system/HTTP/Header.php @@ -11,12 +11,14 @@ namespace CodeIgniter\HTTP; +use Stringable; + /** * Class Header * * Represents a single HTTP header. */ -class Header +class Header implements Stringable { /** * The name of the header. diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 9e19563ab8b0..c2c5a6c55b1e 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -210,20 +210,11 @@ public function detectPath(string $protocol = ''): string $protocol = 'REQUEST_URI'; } - switch ($protocol) { - case 'REQUEST_URI': - $this->path = $this->parseRequestURI(); - break; - - case 'QUERY_STRING': - $this->path = $this->parseQueryString(); - break; - - case 'PATH_INFO': - default: - $this->path = $this->fetchGlobal('server', $protocol) ?? $this->parseRequestURI(); - break; - } + $this->path = match ($protocol) { + 'REQUEST_URI' => $this->parseRequestURI(), + 'QUERY_STRING' => $this->parseQueryString(), + default => $this->fetchGlobal('server', $protocol) ?? $this->parseRequestURI(), + }; return $this->path; } @@ -269,7 +260,7 @@ protected function parseRequestURI(): string // This section ensures that even on servers that require the URI to contain the query string (Nginx) a correct // URI is found, and also fixes the QUERY_STRING Server var and $_GET array. - if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0) { + if (trim($uri, '/') === '' && str_starts_with($query, '/')) { $query = explode('?', $query, 2); $uri = $query[0]; $_SERVER['QUERY_STRING'] = $query[1] ?? ''; @@ -300,7 +291,7 @@ protected function parseQueryString(): string return ''; } - if (strncmp($uri, '/', 1) === 0) { + if (str_starts_with($uri, '/')) { $uri = explode('?', $uri, 2); $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; $uri = $uri[0]; @@ -326,21 +317,13 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa $this->negotiator = Services::negotiator($this, true); } - switch (strtolower($type)) { - case 'media': - return $this->negotiator->media($supported, $strictMatch); - - case 'charset': - return $this->negotiator->charset($supported); - - case 'encoding': - return $this->negotiator->encoding($supported); - - case 'language': - return $this->negotiator->language($supported); - } - - throw HTTPException::forInvalidNegotiationType($type); + return match (strtolower($type)) { + 'media' => $this->negotiator->media($supported, $strictMatch), + 'charset' => $this->negotiator->charset($supported), + 'encoding' => $this->negotiator->encoding($supported), + 'language' => $this->negotiator->language($supported), + default => throw HTTPException::forInvalidNegotiationType($type), + }; } /** @@ -514,7 +497,7 @@ public function getDefaultLocale(): string public function getVar($index = null, $filter = null, $flags = null) { if ( - strpos($this->getHeaderLine('Content-Type'), 'application/json') !== false + str_contains($this->getHeaderLine('Content-Type'), 'application/json') && $this->body !== null ) { if ($index === null) { diff --git a/system/HTTP/RedirectResponse.php b/system/HTTP/RedirectResponse.php index 34f950de0a60..dd6fc4933698 100644 --- a/system/HTTP/RedirectResponse.php +++ b/system/HTTP/RedirectResponse.php @@ -33,7 +33,7 @@ public function to(string $uri, ?int $code = null, string $method = 'auto') { // If it appears to be a relative URL, then convert to full URL // for better security. - if (strpos($uri, 'http') !== 0) { + if (! str_starts_with($uri, 'http')) { $uri = site_url($uri); } diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 684f27726984..c0ef961ecf9c 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -90,7 +90,7 @@ public function getIPAddress(): string if ($spoof) { foreach ($proxyIPs as $proxyIP) { // Check if we have an IP address or a subnet - if (strpos($proxyIP, '/') === false) { + if (! str_contains($proxyIP, '/')) { // An IP address (and not a subnet) is specified. // We can compare right away. if ($proxyIP === $this->ipAddress) { @@ -107,7 +107,7 @@ public function getIPAddress(): string } // If the proxy entry doesn't match the IP protocol - skip it - if (strpos($proxyIP, $separator) === false) { + if (! str_contains($proxyIP, $separator)) { continue; } diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index 0ad3239e3cbd..21e3ca2fea07 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -511,7 +511,7 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null } // IIS environment likely? Use 'refresh' for better compatibility - if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false) { + if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && str_contains($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS')) { $method = 'refresh'; } @@ -521,15 +521,10 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null $code = ($_SERVER['REQUEST_METHOD'] !== 'GET') ? 303 : ($code === 302 ? 307 : $code); } - switch ($method) { - case 'refresh': - $this->setHeader('Refresh', '0;url=' . $uri); - break; - - default: - $this->setHeader('Location', $uri); - break; - } + match ($method) { + 'refresh' => $this->setHeader('Refresh', '0;url=' . $uri), + default => $this->setHeader('Location', $uri), + }; $this->setStatusCode($code); diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 690dd19454f7..2f3be5eabc24 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -13,11 +13,12 @@ use BadMethodCallException; use CodeIgniter\HTTP\Exceptions\HTTPException; +use Stringable; /** * Abstraction for a uniform resource identifier (URI). */ -class URI +class URI implements Stringable { /** * Sub-delimiters used in query strings and fragments. @@ -171,7 +172,7 @@ public static function createURIString( } if (isset($path) && $path !== '') { - $uri .= substr($uri, -1, 1) !== '/' + $uri .= ! str_ends_with($uri, '/') ? '/' . ltrim($path, '/') : ltrim($path, '/'); } @@ -227,12 +228,12 @@ public static function removeDotSegments(string $path): string $output = trim($output, '/ '); // Add leading slash if necessary - if (strpos($path, '/') === 0) { + if (str_starts_with($path, '/')) { $output = '/' . $output; } // Add trailing slash if necessary - if ($output !== '/' && substr($path, -1, 1) === '/') { + if ($output !== '/' && str_ends_with($path, '/')) { $output .= '/'; } @@ -628,14 +629,14 @@ private function changeSchemeAndPath(string $scheme, string $path): array $baseUri = new self($config->baseURL); if ( - substr($this->getScheme(), 0, 4) === 'http' + str_starts_with($this->getScheme(), 'http') && $this->getHost() === $baseUri->getHost() ) { // Check for additional segments $basePath = trim($baseUri->getPath(), '/') . '/'; $trimPath = ltrim($path, '/'); - if ($basePath !== '/' && strpos($trimPath, $basePath) !== 0) { + if ($basePath !== '/' && ! str_starts_with($trimPath, $basePath)) { $path = $basePath . $trimPath; } @@ -807,7 +808,7 @@ public function refreshPath() */ public function setQuery(string $query) { - if (strpos($query, '#') !== false) { + if (str_contains($query, '#')) { if ($this->silent) { return $this; } @@ -816,7 +817,7 @@ public function setQuery(string $query) } // Can't have leading ? - if (! empty($query) && strpos($query, '?') === 0) { + if (! empty($query) && str_starts_with($query, '?')) { $query = substr($query, 1); } @@ -928,10 +929,10 @@ protected function filterPath(?string $path = null): string $path = self::removeDotSegments($path); // Fix up some leading slash edge cases... - if (strpos($orig, './') === 0) { + if (str_starts_with($orig, './')) { $path = '/' . $path; } - if (strpos($orig, '../') === 0) { + if (str_starts_with($orig, '../')) { $path = '/' . $path; } @@ -1030,7 +1031,7 @@ public function resolveRelativeURI(string $uri) $transformed->setQuery($this->getQuery()); } } else { - if (strpos($relative->getPath(), '/') === 0) { + if (str_starts_with($relative->getPath(), '/')) { $transformed->setPath($relative->getPath()); } else { $transformed->setPath($this->mergePaths($this, $relative)); diff --git a/system/HTTP/UserAgent.php b/system/HTTP/UserAgent.php index 10088111cec1..45090910f992 100644 --- a/system/HTTP/UserAgent.php +++ b/system/HTTP/UserAgent.php @@ -12,11 +12,12 @@ namespace CodeIgniter\HTTP; use Config\UserAgents; +use Stringable; /** * Abstraction for an HTTP user agent */ -class UserAgent +class UserAgent implements Stringable { /** * Current user-agent diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index 4918e87e7bf2..81a389a42230 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -53,7 +53,7 @@ function directory_map(string $sourceDir, int $directoryDepth = 0, bool $hidden closedir($fp); return $fileData; - } catch (Throwable $e) { + } catch (Throwable) { return []; } } @@ -129,7 +129,7 @@ function write_file(string $path, string $data, string $mode = 'wb'): bool fclose($fp); return is_int($result); - } catch (Throwable $e) { + } catch (Throwable) { return false; } } @@ -178,7 +178,7 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false, } return true; - } catch (Throwable $e) { + } catch (Throwable) { return false; } } @@ -227,7 +227,7 @@ function get_filenames( } } } - } catch (Throwable $e) { + } catch (Throwable) { return []; } @@ -277,7 +277,7 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r closedir($fp); return $fileData; - } catch (Throwable $fe) { + } catch (Throwable) { return []; } } diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 57d3f38b63a4..40208ff349e4 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -30,9 +30,9 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s if (! $action) { $action = current_url(true); } // If an action is not a full URL then turn it into one - elseif (strpos($action, '://') === false) { + elseif (! str_contains($action, '://')) { // If an action has {locale} - if (strpos($action, '{locale}') !== false) { + if (str_contains($action, '{locale}')) { $action = str_replace('{locale}', Services::request()->getLocale(), $action); } @@ -59,7 +59,7 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites $before = Services::filters()->getFilters()['before']; - if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && strpos($action, base_url()) !== false && ! stripos($form, 'method="get"')) { + if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && ! stripos($form, 'method="get"')) { $form .= csrf_field($csrfId ?? null); } diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 089f199c58e9..3b48071059b8 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -108,7 +108,7 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string $img = 'baseURL, '/ ') . '/'; - } else { - $url = $request->getUri()->getBaseURL(); - } + $url = $request instanceof CLIRequest ? rtrim($config->baseURL, '/ ') . '/' : $request->getUri()->getBaseURL(); // Check for an index page if ($config->indexPage !== '') { diff --git a/system/Honeypot/Honeypot.php b/system/Honeypot/Honeypot.php index fdd8fe95168b..47ce65576a4d 100644 --- a/system/Honeypot/Honeypot.php +++ b/system/Honeypot/Honeypot.php @@ -42,7 +42,7 @@ public function __construct(HoneypotConfig $config) throw HoneypotException::forNoHiddenValue(); } - if (empty($this->config->container) || strpos($this->config->container, '{template}') === false) { + if (empty($this->config->container) || ! str_contains($this->config->container, '{template}')) { $this->config->container = '
{template}
'; } diff --git a/system/Images/Handlers/BaseHandler.php b/system/Images/Handlers/BaseHandler.php index b7d74ae36d15..8cad03eaf61a 100644 --- a/system/Images/Handlers/BaseHandler.php +++ b/system/Images/Handlers/BaseHandler.php @@ -467,31 +467,16 @@ public function reorient(bool $silent = false) { $orientation = $this->getEXIF('Orientation', $silent); - switch ($orientation) { - case 2: - return $this->flip('horizontal'); - - case 3: - return $this->rotate(180); - - case 4: - return $this->rotate(180)->flip('horizontal'); - - case 5: - return $this->rotate(270)->flip('horizontal'); - - case 6: - return $this->rotate(270); - - case 7: - return $this->rotate(90)->flip('horizontal'); - - case 8: - return $this->rotate(90); - - default: - return $this; - } + return match ($orientation) { + 2 => $this->flip('horizontal'), + 3 => $this->rotate(180), + 4 => $this->rotate(180)->flip('horizontal'), + 5 => $this->rotate(270)->flip('horizontal'), + 6 => $this->rotate(270), + 7 => $this->rotate(90)->flip('horizontal'), + 8 => $this->rotate(90), + default => $this, + }; } /** diff --git a/system/Images/Handlers/ImageMagickHandler.php b/system/Images/Handlers/ImageMagickHandler.php index b96fdc06d20d..31cd1461b350 100644 --- a/system/Images/Handlers/ImageMagickHandler.php +++ b/system/Images/Handlers/ImageMagickHandler.php @@ -439,30 +439,15 @@ public function reorient(bool $silent = false) { $orientation = $this->getEXIF('Orientation', $silent); - switch ($orientation) { - case 2: - return $this->flip('horizontal'); - - case 3: - return $this->rotate(180); - - case 4: - return $this->rotate(180)->flip('horizontal'); - - case 5: - return $this->rotate(90)->flip('horizontal'); - - case 6: - return $this->rotate(90); - - case 7: - return $this->rotate(270)->flip('horizontal'); - - case 8: - return $this->rotate(270); - - default: - return $this; - } + return match ($orientation) { + 2 => $this->flip('horizontal'), + 3 => $this->rotate(180), + 4 => $this->rotate(180)->flip('horizontal'), + 5 => $this->rotate(90)->flip('horizontal'), + 6 => $this->rotate(90), + 7 => $this->rotate(270)->flip('horizontal'), + 8 => $this->rotate(270), + default => $this, + }; } } diff --git a/system/Language/Language.php b/system/Language/Language.php index 640dcfaa552d..10771aceffe1 100644 --- a/system/Language/Language.php +++ b/system/Language/Language.php @@ -90,7 +90,7 @@ public function getLocale(): string public function getLine(string $line, array $args = []) { // if no file is given, just parse the line - if (strpos($line, '.') === false) { + if (! str_contains($line, '.')) { return $this->formatMessage($line, $args); } diff --git a/system/Log/Handlers/ChromeLoggerHandler.php b/system/Log/Handlers/ChromeLoggerHandler.php index b2c6d28fdfb4..0b04fe355f92 100644 --- a/system/Log/Handlers/ChromeLoggerHandler.php +++ b/system/Log/Handlers/ChromeLoggerHandler.php @@ -141,7 +141,7 @@ protected function format($object) // @todo Modify formatting of objects once we can view them in browser. $objectArray = (array) $object; - $objectArray['___class_name'] = get_class($object); + $objectArray['___class_name'] = $object::class; return $objectArray; } diff --git a/system/Log/Handlers/FileHandler.php b/system/Log/Handlers/FileHandler.php index e80a6121c676..3f02f6a6ac33 100644 --- a/system/Log/Handlers/FileHandler.php +++ b/system/Log/Handlers/FileHandler.php @@ -86,7 +86,7 @@ public function handle($level, $message): bool } // Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format - if (strpos($this->dateFormat, 'u') !== false) { + if (str_contains($this->dateFormat, 'u')) { $microtimeFull = microtime(true); $microtimeShort = sprintf('%06d', ($microtimeFull - floor($microtimeFull)) * 1_000_000); $date = new DateTime(date('Y-m-d H:i:s.' . $microtimeShort, (int) $microtimeFull)); diff --git a/system/Log/Logger.php b/system/Log/Logger.php index 9643a82191c6..9dab91d92b33 100644 --- a/system/Log/Logger.php +++ b/system/Log/Logger.php @@ -339,7 +339,7 @@ protected function interpolate($message, array $context = []) $replace['{env}'] = ENVIRONMENT; // Allow us to log the file/line that we are logging from - if (strpos($message, '{file}') !== false) { + if (str_contains($message, '{file}')) { [$file, $line] = $this->determineFile(); $replace['{file}'] = $file; @@ -347,7 +347,7 @@ protected function interpolate($message, array $context = []) } // Match up environment variables in {env:foo} tags. - if (strpos($message, 'env:') !== false) { + if (str_contains($message, 'env:')) { preg_match('/env:[^}]+/', $message, $matches); foreach ($matches as $str) { diff --git a/system/Model.php b/system/Model.php index 5c9ce0473a3d..aade592c2f2a 100644 --- a/system/Model.php +++ b/system/Model.php @@ -130,10 +130,8 @@ class Model extends BaseModel /** * Primary Key value when inserting and useAutoIncrement is false. - * - * @var int|string|null */ - private $tempPrimaryKeyValue; + private int|string|null $tempPrimaryKeyValue = null; /** * Builder method names that should not be used in the Model. @@ -503,7 +501,7 @@ protected function doErrors() return []; } - return [get_class($this->db) => $error['message']]; + return [$this->db::class => $error['message']]; } /** diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index 2c0179a69dc3..c3aee37af8ee 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -421,7 +421,7 @@ protected function calculateCurrentPage(string $group) try { $this->groups[$group]['currentPage'] = (int) $this->groups[$group]['currentUri'] ->setSilent(false)->getSegment($this->segment[$group]); - } catch (HTTPException $e) { + } catch (HTTPException) { $this->groups[$group]['currentPage'] = 1; } } else { diff --git a/system/Publisher/Publisher.php b/system/Publisher/Publisher.php index 88459153188f..cab713c29547 100644 --- a/system/Publisher/Publisher.php +++ b/system/Publisher/Publisher.php @@ -166,7 +166,7 @@ public function __construct(?string $source = null, ?string $destination = null) // Make sure the destination is allowed foreach (array_keys($this->restrictions) as $directory) { - if (strpos($this->destination, $directory) === 0) { + if (str_starts_with($this->destination, $directory)) { return; } } @@ -469,7 +469,7 @@ private function verifyAllowed(string $from, string $to) { // Verify this is an allowed file for its destination foreach ($this->restrictions as $directory => $pattern) { - if (strpos($to, $directory) === 0 && self::matchFiles([$to], $pattern) === []) { + if (str_starts_with($to, $directory) && self::matchFiles([$to], $pattern) === []) { throw PublisherException::forFileNotAllowed($from, $directory, $pattern); } } diff --git a/system/RESTful/BaseResource.php b/system/RESTful/BaseResource.php index 65ad21251a3d..f1d40999e8c7 100644 --- a/system/RESTful/BaseResource.php +++ b/system/RESTful/BaseResource.php @@ -55,7 +55,7 @@ public function setModel($which = null) } if (! empty($this->model) && empty($this->modelName)) { - $this->modelName = get_class($this->model); + $this->modelName = $this->model::class; } } } diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index 3c29ff8290d4..b6c22d9b3242 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -149,7 +149,7 @@ public function getRoute(string $uri): array // Ensure the controller stores the fully-qualified class name // We have to check for a length over 1, since by default it will be '\' - if (strpos($this->controller, '\\') === false && strlen($this->defaultNamespace) > 1) { + if (! str_contains($this->controller, '\\') && strlen($this->defaultNamespace) > 1) { $this->controller = '\\' . ltrim( str_replace( '/', diff --git a/system/Router/AutoRouterImproved.php b/system/Router/AutoRouterImproved.php index 39b264ecf780..9340a33186d8 100644 --- a/system/Router/AutoRouterImproved.php +++ b/system/Router/AutoRouterImproved.php @@ -177,7 +177,7 @@ public function getRoute(string $uri): array // Check parameters try { $this->checkParameters($uri); - } catch (ReflectionException $e) { + } catch (ReflectionException) { throw PageNotFoundException::forControllerNotFound($this->controller, $this->method); } @@ -228,7 +228,7 @@ private function checkRemap(): void 'AutoRouterImproved does not support `_remap()` method.' . ' Controller:' . $this->controller ); - } catch (ReflectionException $e) { + } catch (ReflectionException) { // Do nothing. } } diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 30be78a2bce5..a6c7fc53254c 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1058,8 +1058,8 @@ public function reverseRoute(string $search, ...$params) // Add the default namespace if needed. $namespace = trim($this->defaultNamespace, '\\') . '\\'; if ( - substr($search, 0, 1) !== '\\' - && substr($search, 0, strlen($namespace)) !== $namespace + ! str_starts_with($search, '\\') + && ! str_starts_with($search, $namespace) ) { $search = $namespace . $search; } @@ -1083,7 +1083,7 @@ public function reverseRoute(string $search, ...$params) // If there's any chance of a match, then it will // be with $search at the beginning of the $to string. - if (strpos($to, $search) !== 0) { + if (! str_starts_with($to, $search)) { continue; } @@ -1211,7 +1211,7 @@ protected function buildReverseRoute(string $from, array $params): string preg_match_all('/\(([^)]+)\)/', $from, $matches); if (empty($matches[0])) { - if (strpos($from, '{locale}') !== false) { + if (str_contains($from, '{locale}')) { $locale = $params[0] ?? null; } @@ -1249,7 +1249,7 @@ protected function buildReverseRoute(string $from, array $params): string */ private function replaceLocale(string $route, ?string $locale = null): string { - if (strpos($route, '{locale}') === false) { + if (! str_contains($route, '{locale}')) { return $route; } @@ -1352,7 +1352,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul // If is redirect, No processing if (! isset($options['redirect']) && is_string($to)) { // If no namespace found, add the default namespace - if (strpos($to, '\\') === false || strpos($to, '\\') > 0) { + if (! str_contains($to, '\\') || strpos($to, '\\') > 0) { $namespace = $options['namespace'] ?? $this->defaultNamespace; $to = trim($namespace, '\\') . '\\' . $to; } @@ -1473,7 +1473,7 @@ private function determineCurrentSubdomain() // on the URL else parse_url will mis-interpret // 'host' as the 'path'. $url = $this->httpHost; - if (strpos($url, 'http') !== 0) { + if (! str_starts_with($url, 'http')) { $url = 'http://' . $url; } diff --git a/system/Router/Router.php b/system/Router/Router.php index 24bcc3b0305b..5a12a680f9a8 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -165,7 +165,7 @@ public function handle(?string $uri = null) // If we cannot find a URI to match against, then // everything runs off of its default settings. if ($uri === null || $uri === '') { - return strpos($this->controller, '\\') === false + return ! str_contains($this->controller, '\\') ? $this->collection->getDefaultNamespace() . $this->controller : $this->controller; } @@ -410,7 +410,7 @@ protected function checkRoutes(string $uri): bool $matchedKey = $routeKey; // Are we dealing with a locale? - if (strpos($routeKey, '{locale}') !== false) { + if (str_contains($routeKey, '{locale}')) { $routeKey = str_replace('{locale}', '[^/]+', $routeKey); } @@ -432,7 +432,7 @@ protected function checkRoutes(string $uri): bool } // Store our locale so CodeIgniter object can // assign it to the Request. - if (strpos($matchedKey, '{locale}') !== false) { + if (str_contains($matchedKey, '{locale}')) { preg_match( '#^' . str_replace('{locale}', '(?[^/]+)', $matchedKey) . '$#u', $uri, @@ -469,13 +469,13 @@ protected function checkRoutes(string $uri): bool [$controller] = explode('::', $handler); // Checks `/` in controller name - if (strpos($controller, '/') !== false) { + if (str_contains($controller, '/')) { throw RouterException::forInvalidControllerName($handler); } - if (strpos($handler, '$') !== false && strpos($routeKey, '(') !== false) { + if (str_contains($handler, '$') && str_contains($routeKey, '(')) { // Checks dynamic controller - if (strpos($controller, '$') !== false) { + if (str_contains($controller, '$')) { throw RouterException::forDynamicController($handler); } diff --git a/system/Security/Security.php b/system/Security/Security.php index 422e589b81cd..ce1a74172d80 100644 --- a/system/Security/Security.php +++ b/system/Security/Security.php @@ -297,7 +297,7 @@ public function verify(RequestInterface $request) try { $token = ($postedToken !== null && $this->tokenRandomize) ? $this->derandomize($postedToken) : $postedToken; - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException) { $token = null; } diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index bfde56faf6ff..1d8e90e97d62 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -476,7 +476,7 @@ public function assertCloseEnoughString($expected, $actual, string $message = '' $difference = abs($expected - $actual); $this->assertLessThanOrEqual($tolerance, $difference, $message); - } catch (Exception $e) { + } catch (Exception) { return false; } } @@ -514,7 +514,7 @@ protected function getHeaderEmitted(string $header, bool $ignoreCase = false, st foreach (xdebug_get_headers() as $emittedHeader) { $found = $ignoreCase ? (stripos($emittedHeader, $header) === 0) - : (strpos($emittedHeader, $header) === 0); + : (str_starts_with($emittedHeader, $header)); if ($found) { return $emittedHeader; diff --git a/system/Test/ControllerTestTrait.php b/system/Test/ControllerTestTrait.php index 64cbcbe175c0..37e104d2ac27 100644 --- a/system/Test/ControllerTestTrait.php +++ b/system/Test/ControllerTestTrait.php @@ -201,7 +201,7 @@ public function execute(string $method, ...$params) // getStatusCode() throws for empty codes try { $response->getStatusCode(); - } catch (HTTPException $e) { + } catch (HTTPException) { // If no code has been set then assume success $response->setStatusCode(200); } diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 5ab12e44ab29..9e6b960a21e3 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -243,11 +243,11 @@ public function parseSelector(string $selector) $attr = null; // ID? - if (strpos($selector, '#') !== false) { + if (str_contains($selector, '#')) { [$tag, $id] = explode('#', $selector); } // Attribute - elseif (strpos($selector, '[') !== false && strpos($selector, ']') !== false) { + elseif (str_contains($selector, '[') && str_contains($selector, ']')) { $open = strpos($selector, '['); $close = strpos($selector, ']'); @@ -265,7 +265,7 @@ public function parseSelector(string $selector) $attr = [$name => trim($value, '] ')]; } // Class? - elseif (strpos($selector, '.') !== false) { + elseif (str_contains($selector, '.')) { [$tag, $class] = explode('.', $selector); } // Otherwise, assume the entire string is our tag diff --git a/system/Test/DatabaseTestTrait.php b/system/Test/DatabaseTestTrait.php index d7b8b02ddaa8..247cc74ee564 100644 --- a/system/Test/DatabaseTestTrait.php +++ b/system/Test/DatabaseTestTrait.php @@ -239,7 +239,7 @@ protected function clearInsertCache() */ public function loadBuilder(string $tableName) { - $builderClass = str_replace('Connection', 'Builder', get_class($this->db)); + $builderClass = str_replace('Connection', 'Builder', $this->db::class); return new $builderClass($tableName, $this->db); } diff --git a/system/Test/Fabricator.php b/system/Test/Fabricator.php index d540351e82e0..1c3fa9caa0e9 100644 --- a/system/Test/Fabricator.php +++ b/system/Test/Fabricator.php @@ -301,7 +301,7 @@ protected function guessFormatter($field): string $this->faker->getFormatter($field); return $field; - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException) { // No match, keep going } diff --git a/system/Test/FilterTestTrait.php b/system/Test/FilterTestTrait.php index 3bcd37962941..cdd7aebafc05 100644 --- a/system/Test/FilterTestTrait.php +++ b/system/Test/FilterTestTrait.php @@ -127,7 +127,7 @@ protected function getFilterCaller($filter, string $position): Closure if (is_string($filter)) { // Check for an alias (no namespace) - if (strpos($filter, '\\') === false) { + if (! str_contains($filter, '\\')) { if (! isset($this->filtersConfig->aliases[$filter])) { throw new RuntimeException("No filter found with alias '{$filter}'"); } @@ -140,7 +140,7 @@ protected function getFilterCaller($filter, string $position): Closure } if (! $filter instanceof FilterInterface) { - throw FilterException::forIncorrectInterface(get_class($filter)); + throw FilterException::forIncorrectInterface($filter::class); } $request = clone $this->request; diff --git a/system/Test/TestLogger.php b/system/Test/TestLogger.php index 989fb6ef5c13..980803668221 100644 --- a/system/Test/TestLogger.php +++ b/system/Test/TestLogger.php @@ -76,7 +76,7 @@ public static function didLog(string $level, $message, bool $useExactComparison continue; } - if (strpos($log['message'], $message) !== false) { + if (str_contains($log['message'], $message)) { return true; } } diff --git a/system/Typography/Typography.php b/system/Typography/Typography.php index 9b72c64c1ac9..f67cb200885c 100644 --- a/system/Typography/Typography.php +++ b/system/Typography/Typography.php @@ -78,7 +78,7 @@ public function autoTypography(string $str, bool $reduceLinebreaks = false): str } // Standardize Newlines to make matching easier - if (strpos($str, "\r") !== false) { + if (str_contains($str, "\r")) { $str = str_replace(["\r\n", "\r"], "\n", $str); } @@ -90,7 +90,7 @@ public function autoTypography(string $str, bool $reduceLinebreaks = false): str // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed $htmlComments = []; - if (strpos($str, '