diff --git a/app/Views/errors/cli/error_exception.php b/app/Views/errors/cli/error_exception.php index 9f47d25141d2..2bf1459d4094 100644 --- a/app/Views/errors/cli/error_exception.php +++ b/app/Views/errors/cli/error_exception.php @@ -52,7 +52,7 @@ $args = implode(', ', array_map(static fn ($value) => match (true) { is_object($value) => 'Object(' . $value::class . ')', - is_array($value) => count($value) ? '[...]' : '[]', + is_array($value) => $value !== [] ? '[...]' : '[]', $value === null => 'null', // return the lowercased version default => var_export($value, true), }, array_values($error['args'] ?? []))); diff --git a/rector.php b/rector.php index e7c98746eb86..ee8621691862 100644 --- a/rector.php +++ b/rector.php @@ -38,7 +38,6 @@ use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector; -use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector; @@ -56,11 +55,8 @@ return RectorConfig::configure() ->withPhpSets(php81: true) - ->withPreparedSets(deadCode: true) - ->withSets([ - PHPUnitSetList::PHPUNIT_CODE_QUALITY, - PHPUnitSetList::PHPUNIT_100, - ]) + ->withPreparedSets(deadCode: true, instanceOf: true, strictBooleans: true, phpunitCodeQuality: true) + ->withComposerBased(phpunit: true) ->withParallel(120, 8, 10) ->withCache( // Github action cache or local diff --git a/system/CLI/CLI.php b/system/CLI/CLI.php index 236e5b685fff..1a349d4d3a4c 100644 --- a/system/CLI/CLI.php +++ b/system/CLI/CLI.php @@ -230,7 +230,7 @@ public static function prompt(string $field, $options = null, $validation = null } if (! is_array($validation)) { - $validation = $validation ? explode('|', $validation) : []; + $validation = $validation !== null && $validation !== '' && $validation !== '0' ? explode('|', $validation) : []; } if (is_string($options)) { @@ -348,7 +348,7 @@ public static function promptByMultipleKeys(string $text, array $options): array // return the prompt again if $input contain(s) non-numeric character, except a comma. // And if max from $options less than max from input, // it means user tried to access null value in $options - if (! $pattern || $maxOptions < $maxInput) { + if ($pattern === 0 || $pattern === false || $maxOptions < $maxInput) { static::error('Please select correctly.'); CLI::newLine(); diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index fd0630a9982c..ea7c00e382e2 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -308,7 +308,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs if ($filename !== '.' && $filename !== '..') { if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.') { $this->deleteFiles($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1); - } elseif (! $htdocs || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) { + } elseif (! $htdocs || in_array(preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename), [0, false], true)) { @unlink($path . DIRECTORY_SEPARATOR . $filename); } } diff --git a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php index e08a16ff7a60..2755a389ffc3 100644 --- a/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php +++ b/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php @@ -221,7 +221,7 @@ private function getRouteForDefaultController( if ($classShortname === $defaultController) { $pattern = '#' . preg_quote(lcfirst($defaultController), '#') . '\z#'; $routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/'); - $routeWithoutController = $routeWithoutController ?: '/'; + $routeWithoutController = $routeWithoutController !== '' && $routeWithoutController !== '0' ? $routeWithoutController : '/'; [$params, $routeParams] = $this->getParameters($method); diff --git a/system/Commands/Utilities/Routes/ControllerMethodReader.php b/system/Commands/Utilities/Routes/ControllerMethodReader.php index c443b669465a..b40a832b4f48 100644 --- a/system/Commands/Utilities/Routes/ControllerMethodReader.php +++ b/system/Commands/Utilities/Routes/ControllerMethodReader.php @@ -161,7 +161,7 @@ private function getRouteWithoutController( $pattern = '#' . preg_quote(lcfirst($defaultController), '#') . '\z#'; $routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/'); - $routeWithoutController = $routeWithoutController ?: '/'; + $routeWithoutController = $routeWithoutController !== '' && $routeWithoutController !== '0' ? $routeWithoutController : '/'; return [[ 'route' => $routeWithoutController, diff --git a/system/Config/DotEnv.php b/system/Config/DotEnv.php index db7152fd09d5..9c120afec523 100644 --- a/system/Config/DotEnv.php +++ b/system/Config/DotEnv.php @@ -94,7 +94,7 @@ public function parse(): ?array */ protected function setVariable(string $name, string $value = '') { - if (! getenv($name, true)) { + if (in_array(getenv($name, true), ['', '0'], true) || getenv($name, true) === [] || getenv($name, true) === false) { putenv("{$name}={$value}"); } diff --git a/system/Config/Services.php b/system/Config/Services.php index 61026de4292c..aa8c180a3388 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -345,7 +345,7 @@ public static function image(?string $handler = null, ?Images $config = null, bo $config ??= config(Images::class); assert($config instanceof Images); - $handler = $handler ?: $config->defaultHandler; + $handler = $handler !== null && $handler !== '' && $handler !== '0' ? $handler : $config->defaultHandler; $class = $config->handlers[$handler]; return new $class($config); @@ -385,7 +385,7 @@ public static function language(?string $locale = null, bool $getShared = true) } // Use '?:' for empty string check - $locale = $locale ?: $requestLocale; + $locale = $locale !== null && $locale !== '' && $locale !== '0' ? $locale : $requestLocale; return new Language($locale); } @@ -484,7 +484,7 @@ public static function parser(?string $viewPath = null, ?ViewConfig $config = nu return static::getSharedInstance('parser', $viewPath, $config); } - $viewPath = $viewPath ?: (new Paths())->viewDirectory; + $viewPath = $viewPath !== null && $viewPath !== '' && $viewPath !== '0' ? $viewPath : (new Paths())->viewDirectory; $config ??= config(ViewConfig::class); return new Parser($config, $viewPath, AppServices::get('locator'), CI_DEBUG, AppServices::get('logger')); @@ -503,7 +503,7 @@ public static function renderer(?string $viewPath = null, ?ViewConfig $config = return static::getSharedInstance('renderer', $viewPath, $config); } - $viewPath = $viewPath ?: (new Paths())->viewDirectory; + $viewPath = $viewPath !== null && $viewPath !== '' && $viewPath !== '0' ? $viewPath : (new Paths())->viewDirectory; $config ??= config(ViewConfig::class); return new View($config, $viewPath, AppServices::get('locator'), CI_DEBUG, AppServices::get('logger')); diff --git a/system/Cookie/Cookie.php b/system/Cookie/Cookie.php index 01b10db19b65..d75cce26ac09 100644 --- a/system/Cookie/Cookie.php +++ b/system/Cookie/Cookie.php @@ -482,7 +482,7 @@ public function withNeverExpiring() */ public function withPath(?string $path) { - $path = $path ?: self::$defaults['path']; + $path = $path !== null && $path !== '' && $path !== '0' ? $path : self::$defaults['path']; $this->validatePrefix($this->prefix, $this->secure, $path, $this->domain); $cookie = clone $this; diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index e52baa39f570..2b947ce952e2 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -1736,7 +1736,7 @@ public function countAllResults(bool $reset = true) // Restore the LIMIT setting $this->QBLimit = $limit; - $row = ! $result instanceof ResultInterface ? null : $result->getRow(); + $row = $result instanceof ResultInterface ? $result->getRow() : null; if (empty($row)) { return 0; @@ -3167,11 +3167,11 @@ protected function compileWhereHaving(string $qbKey): string $op = $this->getOperator($condition); if ( $op === false - || ! preg_match( + || in_array(preg_match( '/^(\(?)(.*)(' . preg_quote($op, '/') . ')\s*(.*(?queryClass; + $queryClass = $queryClass !== '' && $queryClass !== '0' ? $queryClass : $this->queryClass; if (empty($this->connID)) { $this->initialize(); diff --git a/system/Database/Database.php b/system/Database/Database.php index 23b6cd0f8d16..3d4840f2e595 100644 --- a/system/Database/Database.php +++ b/system/Database/Database.php @@ -96,7 +96,7 @@ protected function parseDSN(array $params): array { $dsn = parse_url($params['DSN']); - if (! $dsn) { + if ($dsn === 0 || ($dsn === '' || $dsn === '0') || $dsn === [] || $dsn === false || $dsn === null) { throw new InvalidArgumentException('Your DSN connection string is invalid.'); } diff --git a/system/Database/MigrationRunner.php b/system/Database/MigrationRunner.php index cf736171a7cf..bd94607d9105 100644 --- a/system/Database/MigrationRunner.php +++ b/system/Database/MigrationRunner.php @@ -450,7 +450,7 @@ protected function migrationFromFile(string $path, string $namespace) $filename = basename($path, '.php'); - if (! preg_match($this->regex, $filename)) { + if (in_array(preg_match($this->regex, $filename), [0, false], true)) { return false; } @@ -524,7 +524,7 @@ protected function getMigrationNumber(string $migration): string { preg_match($this->regex, $migration, $matches); - return count($matches) ? $matches[1] : '0'; + return $matches !== [] ? $matches[1] : '0'; } /** @@ -539,7 +539,7 @@ protected function getMigrationName(string $migration): string { preg_match($this->regex, $migration, $matches); - return count($matches) ? $matches[2] : ''; + return $matches !== [] ? $matches[2] : ''; } /** @@ -725,7 +725,7 @@ public function getBatchStart(int $batch): string ->get() ->getResultObject(); - return count($migration) ? $migration[0]->version : '0'; + return count($migration) !== 0 ? $migration[0]->version : '0'; } /** diff --git a/system/Database/MySQLi/Forge.php b/system/Database/MySQLi/Forge.php index 3bde0c518daf..54c073389fd3 100644 --- a/system/Database/MySQLi/Forge.php +++ b/system/Database/MySQLi/Forge.php @@ -116,11 +116,11 @@ protected function _createTableAttributes(array $attributes): string } } - if ($this->db->charset !== '' && ! strpos($sql, 'CHARACTER SET') && ! strpos($sql, 'CHARSET')) { + if ($this->db->charset !== '' && in_array(strpos($sql, 'CHARACTER SET'), [0, false], true) && in_array(strpos($sql, 'CHARSET'), [0, false], true)) { $sql .= ' DEFAULT CHARACTER SET = ' . $this->db->escapeString($this->db->charset); } - if ($this->db->DBCollat !== '' && ! strpos($sql, 'COLLATE')) { + if ($this->db->DBCollat !== '' && in_array(strpos($sql, 'COLLATE'), [0, false], true)) { $sql .= ' COLLATE = ' . $this->db->escapeString($this->db->DBCollat); } diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php index 2826ab1bf53c..8d2edc94296c 100644 --- a/system/Database/Postgre/Connection.php +++ b/system/Database/Postgre/Connection.php @@ -463,7 +463,7 @@ public function error(): array { return [ 'code' => '', - 'message' => pg_last_error($this->connID) ?: '', + 'message' => ! in_array(pg_last_error($this->connID), ['', '0'], true) ? pg_last_error($this->connID) : '', ]; } diff --git a/system/Email/Email.php b/system/Email/Email.php index b82779eb891d..a88a067763af 100644 --- a/system/Email/Email.php +++ b/system/Email/Email.php @@ -495,7 +495,7 @@ public function setFrom($from, $name = '', $returnPath = null) if ($name !== '') { // only use Q encoding if there are characters that would require it - if (! preg_match('/[\200-\377]/', $name)) { + if (in_array(preg_match('/[\200-\377]/', $name), [0, false], true)) { $name = '"' . addcslashes($name, "\0..\37\177'\"\\") . '"'; } else { $name = $this->prepQEncoding($name); @@ -532,7 +532,7 @@ public function setReplyTo($replyto, $name = '') $this->tmpArchive['replyName'] = $name; // only use Q encoding if there are characters that would require it - if (! preg_match('/[\200-\377]/', $name)) { + if (in_array(preg_match('/[\200-\377]/', $name), [0, false], true)) { $name = '"' . addcslashes($name, "\0..\37\177'\"\\") . '"'; } else { $name = $this->prepQEncoding($name); diff --git a/system/HTTP/Files/UploadedFile.php b/system/HTTP/Files/UploadedFile.php index 78643aa7166e..0626aeb5581f 100644 --- a/system/HTTP/Files/UploadedFile.php +++ b/system/HTTP/Files/UploadedFile.php @@ -302,7 +302,7 @@ public function getTempName(): string */ public function getExtension(): string { - return $this->guessExtension() ?: $this->getClientExtension(); + return ! in_array($this->guessExtension(), ['', '0'], true) ? $this->guessExtension() : $this->getClientExtension(); } /** diff --git a/system/HTTP/ResponseTrait.php b/system/HTTP/ResponseTrait.php index 45f07d186170..9daad33d0e18 100644 --- a/system/HTTP/ResponseTrait.php +++ b/system/HTTP/ResponseTrait.php @@ -566,7 +566,7 @@ public function getCookieStore() */ public function hasCookie(string $name, ?string $value = null, string $prefix = ''): bool { - $prefix = $prefix ?: Cookie::setDefaults()['prefix']; // to retain BC + $prefix = $prefix !== '' && $prefix !== '0' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC return $this->cookieStore->has($name, $prefix, $value); } @@ -586,7 +586,7 @@ public function getCookie(?string $name = null, string $prefix = '') } try { - $prefix = $prefix ?: Cookie::setDefaults()['prefix']; // to retain BC + $prefix = $prefix !== '' && $prefix !== '0' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC return $this->cookieStore->get($name, $prefix); } catch (CookieException $e) { @@ -607,7 +607,7 @@ public function deleteCookie(string $name = '', string $domain = '', string $pat return $this; } - $prefix = $prefix ?: Cookie::setDefaults()['prefix']; // to retain BC + $prefix = $prefix !== '' && $prefix !== '0' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC $prefixed = $prefix . $name; $store = $this->cookieStore; diff --git a/system/HTTP/SiteURI.php b/system/HTTP/SiteURI.php index 91b14bc05456..2be70a7773ef 100644 --- a/system/HTTP/SiteURI.php +++ b/system/HTTP/SiteURI.php @@ -421,7 +421,7 @@ public function siteUrl($relativePath = '', ?string $scheme = null, ?App $config $relativePath = $this->stringifyRelativePath($relativePath); // Check current host. - $host = ! $config instanceof App ? $this->getHost() : null; + $host = $config instanceof App ? null : $this->getHost(); $config ??= config(App::class); diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index 4b0b49be6464..fded9f64ef20 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -166,7 +166,7 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false, continue; } - if (! $htdocs || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) { + if (! $htdocs || in_array(preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename), [0, false], true)) { $isDir = $object->isDir(); if ($isDir && $delDir) { rmdir($object->getPathname()); diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 58cd828e3faa..92651afb991c 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -62,7 +62,7 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites $before = service('filters')->getFilters()['before']; - if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && ! stripos($form, 'method="get"')) { + if ((in_array('csrf', $before, true) || array_key_exists('csrf', $before)) && str_contains($action, base_url()) && in_array(stripos($form, 'method="get"'), [0, false], true)) { $form .= csrf_field($csrfId ?? null); } @@ -788,7 +788,7 @@ function parse_form_attributes($attributes, array $default): string if (! is_bool($val)) { if ($key === 'value') { $val = esc($val); - } elseif ($key === 'name' && ! strlen($default['name'])) { + } elseif ($key === 'name' && (string) $default['name'] === '') { continue; } $att .= $key . '="' . $val . '"' . ($key === array_key_last($default) ? '' : ' '); diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 30733acd5175..736960606457 100644 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -112,7 +112,7 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string $img = ' $v) { - if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v)) { + if ($k === 'src' && in_array(preg_match('#^([a-z]+:)?//#i', $v), [0, false], true)) { if ($indexPage) { $script .= 'src="' . site_url($v) . '" '; } else { @@ -252,7 +252,7 @@ function link_tag( $href = $href['href'] ?? ''; } - if (! preg_match('#^([a-z]+:)?//#i', $href)) { + if (in_array(preg_match('#^([a-z]+:)?//#i', $href), [0, false], true)) { $attributes['href'] = $indexPage ? site_url($href) : slash_item('baseURL') . $href; } else { $attributes['href'] = $href; diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index 3de105736f57..e378cc67adb7 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -741,7 +741,7 @@ function excerpt(string $text, ?string $phrase = null, int $radius = 100, string $count = ++$count + strlen($s); } - $ellPre = $phrase ? $ellipsis : ''; + $ellPre = $phrase !== null && $phrase !== '' && $phrase !== '0' ? $ellipsis : ''; return str_replace(' ', ' ', $ellPre . $prev . $phrase . $post . $ellipsis); } diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index fefb2fc544a3..354b7542e755 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -73,7 +73,7 @@ trait TimeTrait */ public function __construct(?string $time = null, $timezone = null, ?string $locale = null) { - $this->locale = $locale ?: Locale::getDefault(); + $this->locale = $locale !== null && $locale !== '' && $locale !== '0' ? $locale : Locale::getDefault(); $time ??= ''; @@ -958,7 +958,7 @@ public function sameAs($testTime, ?string $timezone = null): bool if ($testTime instanceof DateTimeInterface) { $testTime = $testTime->format('Y-m-d H:i:s'); } elseif (is_string($testTime)) { - $timezone = $timezone ?: $this->timezone; + $timezone = $timezone !== null && $timezone !== '' && $timezone !== '0' ? $timezone : $this->timezone; $timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); $testTime = new DateTime($testTime, $timezone); $testTime = $testTime->format('Y-m-d H:i:s'); @@ -1108,7 +1108,7 @@ public function getUTCObject($time, ?string $timezone = null) if ($time instanceof self) { $time = $time->toDateTime(); } elseif (is_string($time)) { - $timezone = $timezone ?: $this->timezone; + $timezone = $timezone !== null && $timezone !== '' && $timezone !== '0' ? $timezone : $this->timezone; $timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone); $time = new DateTime($time, $timezone); } diff --git a/system/Images/Image.php b/system/Images/Image.php index ec6d4b8fa5c0..1f857b00f780 100644 --- a/system/Images/Image.php +++ b/system/Images/Image.php @@ -103,7 +103,7 @@ public function getProperties(bool $return = false) { $path = $this->getPathname(); - if (! $vals = getimagesize($path)) { + if ($vals = getimagesize($path) === [] || $vals = getimagesize($path) === false) { throw ImageException::forFileNotSupported(); } diff --git a/system/Router/AutoRouter.php b/system/Router/AutoRouter.php index bb47bf96c89a..19654adae1e2 100644 --- a/system/Router/AutoRouter.php +++ b/system/Router/AutoRouter.php @@ -105,7 +105,7 @@ public function getRoute(string $uri, string $httpVerb): array if ($httpVerb !== 'CLI') { $controller = '\\' . $this->defaultNamespace; - $controller .= $this->directory ? str_replace('/', '\\', $this->directory) : ''; + $controller .= $this->directory !== null && $this->directory !== '' && $this->directory !== '0' ? str_replace('/', '\\', $this->directory) : ''; $controller .= $controllerName; $controller = strtolower($controller); diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index ad5c01a4858f..fae2a7a2934a 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -609,7 +609,7 @@ public function getRoutesOptions(?string $from = null, ?string $verb = null): ar { $options = $this->loadRoutesOptions($verb); - return $from ? $options[$from] ?? [] : $options; + return $from !== null && $from !== '' && $from !== '0' ? $options[$from] ?? [] : $options; } /** @@ -1329,7 +1329,7 @@ protected function fillRouteParams(string $from, ?array $params = null): string $patterns = $matches[0]; foreach ($patterns as $index => $pattern) { - if (! preg_match('#^' . $pattern . '$#u', $params[$index])) { + if (in_array(preg_match('#^' . $pattern . '$#u', $params[$index]), [0, false], true)) { throw RouterException::forInvalidParameterType(); } @@ -1391,7 +1391,7 @@ protected function buildReverseRoute(string $from, array $params): string // or maybe $placeholder is not a placeholder, but a regex. $pattern = $this->placeholders[$placeholderName] ?? $placeholder; - if (! preg_match('#^' . $pattern . '$#u', (string) $params[$index])) { + if (in_array(preg_match('#^' . $pattern . '$#u', (string) $params[$index]), [0, false], true)) { throw RouterException::forInvalidParameterType(); } diff --git a/system/Session/Handlers/FileHandler.php b/system/Session/Handlers/FileHandler.php index 4dbec779526a..e0e52442569f 100644 --- a/system/Session/Handlers/FileHandler.php +++ b/system/Session/Handlers/FileHandler.php @@ -290,7 +290,7 @@ public function gc($max_lifetime) while (($file = readdir($directory)) !== false) { // If the filename doesn't match this pattern, it's either not a session file or is not ours - if (! preg_match($pattern, $file) + if (in_array(preg_match($pattern, $file), [0, false], true) || ! is_file($this->savePath . DIRECTORY_SEPARATOR . $file) || ($mtime = filemtime($this->savePath . DIRECTORY_SEPARATOR . $file)) === false || $mtime > $ts diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index 79bb4ca4613f..8e8b575aff31 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -93,12 +93,12 @@ public function open($path, $name): bool } if ( - ! preg_match_all( + in_array(preg_match_all( '#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#', $this->savePath, $matches, PREG_SET_ORDER - ) + ), [0, false], true) ) { $this->memcached = null; $this->logger->error('Session: Invalid Memcached save path format: ' . $this->savePath); diff --git a/system/Session/Session.php b/system/Session/Session.php index 60e4dbef4101..ca26c44df420 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -234,7 +234,7 @@ public function start() // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers if (isset($_COOKIE[$this->config->cookieName]) - && (! is_string($_COOKIE[$this->config->cookieName]) || ! preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$this->config->cookieName])) + && (! is_string($_COOKIE[$this->config->cookieName]) || in_array(preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$this->config->cookieName]), [0, false], true)) ) { unset($_COOKIE[$this->config->cookieName]); } diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index 62c0fff83844..faa1a0bd5fbe 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -374,7 +374,7 @@ public function assertLogContains(string $level, string $logMessage, string $mes { $this->assertTrue( TestLogger::didLog($level, $logMessage, false), - $message ?: sprintf( + $message !== '' && $message !== '0' ? $message : sprintf( 'Failed asserting that logs have a record of message containing "%s" with level "%s".', $logMessage, $level diff --git a/system/Typography/Typography.php b/system/Typography/Typography.php index c8ca5131171c..be2d0b36c7da 100644 --- a/system/Typography/Typography.php +++ b/system/Typography/Typography.php @@ -171,7 +171,7 @@ public function autoTypography(string $str, bool $reduceLinebreaks = false): str } // No opening block level tag? Add it if needed. - if (! preg_match('/^\s*<(?:' . $this->blockElements . ')/i', $str)) { + if (in_array(preg_match('/^\s*<(?:' . $this->blockElements . ')/i', $str), [0, false], true)) { $str = preg_replace('/^(.*?)<(' . $this->blockElements . ')/i', '
$1
<$2', $str); } diff --git a/system/Validation/Rules.php b/system/Validation/Rules.php index 715e2e51c7e1..7ee680d90698 100644 --- a/system/Validation/Rules.php +++ b/system/Validation/Rules.php @@ -140,7 +140,7 @@ public function is_not_unique($str, string $field, array $data): bool if ( $whereField !== null && $whereField !== '' && $whereValue !== null && $whereValue !== '' - && ! preg_match('/^\{(\w+)\}$/', $whereValue) + && in_array(preg_match('/^\{(\w+)\}$/', $whereValue), [0, false], true) ) { $row = $row->where($whereField, $whereValue); } @@ -198,7 +198,7 @@ public function is_unique($str, string $field, array $data): bool if ( $ignoreField !== null && $ignoreField !== '' && $ignoreValue !== null && $ignoreValue !== '' - && ! preg_match('/^\{(\w+)\}$/', $ignoreValue) + && in_array(preg_match('/^\{(\w+)\}$/', $ignoreValue), [0, false], true) ) { $row = $row->where("{$ignoreField} !=", $ignoreValue); } diff --git a/system/Validation/StrictRules/Rules.php b/system/Validation/StrictRules/Rules.php index ec02a4e0a0ac..3bc130bd93e4 100644 --- a/system/Validation/StrictRules/Rules.php +++ b/system/Validation/StrictRules/Rules.php @@ -164,7 +164,7 @@ public function is_not_unique($str, string $field, array $data): bool if ( $whereField !== null && $whereField !== '' && $whereValue !== null && $whereValue !== '' - && ! preg_match('/^\{(\w+)\}$/', $whereValue) + && in_array(preg_match('/^\{(\w+)\}$/', $whereValue), [0, false], true) ) { $row = $row->where($whereField, $whereValue); } @@ -224,7 +224,7 @@ public function is_unique($str, string $field, array $data): bool if ( $ignoreField !== null && $ignoreField !== '' && $ignoreValue !== null && $ignoreValue !== '' - && ! preg_match('/^\{(\w+)\}$/', $ignoreValue) + && in_array(preg_match('/^\{(\w+)\}$/', $ignoreValue), [0, false], true) ) { $row = $row->where("{$ignoreField} !=", $ignoreValue); } diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index a942a32bceea..c00329d670bb 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -181,7 +181,7 @@ public function run(?array $data = null, ?string $group = null, $dbGroup = null) ); // if keys not found - $values = $values ?: [$field => null]; + $values = $values !== [] ? $values : [$field => null]; } else { $values = dot_array_search($field, $data); } diff --git a/system/View/Parser.php b/system/View/Parser.php index 8a26cf825d3f..80af795bceb8 100644 --- a/system/View/Parser.php +++ b/system/View/Parser.php @@ -613,7 +613,7 @@ public function shouldAddEscaping(string $key) $escape = false; } // If no `esc` filter is found, then we'll need to add one. - elseif (! preg_match('/\s+esc/u', $key)) { + elseif (in_array(preg_match('/\s+esc/u', $key), [0, false], true)) { $escape = 'html'; } @@ -691,7 +691,7 @@ protected function parsePlugins(string $template) * $matches[1] = all parameters string in opening tag * $matches[2] = content between the tags to send to the plugin. */ - if (! preg_match_all($pattern, $template, $matches, PREG_SET_ORDER)) { + if (in_array(preg_match_all($pattern, $template, $matches, PREG_SET_ORDER), [0, false], true)) { continue; } diff --git a/utils/phpstan-baseline/booleanNot.exprNotBoolean.neon b/utils/phpstan-baseline/booleanNot.exprNotBoolean.neon index 41a36091437c..b645d7d0a2b4 100644 --- a/utils/phpstan-baseline/booleanNot.exprNotBoolean.neon +++ b/utils/phpstan-baseline/booleanNot.exprNotBoolean.neon @@ -1,4 +1,4 @@ -# total 13 errors +# total 12 errors parameters: ignoreErrors: @@ -27,11 +27,6 @@ parameters: count: 1 path: ../../system/Filters/Filters.php - - - message: '#^Only booleans are allowed in a negated boolean, int\<0, max\> given\.$#' - count: 1 - path: ../../system/Helpers/form_helper.php - - message: '#^Only booleans are allowed in a negated boolean, CodeIgniter\\CodeIgniter given\.$#' count: 1 diff --git a/utils/phpstan-baseline/identical.alwaysFalse.neon b/utils/phpstan-baseline/identical.alwaysFalse.neon new file mode 100644 index 000000000000..a048001eed03 --- /dev/null +++ b/utils/phpstan-baseline/identical.alwaysFalse.neon @@ -0,0 +1,8 @@ +# total 1 error + +parameters: + ignoreErrors: + - + message: '#^Strict comparison using \=\=\= between array\{0\: int\<0, max\>, 1\: int\<0, max\>, 2\: int, 3\: string, mime\: string, channels\?\: int, bits\?\: int\}\|false and array\{\} will always evaluate to false\.$#' + count: 1 + path: ../../system/Images/Image.php diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 40d901a7ab2d..c86c433953da 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -28,6 +28,7 @@ includes: - generator.returnType.neon - generator.valueType.neon - greaterOrEqual.invalid.neon + - identical.alwaysFalse.neon - if.condNotBoolean.neon - isset.offset.neon - isset.property.neon @@ -48,6 +49,7 @@ includes: - nullCoalesce.expr.neon - nullCoalesce.property.neon - nullCoalesce.variable.neon + - offsetAccess.nonOffsetAccessible.neon - offsetAccess.notFound.neon - parameterByRef.unusedType.neon - property.defaultValue.neon diff --git a/utils/phpstan-baseline/offsetAccess.nonOffsetAccessible.neon b/utils/phpstan-baseline/offsetAccess.nonOffsetAccessible.neon new file mode 100644 index 000000000000..9aa901319837 --- /dev/null +++ b/utils/phpstan-baseline/offsetAccess.nonOffsetAccessible.neon @@ -0,0 +1,23 @@ +# total 4 errors + +parameters: + ignoreErrors: + - + message: '#^Cannot access offset 0 on false\.$#' + count: 2 + path: ../../system/Images/Image.php + + - + message: '#^Cannot access offset 1 on false\.$#' + count: 2 + path: ../../system/Images/Image.php + + - + message: '#^Cannot access offset 2 on false\.$#' + count: 3 + path: ../../system/Images/Image.php + + - + message: '#^Cannot access offset 3 on false\.$#' + count: 2 + path: ../../system/Images/Image.php diff --git a/utils/phpstan-baseline/ternary.condNotBoolean.neon b/utils/phpstan-baseline/ternary.condNotBoolean.neon index 97d5bc785dac..8e0254deec41 100644 --- a/utils/phpstan-baseline/ternary.condNotBoolean.neon +++ b/utils/phpstan-baseline/ternary.condNotBoolean.neon @@ -1,4 +1,4 @@ -# total 8 errors +# total 3 errors parameters: ignoreErrors: @@ -7,16 +7,6 @@ parameters: count: 1 path: ../../system/BaseModel.php - - - message: '#^Only booleans are allowed in a ternary operator condition, string\|null given\.$#' - count: 1 - path: ../../system/CLI/CLI.php - - - - message: '#^Only booleans are allowed in a ternary operator condition, int\<0, max\> given\.$#' - count: 3 - path: ../../system/Database/MigrationRunner.php - - message: '#^Only booleans are allowed in a ternary operator condition, string\|null given\.$#' count: 1 @@ -26,18 +16,3 @@ parameters: message: '#^Only booleans are allowed in a ternary operator condition, int\|null given\.$#' count: 1 path: ../../system/HTTP/OutgoingRequest.php - - - - message: '#^Only booleans are allowed in a ternary operator condition, string\|null given\.$#' - count: 1 - path: ../../system/Helpers/text_helper.php - - - - message: '#^Only booleans are allowed in a ternary operator condition, string\|null given\.$#' - count: 1 - path: ../../system/Router/AutoRouter.php - - - - message: '#^Only booleans are allowed in a ternary operator condition, string\|null given\.$#' - count: 1 - path: ../../system/Router/RouteCollection.php diff --git a/utils/phpstan-baseline/ternary.shortNotAllowed.neon b/utils/phpstan-baseline/ternary.shortNotAllowed.neon index 15e447d499c0..a3dfc431cd94 100644 --- a/utils/phpstan-baseline/ternary.shortNotAllowed.neon +++ b/utils/phpstan-baseline/ternary.shortNotAllowed.neon @@ -1,4 +1,4 @@ -# total 34 errors +# total 27 errors parameters: ignoreErrors: @@ -17,16 +17,6 @@ parameters: count: 1 path: ../../system/Commands/Utilities/Namespaces.php - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Commands/Utilities/Routes/ControllerMethodReader.php - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' count: 2 @@ -35,23 +25,8 @@ parameters: - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' count: 4 - path: ../../system/Config/Services.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 5 path: ../../system/Cookie/Cookie.php - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Database/BaseConnection.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Database/Postgre/Connection.php - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' count: 1 @@ -79,12 +54,7 @@ parameters: - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/HTTP/Files/UploadedFile.php - - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 5 + count: 2 path: ../../system/HTTP/Response.php - @@ -94,12 +64,12 @@ parameters: - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 4 + count: 1 path: ../../system/I18n/Time.php - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 4 + count: 1 path: ../../system/I18n/TimeLegacy.php - @@ -117,11 +87,6 @@ parameters: count: 1 path: ../../system/Session/Session.php - - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 1 - path: ../../system/Test/CIUnitTestCase.php - - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' count: 2 @@ -129,7 +94,7 @@ parameters: - message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#' - count: 2 + count: 1 path: ../../system/Validation/Validation.php - diff --git a/utils/src/Rector/UnderscoreToCamelCaseVariableNameRector.php b/utils/src/Rector/UnderscoreToCamelCaseVariableNameRector.php index 1e1469c7f615..381a7dfb3a1c 100644 --- a/utils/src/Rector/UnderscoreToCamelCaseVariableNameRector.php +++ b/utils/src/Rector/UnderscoreToCamelCaseVariableNameRector.php @@ -178,7 +178,7 @@ private function updateDocblock(string $variableName, string $camelCaseName, ?Fu return; } - if (! preg_match(sprintf(self::PARAM_NAME_REGEX, $variableName), $docCommentText)) { + if (in_array(preg_match(sprintf(self::PARAM_NAME_REGEX, $variableName), $docCommentText), [0, false], true)) { return; }