Skip to content

Commit

Permalink
Merge pull request #7951 from samsonasik/boolean-in-if
Browse files Browse the repository at this point in the history
[Rector] Apply BooleanInIfConditionRuleFixerRector
  • Loading branch information
kenjis authored Oct 16, 2023
2 parents 08458d3 + 31ed124 commit 431748b
Show file tree
Hide file tree
Showing 23 changed files with 38 additions and 156 deletions.
125 changes: 0 additions & 125 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Autoloader/FileLocator.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Autoloader/FileLocator.php',
];
$ignoreErrors[] = [
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
'count' => 6,
Expand Down Expand Up @@ -71,16 +66,6 @@
'count' => 1,
'path' => __DIR__ . '/system/BaseModel.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/BaseModel.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/BaseModel.php',
];
$ignoreErrors[] = [
'message' => '#^Strict comparison using \\!\\=\\= between mixed and null will always evaluate to true\\.$#',
'count' => 1,
Expand Down Expand Up @@ -141,16 +126,6 @@
'count' => 1,
'path' => __DIR__ . '/system/CLI/CLI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/CLI/CLI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/CLI/CLI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in \\|\\|, string given on the left side\\.$#',
'count' => 1,
Expand All @@ -171,11 +146,6 @@
'count' => 5,
'path' => __DIR__ . '/system/CLI/CLI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/CLI/Commands.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 2,
Expand Down Expand Up @@ -211,26 +181,11 @@
'count' => 1,
'path' => __DIR__ . '/system/Cache/Handlers/PredisHandler.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Cache/Handlers/PredisHandler.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Cache/Handlers/RedisHandler.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 6,
'path' => __DIR__ . '/system/CodeIgniter.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, CodeIgniter\\\\HTTP\\\\ResponseInterface\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/CodeIgniter.php',
];
$ignoreErrors[] = [
'message' => '#^Property CodeIgniter\\\\CodeIgniter\\:\\:\\$controller type has no signature specified for Closure\\.$#',
'count' => 1,
Expand Down Expand Up @@ -971,11 +926,6 @@
'count' => 2,
'path' => __DIR__ . '/system/Config/Factories.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
'count' => 2,
'path' => __DIR__ . '/system/Config/Factories.php',
];
$ignoreErrors[] = [
'message' => '#^Accessing offset \'SERVER_PROTOCOL\' directly on \\$_SERVER is discouraged\\.$#',
'count' => 1,
Expand Down Expand Up @@ -1926,31 +1876,11 @@
'count' => 2,
'path' => __DIR__ . '/system/Debug/ExceptionHandler.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Debug/ExceptionHandler.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 3,
'path' => __DIR__ . '/system/Debug/Exceptions.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, Throwable\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Debug/Exceptions.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Debug/Exceptions.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Debug/Exceptions.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 1,
Expand Down Expand Up @@ -2041,11 +1971,6 @@
'count' => 2,
'path' => __DIR__ . '/system/Encryption/Encryption.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, Config\\\\Encryption\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Encryption/Encryption.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 2,
Expand Down Expand Up @@ -2166,11 +2091,6 @@
'count' => 1,
'path' => __DIR__ . '/system/HTTP/CURLRequest.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/HTTP/CURLRequest.php',
];
$ignoreErrors[] = [
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
'count' => 1,
Expand Down Expand Up @@ -2426,11 +2346,6 @@
'count' => 4,
'path' => __DIR__ . '/system/HTTP/SiteURI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/HTTP/SiteURI.php',
];
$ignoreErrors[] = [
'message' => '#^Strict comparison using \\!\\=\\= between mixed and null will always evaluate to true\\.$#',
'count' => 1,
Expand All @@ -2441,16 +2356,6 @@
'count' => 15,
'path' => __DIR__ . '/system/HTTP/URI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/HTTP/URI.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
'count' => 2,
'path' => __DIR__ . '/system/HTTP/URI.php',
];
$ignoreErrors[] = [
'message' => '#^Property CodeIgniter\\\\HTTP\\\\URI\\:\\:\\$fragment \\(string\\) on left side of \\?\\? is not nullable\\.$#',
'count' => 1,
Expand Down Expand Up @@ -2621,11 +2526,6 @@
'count' => 2,
'path' => __DIR__ . '/system/Helpers/test_helper.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Helpers/test_helper.php',
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 3,
Expand Down Expand Up @@ -2786,11 +2686,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Log/Logger.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Log/Logger.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#1 \\$level \\(string\\) of method CodeIgniter\\\\Log\\\\Logger\\:\\:log\\(\\) should be contravariant with parameter \\$level \\(mixed\\) of method Psr\\\\Log\\\\LoggerInterface\\:\\:log\\(\\)$#',
'count' => 1,
Expand Down Expand Up @@ -2848,11 +2743,6 @@
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
'count' => 2,
'path' => __DIR__ . '/system/Pager/Pager.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, int given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Pager/Pager.php',
];
Expand Down Expand Up @@ -3646,11 +3536,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Test/FeatureTestCase.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array\\|null given\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Test/FeatureTestCase.php',
];
$ignoreErrors[] = [
'message' => '#^Property CodeIgniter\\\\Test\\\\CIUnitTestCase\\:\\:\\$bodyFormat \\(string\\) in isset\\(\\) is not nullable\\.$#',
'count' => 1,
Expand Down Expand Up @@ -4156,11 +4041,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Validation/Validation.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, array given\\.$#',
'count' => 2,
'path' => __DIR__ . '/system/Validation/Validation.php',
];
$ignoreErrors[] = [
'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#',
'count' => 2,
Expand Down Expand Up @@ -4231,11 +4111,6 @@
'count' => 3,
'path' => __DIR__ . '/system/View/View.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in an if condition, string\\|null given\\.$#',
'count' => 2,
'path' => __DIR__ . '/system/View/View.php',
];
$ignoreErrors[] = [
'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\\(\\)$#',
'count' => 1,
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
use Utils\Rector\RemoveVarTagFromClassConstantRector;
Expand Down Expand Up @@ -140,4 +141,5 @@
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
$rectorConfig->rule(BooleanInIfConditionRuleFixerRector::class);
};
2 changes: 1 addition & 1 deletion system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
*/
protected function ensureExt(string $path, string $ext): string
{
if ($ext) {
if ($ext !== '') {
$ext = '.' . $ext;

if (substr($path, -strlen($ext)) !== $ext) {
Expand Down
6 changes: 4 additions & 2 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ public function paginate(?int $perPage = null, string $group = 'default', ?int $
// Since multiple models may use the Pager, the Pager must be shared.
$pager = Services::pager();

if ($segment) {
if ($segment !== 0) {
$pager->setSegment($segment, $group);
}

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

assert(is_array($properties));

// Convert any Time instances to appropriate $dateFormat
if ($properties) {
if ($properties !== []) {
$properties = array_map(function ($value) {
if ($value instanceof Time) {
return $this->timeToDate($value);
Expand Down
4 changes: 2 additions & 2 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static function prompt(string $field, $options = null, $validation = null
// Read the input from keyboard.
$input = trim(static::input()) ?: $default;

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

if ($diff) {
if ($diff !== 0) {
$tableRows[$row][$column] .= str_repeat(' ', $diff);
}

Expand Down
3 changes: 2 additions & 1 deletion system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public function verifyCommand(string $command, array $commands): bool

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

if ($alternatives = $this->getCommandAlternatives($command, $commands)) {
$alternatives = $this->getCommandAlternatives($command, $commands);
if ($alternatives !== []) {
if (count($alternatives) === 1) {
$message .= "\n\n" . lang('CLI.altCommandSingular') . "\n ";
} else {
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function save(string $key, $value, int $ttl = 60)
return false;
}

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

Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function save(string $key, $value, int $ttl = 60)
return false;
}

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

Expand Down
3 changes: 2 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ protected function forceSecureAccess($duration = 31_536_000)
*/
public function displayCache(Cache $config)
{
if ($cachedResponse = $this->pageCache->get($this->request, $this->response)) {
$cachedResponse = $this->pageCache->get($this->request, $this->response);
if ($cachedResponse instanceof ResponseInterface) {
$this->response = $cachedResponse;

$this->totalTime = $this->benchmark->getElapsedTime('total_execution');
Expand Down
5 changes: 3 additions & 2 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public static function __callStatic(string $component, array $arguments)
}

// Try to locate the class
if ($class = self::locateClass($options, $alias)) {
$class = self::locateClass($options, $alias);
if ($class !== null) {
return new $class(...$arguments);
}

Expand Down Expand Up @@ -422,7 +423,7 @@ public static function setOptions(string $component, array $values): array
*/
public static function reset(?string $component = null)
{
if ($component) {
if ($component !== null) {
unset(
static::$options[$component],
static::$aliases[$component],
Expand Down
2 changes: 1 addition & 1 deletion system/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function determineView(Throwable $exception, string $templatePath): st
// Production environments should have a custom exception file.
$view = 'production.php';

if (str_ireplace(['off', 'none', 'no', 'false', 'null'], '', ini_get('display_errors'))) {
if (str_ireplace(['off', 'none', 'no', 'false', 'null'], '', ini_get('display_errors')) !== '') {
$view = 'error_exception.php';
}

Expand Down
Loading

0 comments on commit 431748b

Please sign in to comment.