Skip to content

Commit 57c0c5a

Browse files
authored
Merge pull request #8704 from samsonasik/rector-explicit-bool
refactor: [Rector] Apply ExplicitBoolCompareRector
2 parents 9bf27a1 + 1455e8d commit 57c0c5a

File tree

16 files changed

+25
-153
lines changed

16 files changed

+25
-153
lines changed

phpstan-baseline.php

-130
Original file line numberDiff line numberDiff line change
@@ -446,16 +446,6 @@
446446
'count' => 1,
447447
'path' => __DIR__ . '/system/CLI/CLI.php',
448448
];
449-
$ignoreErrors[] = [
450-
'message' => '#^Only booleans are allowed in a negated boolean, array given\\.$#',
451-
'count' => 1,
452-
'path' => __DIR__ . '/system/CLI/CLI.php',
453-
];
454-
$ignoreErrors[] = [
455-
'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
456-
'count' => 1,
457-
'path' => __DIR__ . '/system/CLI/CLI.php',
458-
];
459449
$ignoreErrors[] = [
460450
'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#',
461451
'count' => 1,
@@ -766,11 +756,6 @@
766756
'count' => 1,
767757
'path' => __DIR__ . '/system/Commands/Database/MigrateStatus.php',
768758
];
769-
$ignoreErrors[] = [
770-
'message' => '#^Only booleans are allowed in a negated boolean, array\\<int\\<0, max\\>, array\\<int, mixed\\>\\> given\\.$#',
771-
'count' => 1,
772-
'path' => __DIR__ . '/system/Commands/Database/MigrateStatus.php',
773-
];
774759
$ignoreErrors[] = [
775760
'message' => '#^Parameter \\#1 \\$params \\(array\\<string, mixed\\>\\) of method CodeIgniter\\\\Commands\\\\Database\\\\MigrateStatus\\:\\:run\\(\\) should be contravariant with parameter \\$params \\(array\\<int\\|string, string\\|null\\>\\) of method CodeIgniter\\\\CLI\\\\BaseCommand\\:\\:run\\(\\)$#',
776761
'count' => 1,
@@ -2131,11 +2116,6 @@
21312116
'count' => 1,
21322117
'path' => __DIR__ . '/system/Config/DotEnv.php',
21332118
];
2134-
$ignoreErrors[] = [
2135-
'message' => '#^Only booleans are allowed in a negated boolean, string given\\.$#',
2136-
'count' => 1,
2137-
'path' => __DIR__ . '/system/Config/DotEnv.php',
2138-
];
21392119
$ignoreErrors[] = [
21402120
'message' => '#^Method CodeIgniter\\\\Config\\\\Factories\\:\\:__callStatic\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#',
21412121
'count' => 1,
@@ -2286,11 +2266,6 @@
22862266
'count' => 1,
22872267
'path' => __DIR__ . '/system/Controller.php',
22882268
];
2289-
$ignoreErrors[] = [
2290-
'message' => '#^Only booleans are allowed in a negated boolean, array given\\.$#',
2291-
'count' => 1,
2292-
'path' => __DIR__ . '/system/Controller.php',
2293-
];
22942269
$ignoreErrors[] = [
22952270
'message' => '#^Property CodeIgniter\\\\Controller\\:\\:\\$request \\(CodeIgniter\\\\HTTP\\\\CLIRequest\\|CodeIgniter\\\\HTTP\\\\IncomingRequest\\) does not accept CodeIgniter\\\\HTTP\\\\RequestInterface\\.$#',
22962271
'count' => 1,
@@ -3731,11 +3706,6 @@
37313706
'count' => 1,
37323707
'path' => __DIR__ . '/system/Database/OCI8/Builder.php',
37333708
];
3734-
$ignoreErrors[] = [
3735-
'message' => '#^Only booleans are allowed in a ternary operator condition, int given\\.$#',
3736-
'count' => 1,
3737-
'path' => __DIR__ . '/system/Database/OCI8/Builder.php',
3738-
];
37393709
$ignoreErrors[] = [
37403710
'message' => '#^PHPDoc type CodeIgniter\\\\Database\\\\OCI8\\\\Connection of property CodeIgniter\\\\Database\\\\OCI8\\\\Builder\\:\\:\\$db is not the same as PHPDoc type CodeIgniter\\\\Database\\\\BaseConnection of overridden property CodeIgniter\\\\Database\\\\BaseBuilder\\:\\:\\$db\\.$#',
37413711
'count' => 1,
@@ -6216,11 +6186,6 @@
62166186
'count' => 1,
62176187
'path' => __DIR__ . '/system/HTTP/DownloadResponse.php',
62186188
];
6219-
$ignoreErrors[] = [
6220-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
6221-
'count' => 1,
6222-
'path' => __DIR__ . '/system/HTTP/DownloadResponse.php',
6223-
];
62246189
$ignoreErrors[] = [
62256190
'message' => '#^Return type \\(CodeIgniter\\\\HTTP\\\\DownloadResponse\\) of method CodeIgniter\\\\HTTP\\\\DownloadResponse\\:\\:sendBody\\(\\) should be covariant with return type \\(\\$this\\(CodeIgniter\\\\HTTP\\\\Response\\)\\) of method CodeIgniter\\\\HTTP\\\\Response\\:\\:sendBody\\(\\)$#',
62266191
'count' => 1,
@@ -7116,66 +7081,6 @@
71167081
'count' => 1,
71177082
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
71187083
];
7119-
$ignoreErrors[] = [
7120-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 1024\\> given\\.$#',
7121-
'count' => 2,
7122-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7123-
];
7124-
$ignoreErrors[] = [
7125-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 128\\> given\\.$#',
7126-
'count' => 1,
7127-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7128-
];
7129-
$ignoreErrors[] = [
7130-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 16\\> given\\.$#',
7131-
'count' => 1,
7132-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7133-
];
7134-
$ignoreErrors[] = [
7135-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 1\\> given\\.$#',
7136-
'count' => 1,
7137-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7138-
];
7139-
$ignoreErrors[] = [
7140-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 2048\\> given\\.$#',
7141-
'count' => 2,
7142-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7143-
];
7144-
$ignoreErrors[] = [
7145-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 256\\> given\\.$#',
7146-
'count' => 1,
7147-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7148-
];
7149-
$ignoreErrors[] = [
7150-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 2\\> given\\.$#',
7151-
'count' => 1,
7152-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7153-
];
7154-
$ignoreErrors[] = [
7155-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 32\\> given\\.$#',
7156-
'count' => 1,
7157-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7158-
];
7159-
$ignoreErrors[] = [
7160-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 4\\> given\\.$#',
7161-
'count' => 1,
7162-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7163-
];
7164-
$ignoreErrors[] = [
7165-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 512\\> given\\.$#',
7166-
'count' => 2,
7167-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7168-
];
7169-
$ignoreErrors[] = [
7170-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 64\\> given\\.$#',
7171-
'count' => 1,
7172-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7173-
];
7174-
$ignoreErrors[] = [
7175-
'message' => '#^Only booleans are allowed in a ternary operator condition, int\\<0, 8\\> given\\.$#',
7176-
'count' => 1,
7177-
'path' => __DIR__ . '/system/Helpers/filesystem_helper.php',
7178-
];
71797084
$ignoreErrors[] = [
71807085
'message' => '#^Right side of && is always true\\.$#',
71817086
'count' => 1,
@@ -7386,11 +7291,6 @@
73867291
'count' => 1,
73877292
'path' => __DIR__ . '/system/Helpers/form_helper.php',
73887293
];
7389-
$ignoreErrors[] = [
7390-
'message' => '#^Only booleans are allowed in a negated boolean, string given\\.$#',
7391-
'count' => 1,
7392-
'path' => __DIR__ . '/system/Helpers/form_helper.php',
7393-
];
73947294
$ignoreErrors[] = [
73957295
'message' => '#^Function _list\\(\\) has parameter \\$attributes with no value type specified in iterable type array\\.$#',
73967296
'count' => 1,
@@ -7471,11 +7371,6 @@
74717371
'count' => 1,
74727372
'path' => __DIR__ . '/system/Helpers/html_helper.php',
74737373
];
7474-
$ignoreErrors[] = [
7475-
'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
7476-
'count' => 1,
7477-
'path' => __DIR__ . '/system/Helpers/html_helper.php',
7478-
];
74797374
$ignoreErrors[] = [
74807375
'message' => '#^Function d\\(\\) has parameter \\$vars with no value type specified in iterable type array\\.$#',
74817376
'count' => 1,
@@ -7576,11 +7471,6 @@
75767471
'count' => 1,
75777472
'path' => __DIR__ . '/system/Helpers/url_helper.php',
75787473
];
7579-
$ignoreErrors[] = [
7580-
'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
7581-
'count' => 1,
7582-
'path' => __DIR__ . '/system/Helpers/url_helper.php',
7583-
];
75847474
$ignoreErrors[] = [
75857475
'message' => '#^Variable \\$atts might not be defined\\.$#',
75867476
'count' => 1,
@@ -8386,11 +8276,6 @@
83868276
'count' => 1,
83878277
'path' => __DIR__ . '/system/Router/RouteCollection.php',
83888278
];
8389-
$ignoreErrors[] = [
8390-
'message' => '#^Only booleans are allowed in a ternary operator condition, string given\\.$#',
8391-
'count' => 1,
8392-
'path' => __DIR__ . '/system/Router/RouteCollection.php',
8393-
];
83948279
$ignoreErrors[] = [
83958280
'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#',
83968281
'count' => 1,
@@ -8641,11 +8526,6 @@
86418526
'count' => 1,
86428527
'path' => __DIR__ . '/system/Session/Handlers/FileHandler.php',
86438528
];
8644-
$ignoreErrors[] = [
8645-
'message' => '#^Only booleans are allowed in a negated boolean, string given\\.$#',
8646-
'count' => 1,
8647-
'path' => __DIR__ . '/system/Session/Handlers/FileHandler.php',
8648-
];
86498529
$ignoreErrors[] = [
86508530
'message' => '#^PHPDoc type string of property CodeIgniter\\\\Session\\\\Handlers\\\\FileHandler\\:\\:\\$savePath is not the same as PHPDoc type array\\|string of overridden property CodeIgniter\\\\Session\\\\Handlers\\\\BaseHandler\\:\\:\\$savePath\\.$#',
86518531
'count' => 1,
@@ -10606,11 +10486,6 @@
1060610486
'count' => 1,
1060710487
'path' => __DIR__ . '/system/View/Table.php',
1060810488
];
10609-
$ignoreErrors[] = [
10610-
'message' => '#^Only booleans are allowed in a ternary operator condition, float given\\.$#',
10611-
'count' => 1,
10612-
'path' => __DIR__ . '/system/View/Table.php',
10613-
];
1061410489
$ignoreErrors[] = [
1061510490
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
1061610491
'count' => 1,
@@ -12036,11 +11911,6 @@
1203611911
'count' => 1,
1203711912
'path' => __DIR__ . '/tests/system/Cache/ResponseCacheTest.php',
1203811913
];
12039-
$ignoreErrors[] = [
12040-
'message' => '#^Only booleans are allowed in a ternary operator condition, array given\\.$#',
12041-
'count' => 1,
12042-
'path' => __DIR__ . '/tests/system/Cache/ResponseCacheTest.php',
12043-
];
1204411914
$ignoreErrors[] = [
1204511915
'message' => '#^Re\\-assigning arrays to \\$_GET directly is discouraged\\.$#',
1204611916
'count' => 3,

rector.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
2222
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
2323
use Rector\CodeQuality\Rector\If_\CombineIfRector;
24+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
2425
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
2526
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
2627
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
@@ -158,6 +159,7 @@
158159
$rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class);
159160
$rectorConfig->rule(SingleInArrayToCompareRector::class);
160161
$rectorConfig->rule(VersionCompareFuncCallToConstantRector::class);
162+
$rectorConfig->rule(ExplicitBoolCompareRector::class);
161163

162164
$rectorConfig
163165
->ruleWithConfiguration(StringClassNameToClassConstantRector::class, [

system/CLI/CLI.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public static function prompt(string $field, $options = null, $validation = null
262262
$default = $options[0];
263263
}
264264

265-
static::fwrite(STDOUT, $field . (trim($field) ? ' ' : '') . $extraOutput . ': ');
265+
static::fwrite(STDOUT, $field . (trim($field) !== '' ? ' ' : '') . $extraOutput . ': ');
266266

267267
// Read the input from keyboard.
268268
$input = trim(static::input()) ?: $default;
@@ -386,7 +386,7 @@ public static function promptByMultipleKeys(string $text, array $options): array
386386
*/
387387
private static function isZeroOptions(array $options): void
388388
{
389-
if (! $options) {
389+
if ($options === []) {
390390
throw new InvalidArgumentException('No options to select from were provided');
391391
}
392392
}

system/Commands/Database/MigrateStatus.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function run(array $params)
144144
}
145145
}
146146

147-
if (! $status) {
147+
if ($status === []) {
148148
// @codeCoverageIgnoreStart
149149
CLI::error(lang('Migrations.noneFound'), 'light_gray', 'red');
150150
CLI::newLine();

system/Config/DotEnv.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function normaliseVariable(string $name, string $value = ''): array
140140
*/
141141
protected function sanitizeValue(string $value): string
142142
{
143-
if (! $value) {
143+
if ($value === '') {
144144
return $value;
145145
}
146146

system/Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private function setValidator($rules, array $messages): void
187187
}
188188

189189
// If no error message is defined, use the error message in the Config\Validation file
190-
if (! $messages) {
190+
if ($messages === []) {
191191
$errorName = $rules . '_errors';
192192
$messages = $validation->{$errorName} ?? [];
193193
}

system/Database/OCI8/Builder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected function _limit(string $sql, bool $offsetIgnore = false): string
222222
}
223223

224224
$this->limitUsed = true;
225-
$limitTemplateQuery = 'SELECT * FROM (SELECT INNER_QUERY.*, ROWNUM RNUM FROM (%s) INNER_QUERY WHERE ROWNUM < %d)' . ($offset ? ' WHERE RNUM >= %d' : '');
225+
$limitTemplateQuery = 'SELECT * FROM (SELECT INNER_QUERY.*, ROWNUM RNUM FROM (%s) INNER_QUERY WHERE ROWNUM < %d)' . ($offset !== 0 ? ' WHERE RNUM >= %d' : '');
226226

227227
return sprintf($limitTemplateQuery, $sql, $offset + $this->QBLimit + 1, $offset);
228228
}

system/HTTP/DownloadResponse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function getContentDisposition(): string
194194

195195
$result = sprintf('attachment; filename="%s"', $downloadFilename);
196196

197-
if ($utf8Filename) {
197+
if ($utf8Filename !== '') {
198198
$result .= '; filename*=UTF-8\'\'' . rawurlencode($utf8Filename);
199199
}
200200

system/Helpers/filesystem_helper.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,19 @@ function symbolic_permissions(int $perms): string
379379
}
380380

381381
// Owner
382-
$symbolic .= (($perms & 0x0100) ? 'r' : '-')
383-
. (($perms & 0x0080) ? 'w' : '-')
384-
. (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));
382+
$symbolic .= ((($perms & 0x0100) !== 0) ? 'r' : '-')
383+
. ((($perms & 0x0080) !== 0) ? 'w' : '-')
384+
. ((($perms & 0x0040) !== 0) ? ((($perms & 0x0800) !== 0) ? 's' : 'x') : ((($perms & 0x0800) !== 0) ? 'S' : '-'));
385385

386386
// Group
387-
$symbolic .= (($perms & 0x0020) ? 'r' : '-')
388-
. (($perms & 0x0010) ? 'w' : '-')
389-
. (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));
387+
$symbolic .= ((($perms & 0x0020) !== 0) ? 'r' : '-')
388+
. ((($perms & 0x0010) !== 0) ? 'w' : '-')
389+
. ((($perms & 0x0008) !== 0) ? ((($perms & 0x0400) !== 0) ? 's' : 'x') : ((($perms & 0x0400) !== 0) ? 'S' : '-'));
390390

391391
// World
392-
$symbolic .= (($perms & 0x0004) ? 'r' : '-')
393-
. (($perms & 0x0002) ? 'w' : '-')
394-
. (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));
392+
$symbolic .= ((($perms & 0x0004) !== 0) ? 'r' : '-')
393+
. ((($perms & 0x0002) !== 0) ? 'w' : '-')
394+
. ((($perms & 0x0001) !== 0) ? ((($perms & 0x0200) !== 0) ? 't' : 'x') : ((($perms & 0x0200) !== 0) ? 'T' : '-'));
395395

396396
return $symbolic;
397397
}

system/Helpers/form_helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
function form_open(string $action = '', $attributes = [], array $hidden = []): string
3030
{
3131
// If no action is provided then set to the current url
32-
if (! $action) {
32+
if ($action === '') {
3333
$action = current_url(true);
3434
} // If an action is not a full URL then turn it into one
3535
elseif (strpos($action, '://') === false) {

system/Helpers/html_helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function doctype(string $type = 'html5'): string
195195
function script_tag($src = '', bool $indexPage = false): string
196196
{
197197
$cspNonce = csp_script_nonce();
198-
$cspNonce = $cspNonce ? ' ' . $cspNonce : $cspNonce;
198+
$cspNonce = $cspNonce !== '' ? ' ' . $cspNonce : $cspNonce;
199199
$script = '<script' . $cspNonce . ' ';
200200
if (! is_array($src)) {
201201
$src = ['src' => $src];

system/Helpers/url_helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ function safe_mailto(string $email, string $title = '', $attributes = ''): strin
318318

319319
// improve obfuscation by eliminating newlines & whitespace
320320
$cspNonce = csp_script_nonce();
321-
$cspNonce = $cspNonce ? ' ' . $cspNonce : $cspNonce;
321+
$cspNonce = $cspNonce !== '' ? ' ' . $cspNonce : $cspNonce;
322322
$output = '<script' . $cspNonce . '>'
323323
. 'var l=new Array();';
324324

system/Router/RouteCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ public function group(string $name, ...$params)
767767
// To register a route, we'll set a flag so that our router
768768
// will see the group name.
769769
// If the group name is empty, we go on using the previously built group name.
770-
$this->group = $name ? trim($oldGroup . '/' . $name, '/') : $oldGroup;
770+
$this->group = $name !== '' ? trim($oldGroup . '/' . $name, '/') : $oldGroup;
771771

772772
$callback = array_pop($params);
773773

system/Session/Handlers/FileHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(SessionConfig $config, string $ipAddress)
7373
} else {
7474
$sessionPath = rtrim(ini_get('session.save_path'), '/\\');
7575

76-
if (! $sessionPath) {
76+
if ($sessionPath === '') {
7777
$sessionPath = WRITEPATH . 'session';
7878
}
7979

system/View/Table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public function generate($tableData = null)
365365

366366
foreach ($this->rows as $row) {
367367
// We use modulus to alternate the row colors
368-
$name = fmod($i++, 2) ? '' : 'alt_';
368+
$name = fmod($i++, 2) !== 0.0 ? '' : 'alt_';
369369

370370
$out .= $this->template['row_' . $name . 'start'] . $this->newline;
371371

tests/system/Cache/ResponseCacheTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function createIncomingRequest(
4848
): IncomingRequest {
4949
$_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = [];
5050

51-
$_SERVER['REQUEST_URI'] = '/' . $uri . ($query ? '?' . http_build_query($query) : '');
51+
$_SERVER['REQUEST_URI'] = '/' . $uri . ($query !== [] ? '?' . http_build_query($query) : '');
5252
$_SERVER['SCRIPT_NAME'] = '/index.php';
5353

5454
$appConfig ??= $this->appConfig;

0 commit comments

Comments
 (0)