Skip to content

Commit f2fe526

Browse files
committed
refactor: Fix phpstan boolean errors
1 parent e9bb603 commit f2fe526

38 files changed

+109
-465
lines changed

phpstan-baseline.php

Lines changed: 1 addition & 379 deletions
Large diffs are not rendered by default.

system/BaseModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public function findColumn(string $columnName)
639639

640640
$resultSet = $this->doFindColumn($columnName);
641641

642-
return $resultSet ? array_column($resultSet, $columnName) : null;
642+
return $resultSet !== null ? array_column($resultSet, $columnName) : null;
643643
}
644644

645645
/**
@@ -1137,7 +1137,7 @@ public function delete($id = null, bool $purge = false)
11371137
throw new InvalidArgumentException('delete(): argument #1 ($id) should not be boolean.');
11381138
}
11391139

1140-
if ($id && (is_numeric($id) || is_string($id))) {
1140+
if (! in_array($id, [null, 0, '0'], true) && (is_numeric($id) || is_string($id))) {
11411141
$id = [$id];
11421142
}
11431143

@@ -1250,7 +1250,7 @@ public function errors(bool $forceDB = false)
12501250
}
12511251

12521252
// Do we have validation errors?
1253-
if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors())) {
1253+
if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors()) !== []) {
12541254
return $errors;
12551255
}
12561256

system/CLI/CLI.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ public static function prompt(string $field, $options = null, $validation = null
225225
$extraOutput = '';
226226
$default = '';
227227

228-
if ($validation && ! is_array($validation) && ! is_string($validation)) {
228+
if (isset($validation) && ! is_array($validation) && ! is_string($validation)) {
229229
throw new InvalidArgumentException('$rules can only be of type string|array');
230230
}
231231

232232
if (! is_array($validation)) {
233-
$validation = $validation ? explode('|', $validation) : [];
233+
$validation = ($validation !== null && $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 || $background) {
444+
if ($foreground !== null || $background !== null) {
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 || $background) {
460+
if ($foreground !== null || $background !== null) {
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) {
483+
if ($foreground !== '' || $background !== null) {
484484
$text = static::color($text, $foreground, $background);
485485
}
486486

@@ -768,7 +768,7 @@ public static function generateDimensions()
768768

769769
// Look for the next lines ending in ": <number>"
770770
// Searching for "Columns:" or "Lines:" will fail on non-English locales
771-
if ($return === 0 && $output && preg_match('/:\s*(\d+)\n[^:]+:\s*(\d+)\n/', implode("\n", $output), $matches)) {
771+
if ($return === 0 && $output !== [] && preg_match('/:\s*(\d+)\n[^:]+:\s*(\d+)\n/', implode("\n", $output), $matches)) {
772772
static::$height = (int) $matches[1];
773773
static::$width = (int) $matches[2];
774774
}

system/Cache/Handlers/BaseHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function validateKey($key, $prefix = ''): string
6767
}
6868

6969
$reserved = config(Cache::class)->reservedCharacters ?? self::RESERVED_CHARACTERS;
70-
if ($reserved && strpbrk($key, $reserved) !== false) {
70+
if ($reserved !== '' && strpbrk($key, $reserved) !== false) {
7171
throw new InvalidArgumentException('Cache key contains reserved characters ' . $reserved);
7272
}
7373

system/Cache/Handlers/PredisHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function save(string $key, $value, int $ttl = 60)
121121
return false;
122122
}
123123

124-
if (! $this->redis->hmset($key, ['__ci_type' => $dataType, '__ci_value' => $value])) {
124+
if ($this->redis->hmset($key, ['__ci_type' => $dataType, '__ci_value' => $value]) !== 'OK') {
125125
return false;
126126
}
127127

system/Commands/Server/Serve.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function run(array $params)
110110
// to ensure our environment is set and it simulates basic mod_rewrite.
111111
passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite, $status);
112112

113-
if ($status && $this->portOffset < $this->tries) {
113+
if ($status !== EXIT_SUCCESS && $this->portOffset < $this->tries) {
114114
$this->portOffset++;
115115

116116
$this->run($params);

system/Commands/Utilities/Routes.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function run(array $params)
8686
$host = $params['host'] ?? null;
8787

8888
// Set HTTP_HOST
89-
if ($host) {
89+
if ($host !== null) {
9090
$request = service('request');
9191
$_SERVER = $request->getServer();
9292
$_SERVER['HTTP_HOST'] = $host;
@@ -96,7 +96,7 @@ public function run(array $params)
9696
$collection = service('routes')->loadRoutes();
9797

9898
// Reset HTTP_HOST
99-
if ($host) {
99+
if ($host !== null) {
100100
unset($_SERVER['HTTP_HOST']);
101101
}
102102

@@ -139,7 +139,9 @@ public function run(array $params)
139139
$autoRoutes = $autoRouteCollector->get();
140140

141141
// Check for Module Routes.
142-
if ($routingConfig = config(Routing::class)) {
142+
$routingConfig = config(Routing::class);
143+
144+
if ($routingConfig instanceof Routing) {
143145
foreach ($routingConfig->moduleRoutes as $uri => $namespace) {
144146
$autoRouteCollector = new AutoRouteCollectorImproved(
145147
$namespace,
@@ -188,7 +190,7 @@ public function run(array $params)
188190
usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3]));
189191
}
190192

191-
if ($host) {
193+
if ($host !== null) {
192194
CLI::write('Host: ' . $host);
193195
}
194196

system/Common.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ function esc($data, string $context = 'html', ?string $encoding = null)
441441
$escaper = new Escaper($encoding);
442442
}
443443

444-
if ($encoding && $escaper->getEncoding() !== $encoding) {
444+
if ($encoding !== null && $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 && $locale !== $activeLocale) {
742+
if ($locale !== null && $locale !== $activeLocale) {
743743
$language->setLocale($locale);
744744
}
745745

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

748-
if ($locale && $locale !== $activeLocale) {
748+
if ($locale !== null && $locale !== $activeLocale) {
749749
// Reset to active locale
750750
$language->setLocale($activeLocale);
751751
}

system/Database/Forge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ public function dropTable(string $tableName, bool $ifExists = false, bool $casca
650650
return false;
651651
}
652652

653-
if ($this->db->DBPrefix && str_starts_with($tableName, $this->db->DBPrefix)) {
653+
if ($this->db->DBPrefix !== '' && str_starts_with($tableName, $this->db->DBPrefix)) {
654654
$tableName = substr($tableName, strlen($this->db->DBPrefix));
655655
}
656656

system/Database/MigrationRunner.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ public function force(string $path, string $namespace, ?string $group = null)
389389
*/
390390
public function findMigrations(): array
391391
{
392-
$namespaces = $this->namespace ? [$this->namespace] : array_keys(service('autoloader')->getNamespace());
392+
$namespaces = $this->namespace !== null ? [$this->namespace] : array_keys(service('autoloader')->getNamespace());
393393
$migrations = [];
394394

395395
foreach ($namespaces as $namespace) {
@@ -524,7 +524,7 @@ protected function getMigrationNumber(string $migration): string
524524
{
525525
preg_match($this->regex, $migration, $matches);
526526

527-
return count($matches) ? $matches[1] : '0';
527+
return $matches !== [] ? $matches[1] : '0';
528528
}
529529

530530
/**
@@ -539,7 +539,7 @@ protected function getMigrationName(string $migration): string
539539
{
540540
preg_match($this->regex, $migration, $matches);
541541

542-
return count($matches) ? $matches[2] : '';
542+
return $matches !== [] ? $matches[2] : '';
543543
}
544544

545545
/**
@@ -645,7 +645,7 @@ public function getHistory(string $group = 'default'): array
645645
}
646646

647647
// If a namespace was specified then use it
648-
if ($this->namespace) {
648+
if ($this->namespace !== null) {
649649
$builder->where('namespace', $this->namespace);
650650
}
651651

@@ -700,7 +700,7 @@ public function getLastBatch(): int
700700
->get()
701701
->getResultObject();
702702

703-
$batch = is_array($batch) && count($batch)
703+
$batch = is_array($batch) && $batch !== []
704704
? end($batch)->batch
705705
: 0;
706706

@@ -725,7 +725,7 @@ public function getBatchStart(int $batch): string
725725
->get()
726726
->getResultObject();
727727

728-
return count($migration) ? $migration[0]->version : '0';
728+
return $migration !== [] ? $migration[0]->version : '0';
729729
}
730730

731731
/**

0 commit comments

Comments
 (0)