From c866e750ca7d93b9424aefe19f958abd7ab7ae11 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Wed, 11 Dec 2024 21:34:24 +0300 Subject: [PATCH] refactor: Improve comparison with an empty string --- system/CLI/CLI.php | 16 ++++++++-------- system/Common.php | 18 +++++++++--------- system/Database/MigrationRunner.php | 4 ++-- system/Database/MySQLi/Connection.php | 2 +- system/Database/SQLite3/Connection.php | 2 +- system/Filters/Filters.php | 2 +- system/HTTP/UserAgent.php | 6 +++--- system/Model.php | 4 ++-- system/Router/AutoRouter.php | 6 +++--- system/Router/RouteCollection.php | 8 ++++---- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index ac1d929229c1..8a1d64e5a019 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -230,7 +230,7 @@ public static function prompt(string $field, $options = null, $validation = null } if (! is_array($validation)) { - $validation = ($validation !== null && $validation !== '') ? explode('|', $validation) : []; + $validation = ((string) $validation !== '') ? explode('|', $validation) : []; } if (is_string($options)) { @@ -441,7 +441,7 @@ protected static function validate(string $field, string $value, $rules): bool */ public static function print(string $text = '', ?string $foreground = null, ?string $background = null) { - if ($foreground !== null || $background !== null) { + if ((string) $foreground !== '' || (string) $background !== '') { $text = static::color($text, $foreground, $background); } @@ -457,7 +457,7 @@ public static function print(string $text = '', ?string $foreground = null, ?str */ public static function write(string $text = '', ?string $foreground = null, ?string $background = null) { - if ($foreground !== null || $background !== null) { + if ((string) $foreground !== '' || (string) $background !== '') { $text = static::color($text, $foreground, $background); } @@ -480,7 +480,7 @@ public static function error(string $text, string $foreground = 'light_red', ?st $stdout = static::$isColored; static::$isColored = static::hasColorSupport(STDERR); - if ($foreground !== '' || $background !== null) { + if ($foreground !== '' || (string) $background !== '') { $text = static::color($text, $foreground, $background); } @@ -589,7 +589,7 @@ public static function color(string $text, string $foreground, ?string $backgrou throw CLIException::forInvalidColor('foreground', $foreground); } - if ($background !== null && ! array_key_exists($background, static::$background_colors)) { + if ((string) $background !== '' && ! array_key_exists($background, static::$background_colors)) { throw CLIException::forInvalidColor('background', $background); } @@ -637,7 +637,7 @@ private static function getColoredText(string $text, string $foreground, ?string { $string = "\033[" . static::$foreground_colors[$foreground] . 'm'; - if ($background !== null) { + if ((string) $background !== '') { $string .= "\033[" . static::$background_colors[$background] . 'm'; } @@ -654,7 +654,7 @@ private static function getColoredText(string $text, string $foreground, ?string */ public static function strlen(?string $string): int { - if ($string === null) { + if ((string) $string === '') { return 0; } @@ -835,7 +835,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 ($string === null || $string === '') { + if ((string) $string === '') { return ''; } diff --git a/system/Common.php b/system/Common.php index 996658e6db1e..10c80ea0b7ea 100644 --- a/system/Common.php +++ b/system/Common.php @@ -75,7 +75,7 @@ function cache(?string $key = null) $cache = service('cache'); // No params - return cache object - if ($key === null) { + if ((string) $key === '') { return $cache; } @@ -289,7 +289,7 @@ function csrf_hash(): string */ function csrf_field(?string $id = null): string { - return ''; + return ''; } } @@ -301,7 +301,7 @@ function csrf_field(?string $id = null): string */ function csrf_meta(?string $id = null): string { - return ''; + return ''; } } @@ -441,7 +441,7 @@ function esc($data, string $context = 'html', ?string $encoding = null) $escaper = new Escaper($encoding); } - if ($encoding !== null && $escaper->getEncoding() !== $encoding) { + if ((string) $encoding !== '' && $escaper->getEncoding() !== $encoding) { $escaper = new Escaper($encoding); } @@ -739,13 +739,13 @@ function lang(string $line, array $args = [], ?string $locale = null) // Get active locale $activeLocale = $language->getLocale(); - if ($locale !== null && $locale !== $activeLocale) { + if ((string) $locale !== '' && $locale !== $activeLocale) { $language->setLocale($locale); } $lines = $language->getLine($line, $args); - if ($locale !== null && $locale !== $activeLocale) { + if ((string) $locale !== '' && $locale !== $activeLocale) { // Reset to active locale $language->setLocale($activeLocale); } @@ -849,7 +849,7 @@ function redirect(?string $route = null): RedirectResponse { $response = service('redirectresponse'); - if ($route !== null) { + if ((string) $route !== '') { return $response->route($route); } @@ -1012,7 +1012,7 @@ function single_service(string $name, ...$params): ?object { $service = Services::serviceExists($name); - if ($service === null) { + if ((string) $service === '') { // The service is not defined anywhere so just return. return null; } @@ -1128,7 +1128,7 @@ function timer(?string $name = null, ?callable $callable = null) { $timer = service('timer'); - if ($name === null) { + if ((string) $name === '') { return $timer; } diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index 4a39f13562e8..d8b2896bfec6 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -161,7 +161,7 @@ public function latest(?string $group = null) $this->ensureTable(); - if ($group !== null) { + if ((string) $group !== '') { $this->groupFilter = $group; $this->setGroup($group); } @@ -326,7 +326,7 @@ public function force(string $path, string $namespace, ?string $group = null) $this->ensureTable(); - if ($group !== null) { + if ((string) $group !== '') { $this->groupFilter = $group; $this->setGroup($group); } diff --git a/system/Database/MySQLi/Connection.php b/system/Database/MySQLi/Connection.php index c5a5645b17f8..8b165b870ae0 100644 --- a/system/Database/MySQLi/Connection.php +++ b/system/Database/MySQLi/Connection.php @@ -395,7 +395,7 @@ protected function _listTables(bool $prefixLimit = false, ?string $tableName = n { $sql = 'SHOW TABLES FROM ' . $this->escapeIdentifier($this->database); - if ($tableName !== null) { + if ((string) $tableName !== '') { return $sql . ' LIKE ' . $this->escape($tableName); } diff --git a/system/Database/SQLite3/Connection.php b/system/Database/SQLite3/Connection.php index 408fd46d14f0..577e4b24ab76 100644 --- a/system/Database/SQLite3/Connection.php +++ b/system/Database/SQLite3/Connection.php @@ -194,7 +194,7 @@ protected function _escapeString(string $str): string */ protected function _listTables(bool $prefixLimit = false, ?string $tableName = null): string { - if ($tableName !== null) { + if ((string) $tableName !== '') { return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'' . ' AND "NAME" NOT LIKE \'sqlite!_%\' ESCAPE \'!\'' . ' AND "NAME" LIKE ' . $this->escape($tableName); diff --git a/system/Filters/Filters.php b/system/Filters/Filters.php index 58a2b0669764..fd252fe1fa26 100644 --- a/system/Filters/Filters.php +++ b/system/Filters/Filters.php @@ -557,7 +557,7 @@ public function enableFilters(array $names, string $when = 'before') */ public function getArguments(?string $key = null) { - return $key === null ? $this->arguments : $this->arguments[$key]; + return ((string) $key === '') ? $this->arguments : $this->arguments[$key]; } // -------------------------------------------------------------------- diff --git a/system/HTTP/UserAgent.php b/system/HTTP/UserAgent.php index 88dd56553e91..ec4d17438284 100644 --- a/system/HTTP/UserAgent.php +++ b/system/HTTP/UserAgent.php @@ -125,7 +125,7 @@ public function isBrowser(?string $key = null): bool } // No need to be specific, it's a browser - if ($key === null) { + if ((string) $key === '') { return true; } @@ -143,7 +143,7 @@ public function isRobot(?string $key = null): bool } // No need to be specific, it's a robot - if ($key === null) { + if ((string) $key === '') { return true; } @@ -161,7 +161,7 @@ public function isMobile(?string $key = null): bool } // No need to be specific, it's a mobile - if ($key === null) { + if ((string) $key === '') { return true; } diff --git a/system/Model.php b/system/Model.php index bdbaf45d7629..98e3be0b1f68 100644 --- a/system/Model.php +++ b/system/Model.php @@ -690,7 +690,7 @@ public function builder(?string $table = null) // Check for an existing Builder if ($this->builder instanceof BaseBuilder) { // Make sure the requested table matches the builder - if ($table !== null && $this->builder->getTable() !== $table) { + if ((string) $table !== '' && $this->builder->getTable() !== $table) { return $this->db->table($table); } @@ -704,7 +704,7 @@ public function builder(?string $table = null) throw ModelException::forNoPrimaryKey(static::class); } - $table = ($table === null || $table === '') ? $this->table : $table; + $table = ((string) $table === '') ? $this->table : $table; // Ensure we have a good db connection if (! $this->db instanceof BaseConnection) { diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index 39665afa6747..ce40f5cdc406 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -244,7 +244,7 @@ private function isValidSegment(string $segment): bool */ public function setDirectory(?string $dir = null, bool $append = false, bool $validate = true) { - if ($dir === null || $dir === '') { + if ((string) $dir === '') { $this->directory = null; return; @@ -260,7 +260,7 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va } } - if (! $append || ($this->directory === null || $this->directory === '')) { + if (! $append || ((string) $this->directory === '')) { $this->directory = trim($dir, '/') . '/'; } else { $this->directory .= trim($dir, '/') . '/'; @@ -275,7 +275,7 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va */ public function directory(): string { - return ($this->directory !== null && $this->directory !== '') ? $this->directory : ''; + return ((string) $this->directory !== '') ? $this->directory : ''; } private function controllerName(): string diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 52e9eb8073bd..2dbaba05e4cd 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -560,7 +560,7 @@ public function shouldAutoRoute(): bool */ public function getRoutes(?string $verb = null, bool $includeWildcard = true): array { - if ($verb === null || $verb === '') { + if ((string) $verb === '') { $verb = $this->getHTTPVerb(); } @@ -609,7 +609,7 @@ public function getRoutesOptions(?string $from = null, ?string $verb = null): ar { $options = $this->loadRoutesOptions($verb); - return $from !== null ? $options[$from] ?? [] : $options; + return ((string) $from !== '') ? $options[$from] ?? [] : $options; } /** @@ -1416,14 +1416,14 @@ private function replaceLocale(string $route, ?string $locale = null): string } // Check invalid locale - if ($locale !== null) { + if ((string) $locale !== '') { $config = config(App::class); if (! in_array($locale, $config->supportedLocales, true)) { $locale = null; } } - if ($locale === null) { + if ((string) $locale === '') { $locale = service('request')->getLocale(); }