diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 7acea4e7e82e..d4877780cf88 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -18,7 +18,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 10, + 'count' => 3, 'path' => __DIR__ . '/system/Autoloader/FileLocator.php', ]; $ignoreErrors[] = [ @@ -33,7 +33,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 10, + 'count' => 8, 'path' => __DIR__ . '/system/BaseModel.php', ]; $ignoreErrors[] = [ @@ -81,11 +81,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/CLI/CLI.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/CLI/CLI.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in &&, array given on the right side\\.$#', 'count' => 1, @@ -136,11 +131,6 @@ 'count' => 5, 'path' => __DIR__ . '/system/CLI/CLI.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/system/Cache/CacheFactory.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in &&, string given on the left side\\.$#', 'count' => 1, @@ -763,7 +753,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, + 'count' => 4, 'path' => __DIR__ . '/system/Common.php', ]; $ignoreErrors[] = [ @@ -871,14 +861,9 @@ 'count' => 5, 'path' => __DIR__ . '/system/Cookie/Cookie.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Call to function is_string\\(\\) with non\\-falsy\\-string will always evaluate to true\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Database/BaseBuilder.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 40, + 'count' => 30, 'path' => __DIR__ . '/system/Database/BaseBuilder.php', ]; $ignoreErrors[] = [ @@ -1068,7 +1053,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, + 'count' => 2, 'path' => __DIR__ . '/system/Database/Database.php', ]; $ignoreErrors[] = [ @@ -1143,7 +1128,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 6, + 'count' => 5, 'path' => __DIR__ . '/system/Database/MigrationRunner.php', ]; $ignoreErrors[] = [ @@ -1203,7 +1188,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 12, + 'count' => 11, 'path' => __DIR__ . '/system/Database/MySQLi/Connection.php', ]; $ignoreErrors[] = [ @@ -1268,7 +1253,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, + 'count' => 3, 'path' => __DIR__ . '/system/Database/OCI8/Builder.php', ]; $ignoreErrors[] = [ @@ -1368,7 +1353,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, + 'count' => 7, 'path' => __DIR__ . '/system/Database/Postgre/Builder.php', ]; $ignoreErrors[] = [ @@ -1533,7 +1518,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 10, + 'count' => 9, 'path' => __DIR__ . '/system/Database/SQLSRV/Builder.php', ]; $ignoreErrors[] = [ @@ -1558,7 +1543,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, + 'count' => 6, 'path' => __DIR__ . '/system/Database/SQLSRV/Connection.php', ]; $ignoreErrors[] = [ @@ -1721,11 +1706,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Database/SQLite3/Utils.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Database/Seeder.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Database\\\\Seeder\\:\\:call\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -1738,17 +1718,17 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/system/Debug/BaseExceptionHandler.php', ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/system/Debug/ExceptionHandler.php', ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, + 'count' => 1, 'path' => __DIR__ . '/system/Debug/Exceptions.php', ]; $ignoreErrors[] = [ @@ -1798,7 +1778,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 14, + 'count' => 12, 'path' => __DIR__ . '/system/Email/Email.php', ]; $ignoreErrors[] = [ @@ -1931,11 +1911,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Filters/Filters.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/HTTP/CLIRequest.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', 'count' => 10, @@ -1953,7 +1928,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 7, + 'count' => 6, 'path' => __DIR__ . '/system/HTTP/ContentSecurityPolicy.php', ]; $ignoreErrors[] = [ @@ -2068,7 +2043,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, + 'count' => 2, 'path' => __DIR__ . '/system/HTTP/IncomingRequest.php', ]; $ignoreErrors[] = [ @@ -2101,11 +2076,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/HTTP/Negotiate.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/system/HTTP/Negotiate.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, int\\|null given\\.$#', 'count' => 1, @@ -2158,7 +2128,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, + 'count' => 2, 'path' => __DIR__ . '/system/HTTP/Response.php', ]; $ignoreErrors[] = [ @@ -2193,7 +2163,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 15, + 'count' => 10, 'path' => __DIR__ . '/system/HTTP/URI.php', ]; $ignoreErrors[] = [ @@ -2228,7 +2198,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, + 'count' => 2, 'path' => __DIR__ . '/system/HTTP/UserAgent.php', ]; $ignoreErrors[] = [ @@ -2236,11 +2206,6 @@ 'count' => 4, 'path' => __DIR__ . '/system/HTTP/UserAgent.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Helpers/date_helper.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 1024\\> given\\.$#', 'count' => 2, @@ -2341,21 +2306,11 @@ 'count' => 1, 'path' => __DIR__ . '/system/Helpers/form_helper.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, - 'path' => __DIR__ . '/system/Helpers/html_helper.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#', 'count' => 1, 'path' => __DIR__ . '/system/Helpers/html_helper.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/system/Helpers/number_helper.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', 'count' => 2, @@ -2393,7 +2348,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/system/Honeypot/Honeypot.php', ]; $ignoreErrors[] = [ @@ -2401,11 +2356,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/HotReloader/HotReloader.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/I18n/Time.php', -]; $ignoreErrors[] = [ 'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\Time\\) of method CodeIgniter\\\\I18n\\\\Time\\:\\:setTimestamp\\(\\) should be covariant with return type \\(static\\(DateTimeImmutable\\)\\) of method DateTimeImmutable\\:\\:setTimestamp\\(\\)$#', 'count' => 1, @@ -2421,11 +2371,6 @@ 'count' => 4, 'path' => __DIR__ . '/system/I18n/Time.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/I18n/TimeLegacy.php', -]; $ignoreErrors[] = [ 'message' => '#^Return type \\(CodeIgniter\\\\I18n\\\\TimeLegacy\\) of method CodeIgniter\\\\I18n\\\\TimeLegacy\\:\\:setTimestamp\\(\\) should be covariant with return type \\(static\\(DateTime\\)\\) of method DateTime\\:\\:setTimestamp\\(\\)$#', 'count' => 1, @@ -2473,7 +2418,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 10, + 'count' => 9, 'path' => __DIR__ . '/system/Images/Handlers/ImageMagickHandler.php', ]; $ignoreErrors[] = [ @@ -2523,7 +2468,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 16, + 'count' => 15, 'path' => __DIR__ . '/system/Model.php', ]; $ignoreErrors[] = [ @@ -2563,7 +2508,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, + 'count' => 1, 'path' => __DIR__ . '/system/Pager/Pager.php', ]; $ignoreErrors[] = [ @@ -2603,14 +2548,9 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/system/RESTful/ResourceController.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/Router/AutoRouter.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#', 'count' => 1, @@ -2638,7 +2578,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, + 'count' => 6, 'path' => __DIR__ . '/system/Router/RouteCollection.php', ]; $ignoreErrors[] = [ @@ -2798,7 +2738,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, + 'count' => 3, 'path' => __DIR__ . '/system/Router/Router.php', ]; $ignoreErrors[] = [ @@ -2923,7 +2863,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/system/Session/Handlers/MemcachedHandler.php', ]; $ignoreErrors[] = [ @@ -2933,7 +2873,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, + 'count' => 4, 'path' => __DIR__ . '/system/Session/Handlers/RedisHandler.php', ]; $ignoreErrors[] = [ @@ -2948,7 +2888,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 12, + 'count' => 9, 'path' => __DIR__ . '/system/Session/Session.php', ]; $ignoreErrors[] = [ @@ -3278,7 +3218,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 10, + 'count' => 5, 'path' => __DIR__ . '/system/Test/FeatureTestCase.php', ]; $ignoreErrors[] = [ @@ -3563,7 +3503,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, + 'count' => 1, 'path' => __DIR__ . '/system/Test/Mock/MockResourcePresenter.php', ]; $ignoreErrors[] = [ @@ -3816,11 +3756,6 @@ 'count' => 6, 'path' => __DIR__ . '/system/Validation/FileRules.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, - 'path' => __DIR__ . '/system/Validation/FormatRules.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', 'count' => 8, @@ -3833,7 +3768,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, + 'count' => 3, 'path' => __DIR__ . '/system/Validation/Validation.php', ]; $ignoreErrors[] = [ @@ -3863,7 +3798,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 6, + 'count' => 4, 'path' => __DIR__ . '/system/View/Parser.php', ]; $ignoreErrors[] = [ diff --git a/rector.php b/rector.php index 65f5b8fcbec9..6d69a0fba2fd 100644 --- a/rector.php +++ b/rector.php @@ -23,6 +23,7 @@ use Rector\CodeQuality\Rector\If_\ShortenElseIfRector; use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector; use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector; +use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector; use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector; use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector; use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector; @@ -43,7 +44,9 @@ use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; +use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector; +use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector; use Utils\Rector\PassStrictParameterToFunctionParameterRector; use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector; use Utils\Rector\UnderscoreToCamelCaseVariableNameRector; @@ -139,6 +142,9 @@ $rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class); $rectorConfig->rule(SimplifyEmptyArrayCheckRector::class); $rectorConfig->rule(SimplifyEmptyCheckOnEmptyArrayRector::class); + $rectorConfig->rule(TernaryEmptyArrayArrayDimFetchToCoalesceRector::class); + $rectorConfig->rule(EmptyOnNullableObjectToInstanceOfRector::class); + $rectorConfig->rule(DisallowedEmptyRuleFixerRector::class); $rectorConfig->rule(StringClassNameToClassConstantRector::class); $rectorConfig->rule(PrivatizeFinalClassPropertyRector::class); $rectorConfig->rule(CompleteDynamicPropertiesRector::class); diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 5a350007f3fa..d5b198ae93fb 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -94,7 +94,7 @@ protected function respond($data = null, ?int $status = null, string $message = $output = null; $this->format($data); } else { - $status = empty($status) ? 200 : $status; + $status ??= 200; $output = $this->format($data); } diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index e9ce492c54d6..11aa56edc4f7 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -35,14 +35,14 @@ public function __construct(Autoloader $autoloader) * Attempts to locate a file by examining the name for a namespace * and looking through the PSR-4 namespaced files that we know about. * - * @param string $file The relative file path or namespaced file to - * locate. If not namespaced, search in the app - * folder. - * @param string|null $folder The folder within the namespace that we should - * look for the file. If $file does not contain - * this value, it will be appended to the namespace - * folder. - * @param string $ext The file extension the file should have. + * @param string $file The relative file path or namespaced file to + * locate. If not namespaced, search in the app + * folder. + * @param non-empty-string|null $folder The folder within the namespace that we should + * look for the file. If $file does not contain + * this value, it will be appended to the namespace + * folder. + * @param string $ext The file extension the file should have. * * @return false|string The path to the file, or false if not found. */ @@ -51,7 +51,7 @@ 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 ($folder !== null && strpos($file, $folder) === 0) { $file = substr($file, strlen($folder . '/')); } @@ -101,7 +101,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 ($folder !== null && strpos($path . $filename, '/' . $folder . '/') === false) { $path .= trim($folder, '/') . '/'; } @@ -154,7 +154,7 @@ public function getClassname(string $file): string } } - if (empty($className)) { + if ($className === '') { return ''; } @@ -305,7 +305,7 @@ public function findQualifiedNameFromPath(string $path) */ public function listFiles(string $path): array { - if (empty($path)) { + if ($path === '') { return []; } @@ -338,7 +338,7 @@ public function listFiles(string $path): array */ public function listNamespaceFiles(string $prefix, string $path): array { - if (empty($path) || empty($prefix)) { + if ($path === '' || ($prefix === '')) { return []; } @@ -368,11 +368,13 @@ public function listNamespaceFiles(string $prefix, string $path): array * Checks the app folder to see if the file can be found. * Only for use with filenames that DO NOT include namespacing. * + * @param non-empty-string|null $folder + * * @return false|string The path to the file, or false if not found. */ protected function legacyLocate(string $file, ?string $folder = null) { - $path = APPPATH . (empty($folder) ? $file : $folder . '/' . $file); + $path = APPPATH . ($folder === null ? $file : $folder . '/' . $file); $path = realpath($path) ?: $path; if (is_file($path)) { diff --git a/system/BaseModel.php b/system/BaseModel.php index fcda6b134498..1b669a30d2c2 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1579,7 +1579,7 @@ public function getValidationMessages(): array */ protected function cleanValidationRules(array $rules, ?array $row = null): array { - if (empty($row)) { + if ($row === null || $row === []) { return []; } @@ -1795,7 +1795,7 @@ protected function transformDataToArray($row, string $type): array } // If it's still empty here, means $row is no change or is empty object - if (! $this->allowEmptyInserts && empty($row)) { + if (! $this->allowEmptyInserts && ($row === null || $row === [])) { throw DataException::forEmptyDataset($type); } diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index 64b576735486..9a50e4038184 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -849,7 +849,7 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10) */ public static function wrap(?string $string = null, int $max = 0, int $padLeft = 0): string { - if (empty($string)) { + if ($string === null || $string === '') { return ''; } diff --git a/system/Cache/CacheFactory.php b/system/Cache/CacheFactory.php index 95e07ef813c8..6402b002866d 100644 --- a/system/Cache/CacheFactory.php +++ b/system/Cache/CacheFactory.php @@ -40,6 +40,9 @@ class CacheFactory /** * Attempts to create the desired cache handler, based upon the * + * @param non-empty-string|null $handler + * @param non-empty-string|null $backup + * * @return CacheInterface */ public static function getHandler(Cache $config, ?string $handler = null, ?string $backup = null) @@ -52,8 +55,8 @@ public static function getHandler(Cache $config, ?string $handler = null, ?strin throw CacheException::forNoBackup(); } - $handler = ! empty($handler) ? $handler : $config->handler; - $backup = ! empty($backup) ? $backup : $config->backupHandler; + $handler ??= $config->handler; + $backup ??= $config->backupHandler; if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers)) { throw CacheException::forHandlerNotFound(); diff --git a/system/Common.php b/system/Common.php index 56e1c07b6e3e..690fa3fc5b6a 100644 --- a/system/Common.php +++ b/system/Common.php @@ -288,20 +288,24 @@ function csrf_hash(): string if (! function_exists('csrf_field')) { /** * Generates a hidden input field for use within manually generated forms. + * + * @param non-empty-string|null $id */ function csrf_field(?string $id = null): string { - return ''; + return ''; } } if (! function_exists('csrf_meta')) { /** * Generates a meta tag for use within javascript calls. + * + * @param non-empty-string|null $id */ function csrf_meta(?string $id = null): string { - return ''; + return ''; } } @@ -850,13 +854,13 @@ function old(string $key, $default = null, $escape = 'html') * * If more control is needed, you must use $response->redirect explicitly. * - * @param string|null $route Route name or Controller::method + * @param non-empty-string|null $route Route name or Controller::method */ function redirect(?string $route = null): RedirectResponse { $response = Services::redirectresponse(null, true); - if (! empty($route)) { + if ($route !== null) { return $response->route($route); } @@ -1121,15 +1125,17 @@ function stringify_attributes($attributes, bool $js = false): string * returns its return value if any. * Otherwise will start or stop the timer intelligently. * + * @param non-empty-string|null $name * @param (callable(): mixed)|null $callable * - * @return Timer + * @return mixed|Timer + * @phpstan-return ($name is null ? Timer : ($callable is (callable(): mixed) ? mixed : Timer)) */ function timer(?string $name = null, ?callable $callable = null) { $timer = Services::timer(); - if (empty($name)) { + if ($name === null) { return $timer; } diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 6e3829fa079b..9d43e5577943 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -318,7 +318,7 @@ public function __construct($tableName, ConnectionInterface $db, ?array $options $this->from($tableName); - if (! empty($options)) { + if ($options !== null && $options !== []) { foreach ($options as $key => $value) { if (property_exists($this, $key)) { $this->{$key} = $value; @@ -920,6 +920,7 @@ public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape * @used-by whereNotIn() * @used-by orWhereNotIn() * + * @param non-empty-string|null $key * @param array|BaseBuilder|Closure|null $values The values searched on, or anonymous function with subquery * * @return $this @@ -928,7 +929,7 @@ public function orHavingNotIn(?string $key = null, $values = null, ?bool $escape */ protected function _whereIn(?string $key = null, $values = null, bool $not = false, string $type = 'AND ', ?bool $escape = null, string $clause = 'QBWhere') { - if (empty($key) || ! is_string($key)) { + if ($key === null || $key === '') { throw new InvalidArgumentException(sprintf('%s() expects $key to be a non-empty string', debug_backtrace(0, 2)[1]['function'])); } @@ -1434,7 +1435,7 @@ public function orHaving($key, $value = null, ?bool $escape = null) public function orderBy(string $orderBy, string $direction = '', ?bool $escape = null) { $qbOrderBy = []; - if (empty($orderBy)) { + if ($orderBy === '') { return $this; } @@ -1490,7 +1491,7 @@ public function limit(?int $value = null, ?int $offset = 0) $this->QBLimit = $value; } - if (! empty($offset)) { + if ($offset !== null && $offset !== 0) { $this->QBOffset = $offset; } @@ -1504,8 +1505,8 @@ public function limit(?int $value = null, ?int $offset = 0) */ public function offset(int $offset) { - if (! empty($offset)) { - $this->QBOffset = (int) $offset; + if ($offset !== 0) { + $this->QBOffset = $offset; } return $this; @@ -1736,7 +1737,7 @@ public function getWhere($where = null, ?int $limit = null, ?int $offset = 0, bo $this->where($where); } - if (! empty($limit)) { + if ($limit !== null && $limit !== 0) { $this->limit($limit, $offset); } @@ -2452,7 +2453,7 @@ public function update($set = null, $where = null, ?int $limit = null): bool $this->where($where); } - if (! empty($limit)) { + if ($limit !== null && $limit !== 0) { if (! $this->canLimitWhereUpdates) { throw new DatabaseException('This driver does not allow LIMITs on UPDATE queries using WHERE.'); } @@ -2762,7 +2763,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true) $sql = $this->_delete($this->removeAlias($table)); - if (! empty($limit)) { + if ($limit !== null && $limit !== 0) { $this->QBLimit = $limit; } @@ -3172,7 +3173,7 @@ protected function compileGroupBy(): string */ protected function compileOrderBy(): string { - if (is_array($this->QBOrderBy) && ! empty($this->QBOrderBy)) { + if (is_array($this->QBOrderBy) && $this->QBOrderBy !== []) { foreach ($this->QBOrderBy as &$orderBy) { if ($orderBy['escape'] !== false && ! $this->isLiteral($orderBy['field'])) { $orderBy['field'] = $this->db->protectIdentifiers($orderBy['field']); @@ -3265,7 +3266,7 @@ protected function isLiteral(string $str): bool { $str = trim($str); - if (empty($str) + if ($str === '' || ctype_digit($str) || (string) (float) $str === $str || in_array(strtoupper($str), ['TRUE', 'FALSE'], true) diff --git a/system/Database/Database.php b/system/Database/Database.php index 2818fe220928..58a63e152fba 100644 --- a/system/Database/Database.php +++ b/system/Database/Database.php @@ -104,7 +104,7 @@ protected function parseDSN(array $params): array 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : '', ]; - if (! empty($dsn['query'])) { + if (isset($dsn['query']) && ($dsn['query'] !== '')) { parse_str($dsn['query'], $extra); foreach ($extra as $key => $val) { diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index af8f191c276c..435c11852ae0 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -639,7 +639,7 @@ public function getHistory(string $group = 'default'): array $builder = $this->db->table($this->table); // If group was specified then use it - if (! empty($group)) { + if ($group !== '') { $builder->where('group', $group); } diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php index 45b306ce2a44..3139185f1bc4 100644 --- a/system/Database/MySQLi/Connection.php +++ b/system/Database/MySQLi/Connection.php @@ -150,7 +150,7 @@ public function connect(bool $persistent = false) $ssl['cipher'] = $this->encrypt['ssl_cipher']; } - if (! empty($ssl)) { + if ($ssl !== []) { if (isset($this->encrypt['ssl_verify'])) { if ($this->encrypt['ssl_verify']) { if (defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT')) { diff --git a/system/Database/OCI8/Builder.php b/system/Database/OCI8/Builder.php index bccb0bc54716..f5527ce745c6 100644 --- a/system/Database/OCI8/Builder.php +++ b/system/Database/OCI8/Builder.php @@ -166,7 +166,7 @@ protected function _truncate(string $table): string */ public function delete($where = '', ?int $limit = null, bool $resetData = true) { - if (! empty($limit)) { + if ($limit !== null && $limit !== 0) { $this->QBLimit = $limit; } diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index e9763089a90f..3e3ed68a9356 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -229,7 +229,7 @@ protected function _insertBatch(string $table, array $keys, array $values): stri */ public function delete($where = '', ?int $limit = null, bool $resetData = true) { - if (! empty($limit) || ! empty($this->QBLimit)) { + if ($limit !== null && $limit !== 0 || ! empty($this->QBLimit)) { throw new DatabaseException('PostgreSQL does not allow LIMITs on DELETE queries.'); } diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index b4e77f1d2cf8..c86e82633091 100755 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -522,7 +522,7 @@ public function delete($where = '', ?int $limit = null, bool $resetData = true) return false; // @codeCoverageIgnore } - if (! empty($limit)) { + if ($limit !== null && $limit !== 0) { $this->QBLimit = $limit; } diff --git a/system/Database/SQLSRV/Connection.php b/system/Database/SQLSRV/Connection.php index f1a9fb0b433a..bd9a22e94975 100755 --- a/system/Database/SQLSRV/Connection.php +++ b/system/Database/SQLSRV/Connection.php @@ -444,7 +444,7 @@ public function affectedRows(): int */ public function setDatabase(?string $databaseName = null) { - if (empty($databaseName)) { + if ($databaseName === null || $databaseName === '') { $databaseName = $this->database; } @@ -538,7 +538,7 @@ public function getVersion(): string return $this->dataCache['version']; } - if (! $this->connID || empty($info = sqlsrv_server_info($this->connID))) { + if (! $this->connID || ($info = sqlsrv_server_info($this->connID)) === []) { $this->initialize(); } diff --git a/system/Database/Seeder.php b/system/Database/Seeder.php index 749f6f34d5c7..c0f267d2fee0 100644 --- a/system/Database/Seeder.php +++ b/system/Database/Seeder.php @@ -78,7 +78,7 @@ public function __construct(Database $config, ?BaseConnection $db = null) { $this->seedPath = $config->filesPath ?? APPPATH . 'Database/'; - if (empty($this->seedPath)) { + if ($this->seedPath === '') { throw new InvalidArgumentException('Invalid filesPath set in the Config\Database.'); } diff --git a/system/Debug/BaseExceptionHandler.php b/system/Debug/BaseExceptionHandler.php index d4f045797366..33dd126ff1ec 100644 --- a/system/Debug/BaseExceptionHandler.php +++ b/system/Debug/BaseExceptionHandler.php @@ -161,7 +161,7 @@ protected static function describeMemory(int $bytes): string */ protected static function highlightFile(string $file, int $lineNumber, int $lines = 15) { - if (empty($file) || ! is_readable($file)) { + if ($file === '' || ! is_readable($file)) { return false; } diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index 145474c6c633..1f5a5b52f03b 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -522,7 +522,7 @@ public static function describeMemory(int $bytes): string */ public static function highlightFile(string $file, int $lineNumber, int $lines = 15) { - if (empty($file) || ! is_readable($file)) { + if ($file === '' || ! is_readable($file)) { return false; } diff --git a/system/Debug/Timer.php b/system/Debug/Timer.php index 103094b0a641..45c0bf865c8e 100644 --- a/system/Debug/Timer.php +++ b/system/Debug/Timer.php @@ -136,7 +136,7 @@ public function has(string $name): bool * @param string $name The name of the timer * @param callable(): mixed $callable callable to be executed * - * @return array|bool|float|int|object|resource|string|null + * @return mixed */ public function record(string $name, callable $callable) { diff --git a/system/Email/Email.php b/system/Email/Email.php index 6a7510b1d2a9..d18b40ff7cef 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -1236,13 +1236,13 @@ protected function buildMessage() . $this->prepQuotedPrintable($this->body) . $this->newline . $this->newline . '--' . $altBoundary . '--' . $this->newline . $this->newline; - if (! empty($relBoundary)) { + if (isset($relBoundary)) { $body .= $this->newline . $this->newline; $this->appendAttachments($body, $relBoundary, 'related'); } // multipart/mixed attachments - if (! empty($atcBoundary)) { + if (isset($atcBoundary)) { $body .= $this->newline . $this->newline; $this->appendAttachments($body, $atcBoundary, 'mixed'); } diff --git a/system/HTTP/CLIRequest.php b/system/HTTP/CLIRequest.php index 1d15619c8016..01823dbfc4c5 100644 --- a/system/HTTP/CLIRequest.php +++ b/system/HTTP/CLIRequest.php @@ -95,7 +95,7 @@ public function getPath(): string { $path = implode('/', $this->segments); - return empty($path) ? '' : $path; + return ($path === '') ? '' : $path; } /** diff --git a/system/HTTP/ContentSecurityPolicy.php b/system/HTTP/ContentSecurityPolicy.php index 660d26b6ab88..f4970bf7c544 100644 --- a/system/HTTP/ContentSecurityPolicy.php +++ b/system/HTTP/ContentSecurityPolicy.php @@ -790,7 +790,7 @@ protected function addToHeader(string $name, $values = null) $reportSources = []; foreach ($values as $value => $reportOnly) { - if (is_numeric($value) && is_string($reportOnly) && ! empty($reportOnly)) { + if (is_numeric($value) && is_string($reportOnly) && ($reportOnly !== '')) { $value = $reportOnly; $reportOnly = $this->reportOnly; } diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 48b9f3f61be4..8bc246f7956d 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -152,7 +152,7 @@ class IncomingRequest extends Request */ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?UserAgent $userAgent = null) { - if (empty($uri) || empty($userAgent)) { + if (! $uri instanceof URI || ! $userAgent instanceof UserAgent) { throw new InvalidArgumentException('You must supply the parameters: uri, userAgent.'); } @@ -256,7 +256,7 @@ protected function detectURI(string $protocol, string $baseURL) */ public function detectPath(string $protocol = ''): string { - if (empty($protocol)) { + if ($protocol === '') { $protocol = 'REQUEST_URI'; } diff --git a/system/HTTP/Negotiate.php b/system/HTTP/Negotiate.php index a9297669146a..9c903908695f 100644 --- a/system/HTTP/Negotiate.php +++ b/system/HTTP/Negotiate.php @@ -93,7 +93,7 @@ public function charset(array $supported): string // If no charset is shown as a match, ignore the directive // as allowed by the RFC, and tell it a default value. - if (empty($match)) { + if ($match === '') { return 'utf-8'; } @@ -158,7 +158,7 @@ protected function getBestMatch( throw HTTPException::forEmptySupportedNegotiations(); } - if (empty($header)) { + if ($header === null || $header === '') { return $strictMatch ? '' : $supported[0]; } diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index 90637fdbd7cf..b385bfea45de 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -156,13 +156,13 @@ public function setStatusCode(int $code, string $reason = '') } // Unknown and no message? - if (! array_key_exists($code, static::$statusCodes) && empty($reason)) { + if (! array_key_exists($code, static::$statusCodes) && ($reason === '')) { throw HTTPException::forUnkownStatusCode($code); } $this->statusCode = $code; - $this->reason = ! empty($reason) ? $reason : static::$statusCodes[$code]; + $this->reason = ($reason !== '') ? $reason : static::$statusCodes[$code]; return $this; } @@ -226,7 +226,7 @@ public function setLink(PagerInterface $pager) public function setContentType(string $mime, string $charset = 'UTF-8') { // add charset attribute if not already there and provided as parm - if ((strpos($mime, 'charset=') < 1) && ! empty($charset)) { + if ((strpos($mime, 'charset=') < 1) && ($charset !== '')) { $mime .= '; charset=' . $charset; } diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 608f8b75d5ba..5bec38013bf2 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -163,11 +163,11 @@ public static function createURIString( ?string $fragment = null ): string { $uri = ''; - if (! empty($scheme)) { + if ($scheme !== null && $scheme !== '') { $uri .= $scheme . '://'; } - if (! empty($authority)) { + if ($authority !== null && $authority !== '') { $uri .= $authority; } @@ -886,7 +886,7 @@ public function setQuery(string $query) } // Can't have leading ? - if (! empty($query) && strpos($query, '?') === 0) { + if ($query !== '' && strpos($query, '?') === 0) { $query = substr($query, 1); } @@ -1097,7 +1097,7 @@ public function resolveRelativeURI(string $uri) $transformed = clone $relative; // 5.2.2 Transform References in a non-strict method (no scheme) - if (! empty($relative->getAuthority())) { + if ($relative->getAuthority() !== '') { $transformed ->setAuthority($relative->getAuthority()) ->setPath($relative->getPath()) @@ -1139,7 +1139,7 @@ public function resolveRelativeURI(string $uri) */ protected function mergePaths(self $base, self $reference): string { - if (! empty($base->getAuthority()) && $base->getPath() === '') { + if ($base->getAuthority() !== '' && $base->getPath() === '') { return '/' . ltrim($reference->getPath(), '/ '); } diff --git a/system/HTTP/UserAgent.php b/system/HTTP/UserAgent.php index 77e7cb4392fc..2294b02a2b48 100644 --- a/system/HTTP/UserAgent.php +++ b/system/HTTP/UserAgent.php @@ -260,7 +260,7 @@ public function parse(string $string) // Set the new user-agent string and parse it, unless empty $this->agent = $string; - if (! empty($string)) { + if ($string !== '') { $this->compileData(); } } diff --git a/system/Helpers/date_helper.php b/system/Helpers/date_helper.php index ba9c9a249455..cf1dc33a74ea 100644 --- a/system/Helpers/date_helper.php +++ b/system/Helpers/date_helper.php @@ -20,11 +20,13 @@ * Returns Time::now()->getTimestamp() based on the timezone parameter or on the * app_timezone() setting * + * @param non-empty-string|null $timezone + * * @throws Exception */ function now(?string $timezone = null): int { - $timezone = empty($timezone) ? app_timezone() : $timezone; + $timezone = ($timezone === null || $timezone === '') ? app_timezone() : $timezone; if ($timezone === 'local' || $timezone === date_default_timezone_get()) { return Time::now()->getTimestamp(); diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 88fb339832d1..075dcd510b43 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -313,7 +313,7 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a $video .= _space_indent() . $track . "\n"; } - if (! empty($unsupportedMessage)) { + if ($unsupportedMessage !== '') { $video .= _space_indent() . $unsupportedMessage . "\n"; @@ -359,7 +359,7 @@ function audio($src, string $unsupportedMessage = '', string $attributes = '', a $audio .= "\n" . _space_indent() . $track; } - if (! empty($unsupportedMessage)) { + if ($unsupportedMessage !== '') { $audio .= "\n" . _space_indent() . $unsupportedMessage . "\n"; } @@ -377,7 +377,7 @@ function _media(string $name, array $types = [], string $unsupportedMessage = '' { $media = '<' . $name; - if (empty($attributes)) { + if ($attributes === '') { $media .= '>'; } else { $media .= ' ' . $attributes . '>'; @@ -393,7 +393,7 @@ function _media(string $name, array $types = [], string $unsupportedMessage = '' $media .= _space_indent() . $track . "\n"; } - if (! empty($unsupportedMessage)) { + if ($unsupportedMessage !== '') { $media .= _space_indent() . $unsupportedMessage . "\n"; } @@ -421,7 +421,7 @@ function source(string $src, string $type = 'unknown', string $attributes = '', $source = 'config->container) || strpos($this->config->container, '{template}') === false) { + if ($this->config->container === '' || strpos($this->config->container, '{template}') === false) { $this->config->container = '
{template}
'; } diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index da2bf046517b..93f03d8a84bd 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -212,14 +212,22 @@ public static function createFromTime(?int $hour = null, ?int $minutes = null, ? * * @throws Exception */ - public static function create(?int $year = null, ?int $month = null, ?int $day = null, ?int $hour = null, ?int $minutes = null, ?int $seconds = null, $timezone = null, ?string $locale = null) - { + public static function create( + ?int $year = null, + ?int $month = null, + ?int $day = null, + ?int $hour = null, + ?int $minutes = null, + ?int $seconds = null, + $timezone = null, + ?string $locale = null + ) { $year ??= date('Y'); $month ??= date('m'); $day ??= date('d'); - $hour = empty($hour) ? 0 : $hour; - $minutes = empty($minutes) ? 0 : $minutes; - $seconds = empty($seconds) ? 0 : $seconds; + $hour ??= 0; + $minutes ??= 0; + $seconds ??= 0; return new self(date('Y-m-d H:i:s', strtotime("{$year}-{$month}-{$day} {$hour}:{$minutes}:{$seconds}")), $timezone, $locale); } diff --git a/system/Images/Handlers/BaseHandler.php b/system/Images/Handlers/BaseHandler.php index cf23fd0fcd23..92644eede3db 100644 --- a/system/Images/Handlers/BaseHandler.php +++ b/system/Images/Handlers/BaseHandler.php @@ -691,6 +691,8 @@ abstract public function getVersion(); * $image->resize(100, 200, true) * ->save($target); * + * @param non-empty-string|null $target + * * @return bool */ abstract public function save(?string $target = null, int $quality = 90); diff --git a/system/Images/Handlers/GDHandler.php b/system/Images/Handlers/GDHandler.php index 6620dcac431a..f2cd55874044 100644 --- a/system/Images/Handlers/GDHandler.php +++ b/system/Images/Handlers/GDHandler.php @@ -204,11 +204,13 @@ protected function process(string $action) * Example: * $image->resize(100, 200, true) * ->save(); + * + * @param non-empty-string|null $target */ public function save(?string $target = null, int $quality = 90): bool { $original = $target; - $target = empty($target) ? $this->image()->getPathname() : $target; + $target = ($target === null || $target === '') ? $this->image()->getPathname() : $target; // If no new resource has been created, then we're // simply copy the existing one. diff --git a/system/Images/Handlers/ImageMagickHandler.php b/system/Images/Handlers/ImageMagickHandler.php index 857a273d5372..7dd5e5d755ee 100644 --- a/system/Images/Handlers/ImageMagickHandler.php +++ b/system/Images/Handlers/ImageMagickHandler.php @@ -223,11 +223,13 @@ protected function process(string $action, int $quality = 100): array * Example: * $image->resize(100, 200, true) * ->save(); + * + * @param non-empty-string|null $target */ public function save(?string $target = null, int $quality = 90): bool { $original = $target; - $target = empty($target) ? $this->image()->getPathname() : $target; + $target = ($target === null || $target === '') ? $this->image()->getPathname() : $target; // If no new resource has been created, then we're // simply copy the existing one. diff --git a/system/Images/ImageHandlerInterface.php b/system/Images/ImageHandlerInterface.php index 1c3593a99bd4..76a016b08918 100644 --- a/system/Images/ImageHandlerInterface.php +++ b/system/Images/ImageHandlerInterface.php @@ -132,7 +132,7 @@ public function text(string $text, array $options = []); * $image->resize(100, 200, true) * ->save($target); * - * @param string|null $target The path to save the file to. + * @param non-empty-string|null $target The path to save the file to. * * @return bool */ diff --git a/system/Model.php b/system/Model.php index a9fd4b312c4a..d264107fc4dc 100644 --- a/system/Model.php +++ b/system/Model.php @@ -627,6 +627,8 @@ public function countAllResults(bool $reset = true, bool $test = false) /** * Provides a shared instance of the Query Builder. * + * @param non-empty-string|null $table + * * @return BaseBuilder * * @throws ModelException @@ -650,7 +652,7 @@ public function builder(?string $table = null) throw ModelException::forNoPrimaryKey(static::class); } - $table = empty($table) ? $this->table : $table; + $table = ($table === null || $table === '') ? $this->table : $table; // Ensure we have a good db connection if (! $this->db instanceof BaseConnection) { diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index b08cb00410fd..d9de5faab401 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -310,7 +310,7 @@ public function getNextPageURI(string $group = 'default', bool $returnObject = f $curr = $this->getCurrentPage($group); $page = null; - if (! empty($last) && ! empty($curr) && $last === $curr) { + if (! empty($last) && $curr !== 0 && $last === $curr) { return null; } @@ -334,7 +334,7 @@ public function getPreviousPageURI(string $group = 'default', bool $returnObject $curr = $this->getCurrentPage($group); $page = null; - if (! empty($first) && ! empty($curr) && $first === $curr) { + if ($first !== 0 && $curr !== 0 && $first === $curr) { return null; } diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index 16ec68c87507..87a5b3ac3bc1 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -259,7 +259,7 @@ private function isValidSegment(string $segment): bool */ public function setDirectory(?string $dir = null, bool $append = false, bool $validate = true) { - if (empty($dir)) { + if ($dir === null || $dir === '') { $this->directory = null; return; @@ -275,7 +275,7 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va } } - if ($append !== true || empty($this->directory)) { + if ($append !== true || ($this->directory === null || $this->directory === '')) { $this->directory = trim($dir, '/') . '/'; } else { $this->directory .= trim($dir, '/') . '/'; @@ -290,7 +290,7 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va */ public function directory(): string { - return ! empty($this->directory) ? $this->directory : ''; + return ($this->directory !== null && $this->directory !== '') ? $this->directory : ''; } private function controllerName(): string diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 9a51e6f89a98..4407c1076f3d 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -560,11 +560,12 @@ public function shouldAutoRoute(): bool /** * Returns the raw array of available routes. * - * @param bool $includeWildcard Whether to include '*' routes. + * @param non-empty-string|null $verb + * @param bool $includeWildcard Whether to include '*' routes. */ public function getRoutes(?string $verb = null, bool $includeWildcard = true): array { - if (empty($verb)) { + if ($verb === null || $verb === '') { $verb = $this->getHTTPVerb(); } @@ -995,7 +996,7 @@ public function presenter(string $name, ?array $options = null): RouteCollection */ public function match(array $verbs = [], string $from = '', $to = '', ?array $options = null): RouteCollectionInterface { - if (empty($from) || empty($to)) { + if ($from === '' || empty($to)) { throw new InvalidArgumentException('You must supply the parameters: from, to.'); } diff --git a/system/Router/Router.php b/system/Router/Router.php index a4af03643e01..723c292cb931 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -589,7 +589,7 @@ protected function scanControllers(array $segments): array */ public function setDirectory(?string $dir = null, bool $append = false, bool $validate = true) { - if (empty($dir)) { + if ($dir === null || $dir === '') { $this->directory = null; } diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index 7dc19de05a1a..aa0c1cd3a4ff 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -70,9 +70,7 @@ public function __construct(SessionConfig $config, string $ipAddress) $this->keyPrefix .= $this->ipAddress . ':'; } - if (! empty($this->keyPrefix)) { - ini_set('memcached.sess_prefix', $this->keyPrefix); - } + ini_set('memcached.sess_prefix', $this->keyPrefix); } /** diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index 1fb87597ab8a..b429792929b4 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -71,7 +71,7 @@ public function __construct(SessionConfig $config, string $ipAddress) parent::__construct($config, $ipAddress); // Store Session configurations - $this->sessionExpiration = empty($config->expiration) + $this->sessionExpiration = ($config->expiration === 0) ? (int) ini_get('session.gc_maxlifetime') : $config->expiration; // Add sessionCookieName for multiple session cookies. diff --git a/system/Session/Session.php b/system/Session/Session.php index 29516c006417..7dfe5898d78f 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -299,7 +299,7 @@ protected function configure() ini_set('session.gc_maxlifetime', (string) $this->config->expiration); } - if (! empty($this->config->savePath)) { + if ($this->config->savePath !== '') { ini_set('session.save_path', $this->config->savePath); } @@ -486,13 +486,13 @@ public function set($data, $value = null) * * Replaces the legacy method $session->userdata(); * - * @param string|null $key Identifier of the session property to retrieve + * @param non-empty-string|null $key Identifier of the session property to retrieve * * @return array|bool|float|int|object|string|null The property value(s) */ public function get(?string $key = null) { - if (! empty($key) && (null !== ($value = $_SESSION[$key] ?? null) || null !== ($value = dot_array_search($key, $_SESSION ?? [])))) { + if ($key !== null && $key !== '' && (null !== ($value = $_SESSION[$key] ?? null) || null !== ($value = dot_array_search($key, $_SESSION ?? [])))) { return $value; } @@ -500,7 +500,7 @@ public function get(?string $key = null) return $key === null ? [] : null; } - if (! empty($key)) { + if ($key !== null && $key !== '') { return null; } diff --git a/system/Session/SessionInterface.php b/system/Session/SessionInterface.php index e78736bf34b7..424834c79639 100644 --- a/system/Session/SessionInterface.php +++ b/system/Session/SessionInterface.php @@ -51,7 +51,7 @@ public function set($data, $value = null); * * Replaces the legacy method $session->userdata(); * - * @param string $key Identifier of the session property to retrieve + * @param non-empty-string|null $key Identifier of the session property to retrieve * * @return array|bool|float|int|object|string|null The property value(s) */ diff --git a/system/Test/FeatureTestCase.php b/system/Test/FeatureTestCase.php index c94cf926ca89..5c74e27a774d 100644 --- a/system/Test/FeatureTestCase.php +++ b/system/Test/FeatureTestCase.php @@ -186,7 +186,7 @@ public function call(string $method, string $path, ?array $params = null) ->run($routes, true); $output = \ob_get_contents(); - if (empty($response->getBody()) && ! empty($output)) { + if (empty($response->getBody()) && ! ($output === '' || $output === false)) { $response->setBody($output); } @@ -335,7 +335,7 @@ protected function populateGlobals(string $method, $request, ?array $params = nu { // $params should set the query vars if present, // otherwise set it from the URL. - $get = ! empty($params) && $method === 'get' + $get = ($params !== null && $params !== [] && $method === 'get') ? $params : $this->getPrivateProperty($request->getUri(), 'query'); @@ -371,16 +371,19 @@ protected function setRequestBody($request, ?array $params = null) } if (isset($this->bodyFormat) && $this->bodyFormat !== '') { - if (empty($params)) { + if ($params === null || $params === []) { $params = $request->fetchGlobal('request'); } + $formatMime = ''; + if ($this->bodyFormat === 'json') { $formatMime = 'application/json'; } elseif ($this->bodyFormat === 'xml') { $formatMime = 'application/xml'; } - if (! empty($formatMime) && ! empty($params)) { + + if ($formatMime !== '' && ! ($params === null || $params === [])) { $formatted = Services::format()->getFormatter($formatMime)->format($params); $request->setBody($formatted); $request->setHeader('Content-Type', $formatMime); diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index 6acef638f6b3..565e4e892780 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -326,7 +326,8 @@ protected function setupHeaders(IncomingRequest $request) * * Always populate the GET vars based on the URI. * - * @param string $method HTTP verb + * @param string $method HTTP verb + * @param non-empty-array|null $params * * @return Request * @@ -336,7 +337,7 @@ protected function populateGlobals(string $method, Request $request, ?array $par { // $params should set the query vars if present, // otherwise set it from the URL. - $get = (! empty($params) && $method === 'get') + $get = ($params !== null && $params !== [] && $method === 'get') ? $params : $this->getPrivateProperty($request->getUri(), 'query'); diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index 5089f5a0961f..88775d14de8f 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -252,7 +252,7 @@ public function valid_emails(?string $str = null): bool */ public function valid_ip(?string $ip = null, ?string $which = null): bool { - if (empty($ip)) { + if ($ip === null || $ip === '') { return false; } @@ -281,7 +281,7 @@ public function valid_ip(?string $ip = null, ?string $which = null): bool */ public function valid_url(?string $str = null): bool { - if (empty($str)) { + if ($str === null || $str === '') { return false; } @@ -305,7 +305,7 @@ public function valid_url(?string $str = null): bool */ public function valid_url_strict(?string $str = null, ?string $validSchemes = null): bool { - if (empty($str)) { + if ($str === null || $str === '' || $str === '0') { return false; } @@ -322,6 +322,8 @@ public function valid_url_strict(?string $str = null, ?string $validSchemes = nu /** * Checks for a valid date and matches a given date format + * + * @param non-empty-string|null $format */ public function valid_date(?string $str = null, ?string $format = null): bool { @@ -329,7 +331,7 @@ public function valid_date(?string $str = null, ?string $format = null): bool return false; } - if (empty($format)) { + if ($format === null || $format === '') { return strtotime($str) !== false; } diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index f9bcc61bb511..6e71aadb23f3 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -724,13 +724,15 @@ protected function loadRuleSets() * same format used with setRules(). Additionally, check * for {group}_errors for an array of custom error messages. * + * @param non-empty-string|null $group + * * @return array [rules, customErrors] * * @throws ValidationException */ public function loadRuleGroup(?string $group = null) { - if (empty($group)) { + if ($group === null || $group === '') { return []; } @@ -888,7 +890,8 @@ public function setError(string $field, string $error): ValidationInterface /** * Attempts to find the appropriate error message * - * @param string|null $value The value that caused the validation to fail. + * @param non-empty-string|null $label + * @param string|null $value The value that caused the validation to fail. */ protected function getErrorMessage( string $rule, @@ -912,7 +915,7 @@ protected function getErrorMessage( $message = lang('Validation.' . $rule); } - $message = str_replace('{field}', empty($label) ? $field : lang($label), $message); + $message = str_replace('{field}', ($label === null || $label === '') ? $field : lang($label), $message); $message = str_replace( '{param}', empty($this->rules[$param]['label']) ? $param : lang($this->rules[$param]['label']), diff --git a/system/Validation/ValidationInterface.php b/system/Validation/ValidationInterface.php index 78c38468ddd0..c289afc4541d 100644 --- a/system/Validation/ValidationInterface.php +++ b/system/Validation/ValidationInterface.php @@ -134,6 +134,8 @@ public function reset(): ValidationInterface; * same format used with setRules(). Additionally, check * for {group}_errors for an array of custom error messages. * + * @param non-empty-string|null $group + * * @return array */ public function loadRuleGroup(?string $group = null); diff --git a/system/View/Parser.php b/system/View/Parser.php index 82ace74fba12..8410ef969fca 100644 --- a/system/View/Parser.php +++ b/system/View/Parser.php @@ -185,12 +185,12 @@ public function renderString(string $template, ?array $options = null, ?bool $sa * so that the variable is correctly handled within the * parsing itself, and contexts (including raw) are respected. * - * @param string|null $context The context to escape it for: html, css, js, url, raw - * If 'raw', no escaping will happen + * @param non-empty-string|null $context The context to escape it for: html, css, js, url, raw + * If 'raw', no escaping will happen */ public function setData(array $data = [], ?string $context = null): RendererInterface { - if (! empty($context)) { + if ($context !== null && $context !== '') { foreach ($data as $key => &$value) { if (is_array($value)) { foreach ($value as &$obj) { @@ -592,7 +592,7 @@ protected function applyFilters(string $replace, array $filters): string $param = ! empty($param) ? trim($param[0], '() ') : null; // Params can be separated by commas to allow multiple parameters for the filter - if (! empty($param)) { + if ($param !== null && $param !== '') { $param = explode(',', $param); // Clean it up