Skip to content

Commit f3a85de

Browse files
committed
refactor: Improve comparison with an empty string
1 parent f77d144 commit f3a85de

File tree

10 files changed

+33
-33
lines changed

10 files changed

+33
-33
lines changed

system/CLI/CLI.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static function prompt(string $field, $options = null, $validation = null
230230
}
231231

232232
if (! is_array($validation)) {
233-
$validation = ($validation !== null && $validation !== '') ? explode('|', $validation) : [];
233+
$validation = ((string) $validation !== '') ? explode('|', $validation) : [];
234234
}
235235

236236
if (is_string($options)) {
@@ -441,7 +441,7 @@ protected static function validate(string $field, string $value, $rules): bool
441441
*/
442442
public static function print(string $text = '', ?string $foreground = null, ?string $background = null)
443443
{
444-
if ($foreground !== null || $background !== null) {
444+
if ((string) $foreground !== '' || (string) $background !== '') {
445445
$text = static::color($text, $foreground, $background);
446446
}
447447

@@ -457,7 +457,7 @@ public static function print(string $text = '', ?string $foreground = null, ?str
457457
*/
458458
public static function write(string $text = '', ?string $foreground = null, ?string $background = null)
459459
{
460-
if ($foreground !== null || $background !== null) {
460+
if ((string) $foreground !== '' || (string) $background !== '') {
461461
$text = static::color($text, $foreground, $background);
462462
}
463463

@@ -480,7 +480,7 @@ public static function error(string $text, string $foreground = 'light_red', ?st
480480
$stdout = static::$isColored;
481481
static::$isColored = static::hasColorSupport(STDERR);
482482

483-
if ($foreground !== '' || $background !== null) {
483+
if ($foreground !== '' || (string) $background !== '') {
484484
$text = static::color($text, $foreground, $background);
485485
}
486486

@@ -589,7 +589,7 @@ public static function color(string $text, string $foreground, ?string $backgrou
589589
throw CLIException::forInvalidColor('foreground', $foreground);
590590
}
591591

592-
if ($background !== null && ! array_key_exists($background, static::$background_colors)) {
592+
if ((string) $background !== '' && ! array_key_exists($background, static::$background_colors)) {
593593
throw CLIException::forInvalidColor('background', $background);
594594
}
595595

@@ -637,7 +637,7 @@ private static function getColoredText(string $text, string $foreground, ?string
637637
{
638638
$string = "\033[" . static::$foreground_colors[$foreground] . 'm';
639639

640-
if ($background !== null) {
640+
if ((string) $background !== '') {
641641
$string .= "\033[" . static::$background_colors[$background] . 'm';
642642
}
643643

@@ -654,7 +654,7 @@ private static function getColoredText(string $text, string $foreground, ?string
654654
*/
655655
public static function strlen(?string $string): int
656656
{
657-
if ($string === null) {
657+
if ((string) $string === '') {
658658
return 0;
659659
}
660660

@@ -835,7 +835,7 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
835835
*/
836836
public static function wrap(?string $string = null, int $max = 0, int $padLeft = 0): string
837837
{
838-
if ($string === null || $string === '') {
838+
if ((string) $string === '') {
839839
return '';
840840
}
841841

system/Common.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function cache(?string $key = null)
7575
$cache = service('cache');
7676

7777
// No params - return cache object
78-
if ($key === null) {
78+
if ((string) $key === '') {
7979
return $cache;
8080
}
8181

@@ -289,7 +289,7 @@ function csrf_hash(): string
289289
*/
290290
function csrf_field(?string $id = null): string
291291
{
292-
return '<input type="hidden"' . ($id !== null ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
292+
return '<input type="hidden"' . ((string) $id !== '' ? ' id="' . esc($id, 'attr') . '"' : '') . ' name="' . csrf_token() . '" value="' . csrf_hash() . '"' . _solidus() . '>';
293293
}
294294
}
295295

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

@@ -441,7 +441,7 @@ function esc($data, string $context = 'html', ?string $encoding = null)
441441
$escaper = new Escaper($encoding);
442442
}
443443

444-
if ($encoding !== null && $escaper->getEncoding() !== $encoding) {
444+
if ((string) $encoding !== '' && $escaper->getEncoding() !== $encoding) {
445445
$escaper = new Escaper($encoding);
446446
}
447447

@@ -739,13 +739,13 @@ function lang(string $line, array $args = [], ?string $locale = null)
739739
// Get active locale
740740
$activeLocale = $language->getLocale();
741741

742-
if ($locale !== null && $locale !== $activeLocale) {
742+
if ((string) $locale !== '' && $locale !== $activeLocale) {
743743
$language->setLocale($locale);
744744
}
745745

746746
$lines = $language->getLine($line, $args);
747747

748-
if ($locale !== null && $locale !== $activeLocale) {
748+
if ((string) $locale !== '' && $locale !== $activeLocale) {
749749
// Reset to active locale
750750
$language->setLocale($activeLocale);
751751
}
@@ -849,7 +849,7 @@ function redirect(?string $route = null): RedirectResponse
849849
{
850850
$response = service('redirectresponse');
851851

852-
if ($route !== null) {
852+
if ((string) $route !== '') {
853853
return $response->route($route);
854854
}
855855

@@ -1128,7 +1128,7 @@ function timer(?string $name = null, ?callable $callable = null)
11281128
{
11291129
$timer = service('timer');
11301130

1131-
if ($name === null) {
1131+
if ((string) $name === '') {
11321132
return $timer;
11331133
}
11341134

system/Database/MigrationRunner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function latest(?string $group = null)
161161

162162
$this->ensureTable();
163163

164-
if ($group !== null) {
164+
if ((string) $group !== '') {
165165
$this->groupFilter = $group;
166166
$this->setGroup($group);
167167
}
@@ -326,7 +326,7 @@ public function force(string $path, string $namespace, ?string $group = null)
326326

327327
$this->ensureTable();
328328

329-
if ($group !== null) {
329+
if ((string) $group !== '') {
330330
$this->groupFilter = $group;
331331
$this->setGroup($group);
332332
}

system/Database/MySQLi/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ protected function _listTables(bool $prefixLimit = false, ?string $tableName = n
395395
{
396396
$sql = 'SHOW TABLES FROM ' . $this->escapeIdentifier($this->database);
397397

398-
if ($tableName !== null) {
398+
if ((string) $tableName !== '') {
399399
return $sql . ' LIKE ' . $this->escape($tableName);
400400
}
401401

system/Database/SQLite3/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected function _escapeString(string $str): string
194194
*/
195195
protected function _listTables(bool $prefixLimit = false, ?string $tableName = null): string
196196
{
197-
if ($tableName !== null) {
197+
if ((string) $tableName !== '') {
198198
return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
199199
. ' AND "NAME" NOT LIKE \'sqlite!_%\' ESCAPE \'!\''
200200
. ' AND "NAME" LIKE ' . $this->escape($tableName);

system/Filters/Filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public function enableFilters(array $names, string $when = 'before')
557557
*/
558558
public function getArguments(?string $key = null)
559559
{
560-
return $key === null ? $this->arguments : $this->arguments[$key];
560+
return ((string) $key === '') ? $this->arguments : $this->arguments[$key];
561561
}
562562

563563
// --------------------------------------------------------------------

system/HTTP/UserAgent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function isBrowser(?string $key = null): bool
125125
}
126126

127127
// No need to be specific, it's a browser
128-
if ($key === null) {
128+
if ((string) $key === '') {
129129
return true;
130130
}
131131

@@ -143,7 +143,7 @@ public function isRobot(?string $key = null): bool
143143
}
144144

145145
// No need to be specific, it's a robot
146-
if ($key === null) {
146+
if ((string) $key === '') {
147147
return true;
148148
}
149149

@@ -161,7 +161,7 @@ public function isMobile(?string $key = null): bool
161161
}
162162

163163
// No need to be specific, it's a mobile
164-
if ($key === null) {
164+
if ((string) $key === '') {
165165
return true;
166166
}
167167

system/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public function builder(?string $table = null)
690690
// Check for an existing Builder
691691
if ($this->builder instanceof BaseBuilder) {
692692
// Make sure the requested table matches the builder
693-
if ($table !== null && $this->builder->getTable() !== $table) {
693+
if ((string) $table !== '' && $this->builder->getTable() !== $table) {
694694
return $this->db->table($table);
695695
}
696696

@@ -704,7 +704,7 @@ public function builder(?string $table = null)
704704
throw ModelException::forNoPrimaryKey(static::class);
705705
}
706706

707-
$table = ($table === null || $table === '') ? $this->table : $table;
707+
$table = ((string) $table === '') ? $this->table : $table;
708708

709709
// Ensure we have a good db connection
710710
if (! $this->db instanceof BaseConnection) {

system/Router/AutoRouter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function isValidSegment(string $segment): bool
244244
*/
245245
public function setDirectory(?string $dir = null, bool $append = false, bool $validate = true)
246246
{
247-
if ($dir === null || $dir === '') {
247+
if ((string) $dir === '') {
248248
$this->directory = null;
249249

250250
return;
@@ -260,7 +260,7 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va
260260
}
261261
}
262262

263-
if (! $append || ($this->directory === null || $this->directory === '')) {
263+
if (! $append || ((string) $this->directory === '')) {
264264
$this->directory = trim($dir, '/') . '/';
265265
} else {
266266
$this->directory .= trim($dir, '/') . '/';
@@ -275,7 +275,7 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va
275275
*/
276276
public function directory(): string
277277
{
278-
return ($this->directory !== null && $this->directory !== '') ? $this->directory : '';
278+
return ((string) $this->directory !== '') ? $this->directory : '';
279279
}
280280

281281
private function controllerName(): string

system/Router/RouteCollection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public function shouldAutoRoute(): bool
560560
*/
561561
public function getRoutes(?string $verb = null, bool $includeWildcard = true): array
562562
{
563-
if ($verb === null || $verb === '') {
563+
if ((string) $verb === '') {
564564
$verb = $this->getHTTPVerb();
565565
}
566566

@@ -609,7 +609,7 @@ public function getRoutesOptions(?string $from = null, ?string $verb = null): ar
609609
{
610610
$options = $this->loadRoutesOptions($verb);
611611

612-
return $from !== null ? $options[$from] ?? [] : $options;
612+
return ((string) $from !== '') ? $options[$from] ?? [] : $options;
613613
}
614614

615615
/**
@@ -1416,14 +1416,14 @@ private function replaceLocale(string $route, ?string $locale = null): string
14161416
}
14171417

14181418
// Check invalid locale
1419-
if ($locale !== null) {
1419+
if ((string) $locale !== '') {
14201420
$config = config(App::class);
14211421
if (! in_array($locale, $config->supportedLocales, true)) {
14221422
$locale = null;
14231423
}
14241424
}
14251425

1426-
if ($locale === null) {
1426+
if ((string) $locale === '') {
14271427
$locale = service('request')->getLocale();
14281428
}
14291429

0 commit comments

Comments
 (0)