Skip to content

Commit

Permalink
refactor: Improve comparison with an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Dec 11, 2024
1 parent 37a924d commit 0bf76f5
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 33 deletions.
16 changes: 8 additions & 8 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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';
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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 '';
}

Expand Down
16 changes: 8 additions & 8 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -289,7 +289,7 @@ function csrf_hash(): string
*/
function csrf_field(?string $id = null): string
{
return '<input type="hidden"' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
return '<input type="hidden"' . ((string) $id !== '' ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
}
}

Expand All @@ -301,7 +301,7 @@ function csrf_field(?string $id = null): string
*/
function csrf_meta(?string $id = null): string
{
return '<meta' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
return '<meta' . ((string) $id !== '' ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_header() . '" content="' . csrf_hash() . '"' . _solidus() . '>';
}
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -849,7 +849,7 @@ function redirect(?string $route = null): RedirectResponse
{
$response = service('redirectresponse');

if ($route !== null) {
if ((string) $route !== '') {
return $response->route($route);
}

Expand Down Expand Up @@ -1128,7 +1128,7 @@ function timer(?string $name = null, ?callable $callable = null)
{
$timer = service('timer');

if ($name === null) {
if ((string) $name === '') {
return $timer;
}

Expand Down
4 changes: 2 additions & 2 deletions system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function latest(?string $group = null)

$this->ensureTable();

if ($group !== null) {
if ((string) $group !== '') {
$this->groupFilter = $group;
$this->setGroup($group);
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

// --------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions system/HTTP/UserAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions system/Router/AutoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, '/') . '/';
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 0bf76f5

Please sign in to comment.