Skip to content

Commit 431748b

Browse files
authored
Merge pull request #7951 from samsonasik/boolean-in-if
[Rector] Apply BooleanInIfConditionRuleFixerRector
2 parents 08458d3 + 31ed124 commit 431748b

23 files changed

+38
-156
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
'count' => 1,
2727
'path' => __DIR__ . '/system/Autoloader/FileLocator.php',
2828
];
29-
$ignoreErrors[] = [
30-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
31-
'count' => 1,
32-
'path' => __DIR__ . '/system/Autoloader/FileLocator.php',
33-
];
3429
$ignoreErrors[] = [
3530
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
3631
'count' => 6,
@@ -71,16 +66,6 @@
7166
'count' => 1,
7267
'path' => __DIR__ . '/system/BaseModel.php',
7368
];
74-
$ignoreErrors[] = [
75-
'message' => '#^Only booleans are allowed in an if condition, array\\|null given\\.$#',
76-
'count' => 1,
77-
'path' => __DIR__ . '/system/BaseModel.php',
78-
];
79-
$ignoreErrors[] = [
80-
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
81-
'count' => 1,
82-
'path' => __DIR__ . '/system/BaseModel.php',
83-
];
8469
$ignoreErrors[] = [
8570
'message' => '#^Strict comparison using \\!\\=\\= between mixed and null will always evaluate to true\\.$#',
8671
'count' => 1,
@@ -141,16 +126,6 @@
141126
'count' => 1,
142127
'path' => __DIR__ . '/system/CLI/CLI.php',
143128
];
144-
$ignoreErrors[] = [
145-
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
146-
'count' => 1,
147-
'path' => __DIR__ . '/system/CLI/CLI.php',
148-
];
149-
$ignoreErrors[] = [
150-
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
151-
'count' => 1,
152-
'path' => __DIR__ . '/system/CLI/CLI.php',
153-
];
154129
$ignoreErrors[] = [
155130
'message' => '#^Only booleans are allowed in \\|\\|, string given on the left side\\.$#',
156131
'count' => 1,
@@ -171,11 +146,6 @@
171146
'count' => 5,
172147
'path' => __DIR__ . '/system/CLI/CLI.php',
173148
];
174-
$ignoreErrors[] = [
175-
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
176-
'count' => 1,
177-
'path' => __DIR__ . '/system/CLI/Commands.php',
178-
];
179149
$ignoreErrors[] = [
180150
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
181151
'count' => 2,
@@ -211,26 +181,11 @@
211181
'count' => 1,
212182
'path' => __DIR__ . '/system/Cache/Handlers/PredisHandler.php',
213183
];
214-
$ignoreErrors[] = [
215-
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
216-
'count' => 1,
217-
'path' => __DIR__ . '/system/Cache/Handlers/PredisHandler.php',
218-
];
219-
$ignoreErrors[] = [
220-
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
221-
'count' => 1,
222-
'path' => __DIR__ . '/system/Cache/Handlers/RedisHandler.php',
223-
];
224184
$ignoreErrors[] = [
225185
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
226186
'count' => 6,
227187
'path' => __DIR__ . '/system/CodeIgniter.php',
228188
];
229-
$ignoreErrors[] = [
230-
'message' => '#^Only booleans are allowed in an if condition, CodeIgniter\\\\HTTP\\\\ResponseInterface\\|null given\\.$#',
231-
'count' => 1,
232-
'path' => __DIR__ . '/system/CodeIgniter.php',
233-
];
234189
$ignoreErrors[] = [
235190
'message' => '#^Property CodeIgniter\\\\CodeIgniter\\:\\:\\$controller type has no signature specified for Closure\\.$#',
236191
'count' => 1,
@@ -971,11 +926,6 @@
971926
'count' => 2,
972927
'path' => __DIR__ . '/system/Config/Factories.php',
973928
];
974-
$ignoreErrors[] = [
975-
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
976-
'count' => 2,
977-
'path' => __DIR__ . '/system/Config/Factories.php',
978-
];
979929
$ignoreErrors[] = [
980930
'message' => '#^Accessing offset \'SERVER_PROTOCOL\' directly on \\$_SERVER is discouraged\\.$#',
981931
'count' => 1,
@@ -1926,31 +1876,11 @@
19261876
'count' => 2,
19271877
'path' => __DIR__ . '/system/Debug/ExceptionHandler.php',
19281878
];
1929-
$ignoreErrors[] = [
1930-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
1931-
'count' => 1,
1932-
'path' => __DIR__ . '/system/Debug/ExceptionHandler.php',
1933-
];
19341879
$ignoreErrors[] = [
19351880
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
19361881
'count' => 3,
19371882
'path' => __DIR__ . '/system/Debug/Exceptions.php',
19381883
];
1939-
$ignoreErrors[] = [
1940-
'message' => '#^Only booleans are allowed in an if condition, Throwable\\|null given\\.$#',
1941-
'count' => 1,
1942-
'path' => __DIR__ . '/system/Debug/Exceptions.php',
1943-
];
1944-
$ignoreErrors[] = [
1945-
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
1946-
'count' => 1,
1947-
'path' => __DIR__ . '/system/Debug/Exceptions.php',
1948-
];
1949-
$ignoreErrors[] = [
1950-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
1951-
'count' => 1,
1952-
'path' => __DIR__ . '/system/Debug/Exceptions.php',
1953-
];
19541884
$ignoreErrors[] = [
19551885
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
19561886
'count' => 1,
@@ -2041,11 +1971,6 @@
20411971
'count' => 2,
20421972
'path' => __DIR__ . '/system/Encryption/Encryption.php',
20431973
];
2044-
$ignoreErrors[] = [
2045-
'message' => '#^Only booleans are allowed in an if condition, Config\\\\Encryption\\|null given\\.$#',
2046-
'count' => 1,
2047-
'path' => __DIR__ . '/system/Encryption/Encryption.php',
2048-
];
20491974
$ignoreErrors[] = [
20501975
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
20511976
'count' => 2,
@@ -2166,11 +2091,6 @@
21662091
'count' => 1,
21672092
'path' => __DIR__ . '/system/HTTP/CURLRequest.php',
21682093
];
2169-
$ignoreErrors[] = [
2170-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
2171-
'count' => 1,
2172-
'path' => __DIR__ . '/system/HTTP/CURLRequest.php',
2173-
];
21742094
$ignoreErrors[] = [
21752095
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
21762096
'count' => 1,
@@ -2426,11 +2346,6 @@
24262346
'count' => 4,
24272347
'path' => __DIR__ . '/system/HTTP/SiteURI.php',
24282348
];
2429-
$ignoreErrors[] = [
2430-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
2431-
'count' => 1,
2432-
'path' => __DIR__ . '/system/HTTP/SiteURI.php',
2433-
];
24342349
$ignoreErrors[] = [
24352350
'message' => '#^Strict comparison using \\!\\=\\= between mixed and null will always evaluate to true\\.$#',
24362351
'count' => 1,
@@ -2441,16 +2356,6 @@
24412356
'count' => 15,
24422357
'path' => __DIR__ . '/system/HTTP/URI.php',
24432358
];
2444-
$ignoreErrors[] = [
2445-
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
2446-
'count' => 1,
2447-
'path' => __DIR__ . '/system/HTTP/URI.php',
2448-
];
2449-
$ignoreErrors[] = [
2450-
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
2451-
'count' => 2,
2452-
'path' => __DIR__ . '/system/HTTP/URI.php',
2453-
];
24542359
$ignoreErrors[] = [
24552360
'message' => '#^Property CodeIgniter\\\\HTTP\\\\URI\\:\\:\\$fragment \\(string\\) on left side of \\?\\? is not nullable\\.$#',
24562361
'count' => 1,
@@ -2621,11 +2526,6 @@
26212526
'count' => 2,
26222527
'path' => __DIR__ . '/system/Helpers/test_helper.php',
26232528
];
2624-
$ignoreErrors[] = [
2625-
'message' => '#^Only booleans are allowed in an if condition, array\\|null given\\.$#',
2626-
'count' => 1,
2627-
'path' => __DIR__ . '/system/Helpers/test_helper.php',
2628-
];
26292529
$ignoreErrors[] = [
26302530
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
26312531
'count' => 3,
@@ -2786,11 +2686,6 @@
27862686
'count' => 1,
27872687
'path' => __DIR__ . '/system/Log/Logger.php',
27882688
];
2789-
$ignoreErrors[] = [
2790-
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
2791-
'count' => 1,
2792-
'path' => __DIR__ . '/system/Log/Logger.php',
2793-
];
27942689
$ignoreErrors[] = [
27952690
'message' => '#^Parameter \\#1 \\$level \\(string\\) of method CodeIgniter\\\\Log\\\\Logger\\:\\:log\\(\\) should be contravariant with parameter \\$level \\(mixed\\) of method Psr\\\\Log\\\\LoggerInterface\\:\\:log\\(\\)$#',
27962691
'count' => 1,
@@ -2848,11 +2743,6 @@
28482743
];
28492744
$ignoreErrors[] = [
28502745
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
2851-
'count' => 2,
2852-
'path' => __DIR__ . '/system/Pager/Pager.php',
2853-
];
2854-
$ignoreErrors[] = [
2855-
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
28562746
'count' => 1,
28572747
'path' => __DIR__ . '/system/Pager/Pager.php',
28582748
];
@@ -3646,11 +3536,6 @@
36463536
'count' => 1,
36473537
'path' => __DIR__ . '/system/Test/FeatureTestCase.php',
36483538
];
3649-
$ignoreErrors[] = [
3650-
'message' => '#^Only booleans are allowed in an if condition, array\\|null given\\.$#',
3651-
'count' => 1,
3652-
'path' => __DIR__ . '/system/Test/FeatureTestCase.php',
3653-
];
36543539
$ignoreErrors[] = [
36553540
'message' => '#^Property CodeIgniter\\\\Test\\\\CIUnitTestCase\\:\\:\\$bodyFormat \\(string\\) in isset\\(\\) is not nullable\\.$#',
36563541
'count' => 1,
@@ -4156,11 +4041,6 @@
41564041
'count' => 1,
41574042
'path' => __DIR__ . '/system/Validation/Validation.php',
41584043
];
4159-
$ignoreErrors[] = [
4160-
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
4161-
'count' => 2,
4162-
'path' => __DIR__ . '/system/Validation/Validation.php',
4163-
];
41644044
$ignoreErrors[] = [
41654045
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
41664046
'count' => 2,
@@ -4231,11 +4111,6 @@
42314111
'count' => 3,
42324112
'path' => __DIR__ . '/system/View/View.php',
42334113
];
4234-
$ignoreErrors[] = [
4235-
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
4236-
'count' => 2,
4237-
'path' => __DIR__ . '/system/View/View.php',
4238-
];
42394114
$ignoreErrors[] = [
42404115
'message' => '#^Parameter \\#2 \\$context \\(\'attr\'\\|\'css\'\\|\'html\'\\|\'js\'\\|\'raw\'\\|\'url\'\\|null\\) of method CodeIgniter\\\\View\\\\View\\:\\:setData\\(\\) should be contravariant with parameter \\$context \\(string\\|null\\) of method CodeIgniter\\\\View\\\\RendererInterface\\:\\:setData\\(\\)$#',
42414116
'count' => 1,

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
4242
use Rector\Set\ValueObject\LevelSetList;
4343
use Rector\Set\ValueObject\SetList;
44+
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
4445
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
4546
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
4647
use Utils\Rector\RemoveVarTagFromClassConstantRector;
@@ -140,4 +141,5 @@
140141
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
141142
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
142143
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
144+
$rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class);
143145
};

system/Autoloader/FileLocator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
205205
*/
206206
protected function ensureExt(string $path, string $ext): string
207207
{
208-
if ($ext) {
208+
if ($ext !== '') {
209209
$ext = '.' . $ext;
210210

211211
if (substr($path, -strlen($ext)) !== $ext) {

system/BaseModel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ public function paginate(?int $perPage = null, string $group = 'default', ?int $
11941194
// Since multiple models may use the Pager, the Pager must be shared.
11951195
$pager = Services::pager();
11961196

1197-
if ($segment) {
1197+
if ($segment !== 0) {
11981198
$pager->setSegment($segment, $group);
11991199
}
12001200

@@ -1656,8 +1656,10 @@ protected function objectToArray($data, bool $onlyChanged = true, bool $recursiv
16561656
{
16571657
$properties = $this->objectToRawArray($data, $onlyChanged, $recursive);
16581658

1659+
assert(is_array($properties));
1660+
16591661
// Convert any Time instances to appropriate $dateFormat
1660-
if ($properties) {
1662+
if ($properties !== []) {
16611663
$properties = array_map(function ($value) {
16621664
if ($value instanceof Time) {
16631665
return $this->timeToDate($value);

system/CLI/CLI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static function prompt(string $field, $options = null, $validation = null
267267
// Read the input from keyboard.
268268
$input = trim(static::input()) ?: $default;
269269

270-
if ($validation) {
270+
if ($validation !== []) {
271271
while (! static::validate('"' . trim($field) . '"', $input, $validation)) {
272272
$input = static::prompt($field, $options, $validation);
273273
}
@@ -1088,7 +1088,7 @@ public static function table(array $tbody, array $thead = [])
10881088
foreach ($tableRows[$row] as $col) {
10891089
$diff = $maxColsLengths[$column] - static::strlen($col);
10901090

1091-
if ($diff) {
1091+
if ($diff !== 0) {
10921092
$tableRows[$row][$column] .= str_repeat(' ', $diff);
10931093
}
10941094

system/CLI/Commands.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ public function verifyCommand(string $command, array $commands): bool
146146

147147
$message = lang('CLI.commandNotFound', [$command]);
148148

149-
if ($alternatives = $this->getCommandAlternatives($command, $commands)) {
149+
$alternatives = $this->getCommandAlternatives($command, $commands);
150+
if ($alternatives !== []) {
150151
if (count($alternatives) === 1) {
151152
$message .= "\n\n" . lang('CLI.altCommandSingular') . "\n ";
152153
} else {

system/Cache/Handlers/PredisHandler.php

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

136-
if ($ttl) {
136+
if ($ttl !== 0) {
137137
$this->redis->expireat($key, Time::now()->getTimestamp() + $ttl);
138138
}
139139

system/Cache/Handlers/RedisHandler.php

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

162-
if ($ttl) {
162+
if ($ttl !== 0) {
163163
$this->redis->expireAt($key, Time::now()->getTimestamp() + $ttl);
164164
}
165165

system/CodeIgniter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ protected function forceSecureAccess($duration = 31_536_000)
708708
*/
709709
public function displayCache(Cache $config)
710710
{
711-
if ($cachedResponse = $this->pageCache->get($this->request, $this->response)) {
711+
$cachedResponse = $this->pageCache->get($this->request, $this->response);
712+
if ($cachedResponse instanceof ResponseInterface) {
712713
$this->response = $cachedResponse;
713714

714715
$this->totalTime = $this->benchmark->getElapsedTime('total_execution');

system/Config/Factories.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ public static function __callStatic(string $component, array $arguments)
145145
}
146146

147147
// Try to locate the class
148-
if ($class = self::locateClass($options, $alias)) {
148+
$class = self::locateClass($options, $alias);
149+
if ($class !== null) {
149150
return new $class(...$arguments);
150151
}
151152

@@ -422,7 +423,7 @@ public static function setOptions(string $component, array $values): array
422423
*/
423424
public static function reset(?string $component = null)
424425
{
425-
if ($component) {
426+
if ($component !== null) {
426427
unset(
427428
static::$options[$component],
428429
static::$aliases[$component],

0 commit comments

Comments
 (0)