Skip to content

Commit 4d2a6e5

Browse files
committed
refactor: Fix phpstan boolean errors
1 parent cc1b8f2 commit 4d2a6e5

37 files changed

+108
-86
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: 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
/**

system/Database/MySQLi/Connection.php

Lines changed: 4 additions & 2 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();
@@ -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: 1 addition & 1 deletion
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

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

system/HTTP/OutgoingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function getHostFromUri(URI $uri): string
6464
{
6565
$host = $uri->getHost();
6666

67-
return $host . ($uri->getPort() ? ':' . $uri->getPort() : '');
67+
return $host . ($uri->getPort() > 0 ? ':' . $uri->getPort() : '');
6868
}
6969

7070
/**

system/HTTP/RedirectResponse.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ private function withErrors(): self
115115
{
116116
$validation = service('validation');
117117

118-
if ($validation->getErrors()) {
119-
$session = service('session');
120-
$session->setFlashdata('_ci_validation_errors', $validation->getErrors());
118+
if ($validation->getErrors() !== []) {
119+
service('session')->setFlashdata('_ci_validation_errors', $validation->getErrors());
121120
}
122121

123122
return $this;

system/HTTP/RequestTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ trait RequestTrait
5959
*/
6060
public function getIPAddress(): string
6161
{
62-
if ($this->ipAddress) {
62+
if ($this->ipAddress !== '') {
6363
return $this->ipAddress;
6464
}
6565

system/HTTP/ResponseTrait.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,21 @@ public function setDate(DateTime $date)
123123
*/
124124
public function setLink(PagerInterface $pager)
125125
{
126-
$links = '';
126+
$links = '';
127+
$previous = $pager->getPreviousPageURI();
127128

128-
if ($previous = $pager->getPreviousPageURI()) {
129+
if (is_string($previous) && $previous !== '') {
129130
$links .= '<' . $pager->getPageURI($pager->getFirstPage()) . '>; rel="first",';
130131
$links .= '<' . $previous . '>; rel="prev"';
131132
}
132133

133-
if (($next = $pager->getNextPageURI()) && $previous) {
134+
$next = $pager->getNextPageURI();
135+
136+
if (is_string($next) && $next !== '' && is_string($previous) && $previous !== '') {
134137
$links .= ',';
135138
}
136139

137-
if ($next) {
140+
if (is_string($next) && $next !== '') {
138141
$links .= '<' . $next . '>; rel="next",';
139142
$links .= '<' . $pager->getPageURI($pager->getLastPage()) . '>; rel="last"';
140143
}

0 commit comments

Comments
 (0)