Skip to content

Commit 8cff099

Browse files
authored
refactor: fix phpstan only boolean allowed (#9302)
* refactor: Fix `phpstan` boolean errors * fix: Check predis response * refactor: Improve comparison with an empty string * dev: Update phpstan-baseline * fix: Revert comparison with null
1 parent 19ad2b9 commit 8cff099

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+128
-448
lines changed

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: 10 additions & 10 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) ? 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 ((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 || $background) {
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) {
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

@@ -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
}
@@ -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/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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Exception;
2020
use Predis\Client;
2121
use Predis\Collection\Iterator\Keyspace;
22+
use Predis\Response\Status;
2223

2324
/**
2425
* Predis cache handler
@@ -121,7 +122,7 @@ public function save(string $key, $value, int $ttl = 60)
121122
return false;
122123
}
123124

124-
if (! $this->redis->hmset($key, ['__ci_type' => $dataType, '__ci_value' => $value])) {
125+
if (! $this->redis->hmset($key, ['__ci_type' => $dataType, '__ci_value' => $value]) instanceof Status) {
125126
return false;
126127
}
127128

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: 4 additions & 4 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 ((string) $locale !== '' && $locale !== $activeLocale) {
743743
$language->setLocale($locale);
744744
}
745745

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

748-
if ($locale && $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

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
/**

system/Database/MySQLi/Connection.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function connect(bool $persistent = false)
193193
$clientFlags
194194
)) {
195195
// Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
196-
if (($clientFlags & MYSQLI_CLIENT_SSL) && version_compare($this->mysqli->client_info, 'mysqlnd 5.7.3', '<=')
196+
if (($clientFlags & MYSQLI_CLIENT_SSL) !== 0 && version_compare($this->mysqli->client_info, 'mysqlnd 5.7.3', '<=')
197197
&& empty($this->mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
198198
) {
199199
$this->mysqli->close();
@@ -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

@@ -462,7 +462,9 @@ protected function _indexData(string $table): array
462462
throw new DatabaseException(lang('Database.failGetIndexData'));
463463
}
464464

465-
if (! $indexes = $query->getResultArray()) {
465+
$indexes = $query->getResultArray();
466+
467+
if ($indexes === []) {
466468
return [];
467469
}
468470

system/Database/SQLite3/Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function connect(bool $persistent = false)
8989
$this->database = WRITEPATH . $this->database;
9090
}
9191

92-
$sqlite = (! $this->password)
92+
$sqlite = (! isset($this->password) || $this->password !== '')
9393
? new SQLite3($this->database)
9494
: new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
9595

@@ -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/Email/Email.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public function setFrom($from, $name = '', $returnPath = null)
485485
if ($this->validate) {
486486
$this->validateEmail($this->stringToArray($from));
487487

488-
if ($returnPath) {
488+
if ($returnPath !== null) {
489489
$this->validateEmail($this->stringToArray($returnPath));
490490
}
491491
}
@@ -1233,7 +1233,9 @@ protected function buildMessage()
12331233
$this->headerStr .= $hdr;
12341234
}
12351235

1236-
static::strlen($body) && $body .= $this->newline . $this->newline;
1236+
if (static::strlen($body) > 0) {
1237+
$body .= $this->newline . $this->newline;
1238+
}
12371239

12381240
$body .= $this->getMimeMessage() . $this->newline . $this->newline
12391241
. '--' . $lastBoundary . $this->newline

system/Encryption/Handlers/OpenSSLHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class OpenSSLHandler extends BaseHandler
8282
public function encrypt($data, $params = null)
8383
{
8484
// Allow key override
85-
if ($params) {
85+
if ($params !== null) {
8686
$this->key = is_array($params) && isset($params['key']) ? $params['key'] : $params;
8787
}
8888

@@ -118,7 +118,7 @@ public function encrypt($data, $params = null)
118118
public function decrypt($data, $params = null)
119119
{
120120
// Allow key override
121-
if ($params) {
121+
if ($params !== null) {
122122
$this->key = is_array($params) && isset($params['key']) ? $params['key'] : $params;
123123
}
124124

system/Filters/Filters.php

Lines changed: 2 additions & 2 deletions
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
// --------------------------------------------------------------------
@@ -674,7 +674,7 @@ protected function processMethods()
674674
*/
675675
protected function processFilters(?string $uri = null)
676676
{
677-
if (! isset($this->config->filters) || ! $this->config->filters) {
677+
if (! isset($this->config->filters) || $this->config->filters === []) {
678678
return;
679679
}
680680

system/HTTP/Files/FileCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ protected function getValueDotNotationSyntax(array $index, array $value)
253253
{
254254
$currentIndex = array_shift($index);
255255

256-
if (isset($currentIndex) && is_array($index) && $index && is_array($value[$currentIndex]) && $value[$currentIndex]) {
256+
if (isset($currentIndex) && is_array($index) && $index !== [] && array_key_exists($currentIndex, $value) && is_array($value[$currentIndex])) {
257257
return $this->getValueDotNotationSyntax($index, $value[$currentIndex]);
258258
}
259259

0 commit comments

Comments
 (0)