diff --git a/.github/workflows/test-phpcpd.yml b/.github/workflows/test-phpcpd.yml index 53daa8ebab95..04ef53bb99c5 100644 --- a/.github/workflows/test-phpcpd.yml +++ b/.github/workflows/test-phpcpd.yml @@ -56,4 +56,5 @@ jobs: --exclude system/Database/Postgre/Builder.php --exclude system/Debug/Exceptions.php --exclude system/HTTP/SiteURI.php + --exclude system/Validation/Rules.php -- app/ public/ system/ diff --git a/app/Config/Kint.php b/app/Config/Kint.php index 77aeb08f1432..117e66d87c45 100644 --- a/app/Config/Kint.php +++ b/app/Config/Kint.php @@ -27,7 +27,7 @@ class Kint extends BaseConfig */ /** - * @var list|ConstructablePluginInterface> + * @var list|ConstructablePluginInterface>|null */ public $plugins; @@ -45,12 +45,12 @@ class Kint extends BaseConfig public int $richSort = AbstractRenderer::SORT_FULL; /** - * @var array> + * @var array>|null */ public $richObjectPlugins; /** - * @var array> + * @var array>|null */ public $richTabPlugins; diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 65a22b8f584a..1d32322ab5d4 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -16,11 +16,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Autoloader/Autoloader.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/Autoloader/FileLocator.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, int\\|string given\\.$#', 'count' => 1, @@ -31,11 +26,6 @@ 'count' => 6, 'path' => __DIR__ . '/system/Autoloader/FileLocator.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, - 'path' => __DIR__ . '/system/BaseModel.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\BaseModel\\:\\:chunk\\(\\) has parameter \\$userFunc with no signature specified for Closure\\.$#', 'count' => 1, @@ -161,11 +151,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Cache/Handlers/PredisHandler.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 6, - 'path' => __DIR__ . '/system/CodeIgniter.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', 'count' => 1, @@ -1721,16 +1706,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Debug/BaseExceptionHandler.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Debug/ExceptionHandler.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Debug/Exceptions.php', -]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', 'count' => 5, @@ -2466,11 +2441,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Log/Logger.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 15, - 'path' => __DIR__ . '/system/Model.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Model\\:\\:chunk\\(\\) has parameter \\$userFunc with no signature specified for Closure\\.$#', 'count' => 1, @@ -2546,11 +2516,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/RESTful/BaseResource.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/RESTful/ResourceController.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, string\\|null given\\.$#', 'count' => 1, @@ -2886,11 +2851,6 @@ 'count' => 2, 'path' => __DIR__ . '/system/Session/Session.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 9, - 'path' => __DIR__ . '/system/Session/Session.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Session\\\\Session\\:\\:configure\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -3041,11 +3001,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Session/SessionInterface.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, - 'path' => __DIR__ . '/system/Test/CIDatabaseTestCase.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\CIDatabaseTestCase\\:\\:clearInsertCache\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -3171,26 +3126,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/DOMParser.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Access to an undefined property object\\:\\:\\$createdField\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/Fabricator.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Access to an undefined property object\\:\\:\\$deletedField\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/Fabricator.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Access to an undefined property object\\:\\:\\$updatedField\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/Fabricator.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, - 'path' => __DIR__ . '/system/Test/Fabricator.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\Fabricator\\:\\:resetCounts\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -3211,11 +3146,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/FeatureTestCase.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, - 'path' => __DIR__ . '/system/Test/FeatureTestCase.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\FeatureTestCase\\:\\:clearInsertCache\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -3341,11 +3271,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/Mock/MockCURLRequest.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/Test/Mock/MockCache.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockCache\\:\\:assertHas\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -3381,11 +3306,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/Mock/MockConnection.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/system/Test/Mock/MockConnection.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockConnection\\:\\:_close\\(\\) should return mixed but return statement is missing\\.$#', 'count' => 1, @@ -3496,11 +3416,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/Mock/MockResourceController.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/Mock/MockResourcePresenter.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\Mock\\\\MockResourcePresenter\\:\\:getFormat\\(\\) has no return type specified\\.$#', 'count' => 1, @@ -3621,11 +3536,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/TestLogger.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/TestResponse.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Test\\\\TestResponse\\:\\:__call\\(\\) should return mixed but return statement is missing\\.$#', 'count' => 1, @@ -3741,31 +3651,11 @@ 'count' => 1, 'path' => __DIR__ . '/system/Throttle/Throttler.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Validation/CreditCardRules.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a negated boolean, array\\|null given\\.$#', 'count' => 6, 'path' => __DIR__ . '/system/Validation/FileRules.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 8, - 'path' => __DIR__ . '/system/Validation/Rules.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, - 'path' => __DIR__ . '/system/Validation/StrictRules/Rules.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/Validation/Validation.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\Validation\\\\Validation\\:\\:isClosure\\(\\) has parameter \\$rule with no signature specified for Closure\\.$#', 'count' => 1, @@ -3776,36 +3666,16 @@ 'count' => 2, 'path' => __DIR__ . '/system/Validation/Validation.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Validation/Views/list.php', -]; $ignoreErrors[] = [ 'message' => '#^Call to an undefined static method CodeIgniter\\\\Config\\\\Factories\\:\\:cells\\(\\)\\.$#', 'count' => 1, 'path' => __DIR__ . '/system/View/Cell.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/View/Filters.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 4, - 'path' => __DIR__ . '/system/View/Parser.php', -]; $ignoreErrors[] = [ 'message' => '#^Method CodeIgniter\\\\View\\\\Parser\\:\\:addPlugin\\(\\) has parameter \\$callback with no signature specified for callable\\.$#', 'count' => 1, 'path' => __DIR__ . '/system/View/Parser.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 7, - 'path' => __DIR__ . '/system/View/Table.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a ternary operator condition, float given\\.$#', 'count' => 1, @@ -3821,11 +3691,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/View/Table.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, - 'path' => __DIR__ . '/system/View/View.php', -]; $ignoreErrors[] = [ 'message' => '#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#', 'count' => 2, diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index d5b198ae93fb..3cf0535914c4 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -318,7 +318,7 @@ protected function format($data = null) // Determine correct response type through content negotiation if not explicitly declared if ( - (empty($this->format) || ! in_array($this->format, ['json', 'xml'], true)) + ! in_array($this->format, ['json', 'xml'], true) && $this->request instanceof IncomingRequest ) { $mime = $this->request->negotiate( diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index 11aa56edc4f7..c5cf8e18d28b 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -67,7 +67,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = ' $segments = explode('\\', $file); // The first segment will be empty if a slash started the filename. - if (empty($segments[0])) { + if ($segments[0] === '') { unset($segments[0]); } @@ -89,7 +89,7 @@ public function locateFile(string $file, ?string $folder = null, string $ext = ' } // if no namespaces matched then quit - if (empty($paths)) { + if ($paths === []) { return false; } @@ -272,7 +272,7 @@ public function findQualifiedNameFromPath(string $path) foreach ($this->getNamespaces() as $namespace) { $namespace['path'] = realpath($namespace['path']) ?: $namespace['path']; - if (empty($namespace['path'])) { + if ($namespace['path'] === '') { continue; } diff --git a/system/BaseModel.php b/system/BaseModel.php index 1b669a30d2c2..6a6f0dff4dd2 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -567,7 +567,7 @@ public function find($id = null) 'singleton' => $singleton, ]); - if (! empty($eventData['returnData'])) { + if (isset($eventData['returnData']) && $eventData['returnData'] === true) { return $eventData['data']; } } @@ -629,7 +629,7 @@ public function findAll(int $limit = 0, int $offset = 0) 'singleton' => false, ]); - if (! empty($eventData['returnData'])) { + if (isset($eventData['returnData']) && $eventData['returnData'] === true) { return $eventData['data']; } } @@ -667,7 +667,7 @@ public function first() 'singleton' => true, ]); - if (! empty($eventData['returnData'])) { + if (isset($eventData['returnData']) && $eventData['returnData'] === true) { return $eventData['data']; } } @@ -703,7 +703,7 @@ public function first() */ public function save($row): bool { - if (empty($row)) { + if ((array) $row === []) { return true; } @@ -729,7 +729,9 @@ public function save($row): bool */ protected function shouldUpdate($row): bool { - return ! empty($this->getIdValue($row)); + $id = $this->getIdValue($row); + + return ! ($id === null || $id === []); } /** @@ -1510,15 +1512,15 @@ public function validate($row): bool { $rules = $this->getValidationRules(); - if ($this->skipValidation || $rules === [] || empty($row)) { - return true; - } - // Validation requires array, so cast away. if (is_object($row)) { $row = (array) $row; } + if ($this->skipValidation || $rules === [] || $row === []) { + return true; + } + $rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $row) : $rules; // If no data existed that needs validation @@ -1632,7 +1634,7 @@ public function allowCallbacks(bool $val = true) protected function trigger(string $event, array $eventData) { // Ensure it's a valid event - if (! isset($this->{$event}) || empty($this->{$event})) { + if (! isset($this->{$event}) || $this->{$event} === []) { return $eventData; } @@ -1772,7 +1774,7 @@ protected function transformDataToArray($row, string $type): array throw new InvalidArgumentException(sprintf('Invalid type "%s" used upon transforming data to array.', $type)); } - if (! $this->allowEmptyInserts && empty($row)) { + if (! $this->allowEmptyInserts && ($row === null || (array) $row === [])) { throw DataException::forEmptyDataset($type); } diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 8299bfafa992..39a3029f3954 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -108,7 +108,7 @@ class CodeIgniter /** * Controller to use. * - * @var (Closure(mixed...): ResponseInterface|string)|string + * @var (Closure(mixed...): ResponseInterface|string)|string|null */ protected $controller; @@ -138,7 +138,7 @@ class CodeIgniter /** * Request path to use. * - * @var string + * @var string|null * * @deprecated No longer used. */ @@ -302,7 +302,7 @@ private function configureKint(): void Kint::$display_called_from = $config->displayCalledFrom; Kint::$expanded = $config->expanded; - if (! empty($config->plugins) && is_array($config->plugins)) { + if (isset($config->plugins) && is_array($config->plugins)) { Kint::$plugins = $config->plugins; } @@ -315,10 +315,10 @@ private function configureKint(): void RichRenderer::$theme = $config->richTheme; RichRenderer::$folder = $config->richFolder; RichRenderer::$sort = $config->richSort; - if (! empty($config->richObjectPlugins) && is_array($config->richObjectPlugins)) { + if (isset($config->richObjectPlugins) && is_array($config->richObjectPlugins)) { RichRenderer::$value_plugins = $config->richObjectPlugins; } - if (! empty($config->richTabPlugins) && is_array($config->richTabPlugins)) { + if (isset($config->richTabPlugins) && is_array($config->richTabPlugins)) { RichRenderer::$tab_plugins = $config->richTabPlugins; } @@ -848,11 +848,10 @@ protected function tryToRouteIt(?RouteCollectionInterface $routes = null) */ protected function determinePath() { - if (! empty($this->path)) { - return $this->path; - } - - return method_exists($this->request, 'getPath') ? $this->request->getPath() : $this->request->getUri()->getPath(); + return $this->path ?? + (method_exists($this->request, 'getPath') + ? $this->request->getPath() + : $this->request->getUri()->getPath()); } /** @@ -892,7 +891,7 @@ protected function startController() } // No controller specified - we don't know what to do now. - if (empty($this->controller)) { + if (! isset($this->controller)) { throw PageNotFoundException::forEmptyController(); } @@ -1091,7 +1090,7 @@ public function spoofRequestMethod() $method = $this->request->getPost('_method'); - if (empty($method)) { + if ($method === null) { return; } diff --git a/system/Model.php b/system/Model.php index d264107fc4dc..5dba810c8c59 100644 --- a/system/Model.php +++ b/system/Model.php @@ -257,13 +257,13 @@ protected function doFirst() if ($this->tempUseSoftDeletes) { $builder->where($this->table . '.' . $this->deletedField, null); - } elseif ($this->useSoftDeletes && empty($builder->QBGroupBy) && $this->primaryKey) { + } elseif ($this->useSoftDeletes && ($builder->QBGroupBy === []) && $this->primaryKey) { $builder->groupBy($this->table . '.' . $this->primaryKey); } // Some databases, like PostgreSQL, need order // information to consistently return correct results. - if ($builder->QBGroupBy && empty($builder->QBOrderBy) && $this->primaryKey) { + if ($builder->QBGroupBy && ($builder->QBOrderBy === []) && $this->primaryKey) { $builder->orderBy($this->table . '.' . $this->primaryKey, 'asc'); } @@ -286,7 +286,7 @@ protected function doInsert(array $row) // Require non-empty primaryKey when // not using auto-increment feature - if (! $this->useAutoIncrement && empty($row[$this->primaryKey])) { + if (! $this->useAutoIncrement && ! isset($row[$this->primaryKey])) { throw DataException::forEmptyPrimaryKey('insert'); } @@ -351,7 +351,7 @@ protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $ foreach ($set as $row) { // Require non-empty primaryKey when // not using auto-increment feature - if (! $this->useAutoIncrement && empty($row[$this->primaryKey])) { + if (! $this->useAutoIncrement && ! isset($row[$this->primaryKey])) { throw DataException::forEmptyPrimaryKey('insertBatch'); } } @@ -433,7 +433,7 @@ protected function doDelete($id = null, bool $purge = false) } if ($this->useSoftDeletes && ! $purge) { - if (empty($builder->getCompiledQBWhere())) { + if ($builder->getCompiledQBWhere() === []) { throw new DatabaseException( 'Deletes are not allowed unless they contain a "where" or "like" clause.' ); @@ -557,7 +557,7 @@ public function getIdValue($row) return $row->{$this->primaryKey}; } - if (is_array($row) && ! empty($row[$this->primaryKey])) { + if (is_array($row) && isset($row[$this->primaryKey])) { return $row[$this->primaryKey]; } @@ -591,7 +591,7 @@ public function chunk(int $size, Closure $userFunc) $offset += $size; - if (empty($rows)) { + if ($rows === []) { continue; } @@ -648,7 +648,7 @@ public function builder(?string $table = null) // We're going to force a primary key to exist // so we don't have overly convoluted code, // and future features are likely to require them. - if (empty($this->primaryKey)) { + if ($this->primaryKey === '') { throw ModelException::forNoPrimaryKey(static::class); } @@ -729,8 +729,8 @@ protected function shouldUpdate($row): bool */ public function insert($row = null, bool $returnID = true) { - if (! empty($this->tempData['data'])) { - if (empty($row)) { + if (isset($this->tempData['data'])) { + if ($row === null) { $row = $this->tempData['data']; } else { $row = $this->transformDataToArray($row, 'insert'); @@ -792,8 +792,8 @@ protected function doProtectFieldsForInsert(array $row): array */ public function update($id = null, $row = null): bool { - if (! empty($this->tempData['data'])) { - if (empty($row)) { + if (isset($this->tempData['data'])) { + if ($row === null) { $row = $this->tempData['data']; } else { $row = $this->transformDataToArray($row, 'update'); @@ -910,7 +910,7 @@ public static function classToArray($data, $primaryKey = null, string $dateForma $properties = $data->toRawArray($onlyChanged); // Always grab the primary key otherwise updates will fail. - if (! empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey, $properties, true) && ! empty($data->{$primaryKey})) { + if ($properties !== [] && isset($primaryKey) && ! in_array($primaryKey, $properties, true) && isset($data->{$primaryKey})) { $properties[$primaryKey] = $data->{$primaryKey}; } } else { diff --git a/system/RESTful/ResourceController.php b/system/RESTful/ResourceController.php index 03ba4d314613..6ffb602c8bd1 100644 --- a/system/RESTful/ResourceController.php +++ b/system/RESTful/ResourceController.php @@ -105,6 +105,7 @@ public function delete($id = null) * Set/change the expected response representation for returned objects * * @param string $format json/xml + * @phpstan-param 'json'|'xml' $format * * @return void */ diff --git a/system/Session/Session.php b/system/Session/Session.php index 7dfe5898d78f..5c087579a2e4 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -241,7 +241,7 @@ public function start() $this->startSession(); // Is session ID auto-regeneration configured? (ignoring ajax requests) - if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') + if ((! isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') && ($regenerateTime = $this->config->timeToUpdate) > 0 ) { if (! isset($_SESSION['__ci_last_regenerate'])) { @@ -368,7 +368,7 @@ protected function configureSidLength() */ protected function initVars() { - if (empty($_SESSION['__ci_vars'])) { + if (! isset($_SESSION['__ci_vars'])) { return; } @@ -384,7 +384,7 @@ protected function initVars() } } - if (empty($_SESSION['__ci_vars'])) { + if ($_SESSION['__ci_vars'] === []) { unset($_SESSION['__ci_vars']); } } @@ -646,7 +646,7 @@ public function getFlashdata(?string $key = null) $flashdata = []; - if (! empty($_SESSION['__ci_vars'])) { + if (isset($_SESSION['__ci_vars'])) { foreach ($_SESSION['__ci_vars'] as $key => &$value) { if (! is_int($value)) { $flashdata[$key] = $_SESSION[$key]; @@ -706,7 +706,7 @@ public function markAsFlashdata($key): bool */ public function unmarkFlashdata($key) { - if (empty($_SESSION['__ci_vars'])) { + if (! isset($_SESSION['__ci_vars'])) { return; } @@ -720,7 +720,7 @@ public function unmarkFlashdata($key) } } - if (empty($_SESSION['__ci_vars'])) { + if ($_SESSION['__ci_vars'] === []) { unset($_SESSION['__ci_vars']); } } @@ -778,7 +778,7 @@ public function getTempdata(?string $key = null) $tempdata = []; - if (! empty($_SESSION['__ci_vars'])) { + if (isset($_SESSION['__ci_vars'])) { foreach ($_SESSION['__ci_vars'] as $key => &$value) { if (is_int($value)) { $tempdata[$key] = $_SESSION[$key]; @@ -856,7 +856,7 @@ public function markAsTempdata($key, int $ttl = 300): bool */ public function unmarkTempdata($key) { - if (empty($_SESSION['__ci_vars'])) { + if (! isset($_SESSION['__ci_vars'])) { return; } @@ -870,7 +870,7 @@ public function unmarkTempdata($key) } } - if (empty($_SESSION['__ci_vars'])) { + if ($_SESSION['__ci_vars'] === []) { unset($_SESSION['__ci_vars']); } } diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index 4e889b5ea1d0..b27db73f229e 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -123,7 +123,7 @@ abstract class CIUnitTestCase extends TestCase /** * The namespace(s) to help us find the migration classes. - * Empty is equivalent to running `spark migrate --all`. + * `null` is equivalent to running `spark migrate --all`. * Note that running "all" runs migrations in date order, * but specifying namespaces runs them in namespace order (then date) * diff --git a/system/Test/DatabaseTestTrait.php b/system/Test/DatabaseTestTrait.php index d7b8b02ddaa8..f69a535f41ec 100644 --- a/system/Test/DatabaseTestTrait.php +++ b/system/Test/DatabaseTestTrait.php @@ -120,7 +120,7 @@ protected function regressDatabase() } // If no namespace was specified then rollback all - if (empty($this->namespace)) { + if ($this->namespace === null) { $this->migrations->setNamespace(null); $this->migrations->regress(0, 'tests'); } @@ -146,7 +146,7 @@ protected function migrateDatabase() } // If no namespace was specified then migrate all - if (empty($this->namespace)) { + if ($this->namespace === null) { $this->migrations->setNamespace(null); $this->migrations->latest('tests'); self::$doneMigration = true; @@ -182,8 +182,8 @@ protected function setUpSeed() */ protected function runSeeds() { - if (! empty($this->seed)) { - if (! empty($this->basePath)) { + if ($this->seed !== '') { + if ($this->basePath !== '') { $this->seeder->setPath(rtrim($this->basePath, '/') . '/Seeds'); } diff --git a/system/Test/Fabricator.php b/system/Test/Fabricator.php index 3a28f281d127..c247b8741b01 100644 --- a/system/Test/Fabricator.php +++ b/system/Test/Fabricator.php @@ -128,7 +128,7 @@ public function __construct($model, ?array $formatters = null, ?string $locale = // Determine eligible date fields foreach (['createdField', 'updatedField', 'deletedField'] as $field) { - if (! empty($this->model->{$field})) { + if (isset($this->model->{$field})) { $this->dateFields[] = $this->model->{$field}; } } @@ -152,7 +152,7 @@ public static function resetCounts() */ public static function getCount(string $table): int { - return empty(self::$tableCounts[$table]) ? 0 : self::$tableCounts[$table]; + return ! isset(self::$tableCounts[$table]) ? 0 : self::$tableCounts[$table]; } /** @@ -282,7 +282,7 @@ protected function detectFormatters(): self { $this->formatters = []; - if (! empty($this->model->allowedFields)) { + if (isset($this->model->allowedFields)) { foreach ($this->model->allowedFields as $field) { $this->formatters[$field] = $this->guessFormatter($field); } @@ -513,12 +513,12 @@ protected function createMock(?int $count = null) // Determine which fields we will need $fields = []; - if (! empty($this->model->useTimestamps)) { + if ($this->model->useTimestamps) { $fields[$this->model->createdField] = $datetime; $fields[$this->model->updatedField] = $datetime; } - if (! empty($this->model->useSoftDeletes)) { + if ($this->model->useSoftDeletes) { $fields[$this->model->deletedField] = null; } diff --git a/system/Test/FeatureTestCase.php b/system/Test/FeatureTestCase.php index 5c74e27a774d..fff624a840f3 100644 --- a/system/Test/FeatureTestCase.php +++ b/system/Test/FeatureTestCase.php @@ -186,7 +186,7 @@ public function call(string $method, string $path, ?array $params = null) ->run($routes, true); $output = \ob_get_contents(); - if (empty($response->getBody()) && ! ($output === '' || $output === false)) { + if (($response->getBody() === null) && ! ($output === '' || $output === false)) { $response->setBody($output); } diff --git a/system/Test/Mock/MockCache.php b/system/Test/Mock/MockCache.php index 73a755802285..082807b559ec 100644 --- a/system/Test/Mock/MockCache.php +++ b/system/Test/Mock/MockCache.php @@ -22,7 +22,7 @@ class MockCache extends BaseHandler implements CacheInterface /** * Mock cache storage. * - * @var array + * @var array */ protected $cache = []; @@ -155,7 +155,7 @@ public function increment(string $key, int $offset = 1) $key = static::validateKey($key, $this->prefix); $data = $this->cache[$key] ?: null; - if (empty($data)) { + if ($data === null) { $data = 0; } elseif (! is_int($data)) { return false; @@ -175,7 +175,7 @@ public function decrement(string $key, int $offset = 1) $data = $this->cache[$key] ?: null; - if (empty($data)) { + if ($data === null) { $data = 0; } elseif (! is_int($data)) { return false; @@ -281,9 +281,9 @@ public function assertHasValue(string $key, $value = null) { $item = $this->get($key); - // Let assertHas handle throwing the error for consistency + // Let assertHas() handle throwing the error for consistency // if the key is not found - if (empty($item)) { + if ($item === null) { $this->assertHas($key); } diff --git a/system/Test/Mock/MockConnection.php b/system/Test/Mock/MockConnection.php index ea4ebac9b471..2e9ebe3b527a 100644 --- a/system/Test/Mock/MockConnection.php +++ b/system/Test/Mock/MockConnection.php @@ -62,7 +62,7 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s $query->setQuery($sql, $binds, $setEscapeFlags); - if (! empty($this->swapPre) && ! empty($this->DBPrefix)) { + if ($this->swapPre !== '' && $this->DBPrefix !== '') { $query->swapPrefix($this->DBPrefix, $this->swapPre); } diff --git a/system/Test/TestResponse.php b/system/Test/TestResponse.php index 956b32188ac8..215942d54fe9 100644 --- a/system/Test/TestResponse.php +++ b/system/Test/TestResponse.php @@ -136,8 +136,10 @@ public function isOK(): bool return false; } + $body = (string) $this->response->getBody(); + // Empty bodies are not considered valid, unless in redirects - return ! ($status < 300 && empty($this->response->getBody())); + return ! ($status < 300 && $body === ''); } /** diff --git a/system/Validation/CreditCardRules.php b/system/Validation/CreditCardRules.php index 61f59d7bfe86..8e17b75d375f 100644 --- a/system/Validation/CreditCardRules.php +++ b/system/Validation/CreditCardRules.php @@ -185,7 +185,7 @@ public function valid_cc_number(?string $ccNumber, string $type): bool } // If empty, it's not a card type we recognize, or invalid type. - if (empty($info)) { + if ($info === null) { return false; } diff --git a/system/Validation/Rules.php b/system/Validation/Rules.php index 6481a45b4e00..7a051f902ed8 100644 --- a/system/Validation/Rules.php +++ b/system/Validation/Rules.php @@ -105,7 +105,8 @@ public function is_not_unique(?string $str, string $field, array $data): bool ->limit(1); if ( - ! empty($whereField) && ! empty($whereValue) + $whereField !== null && $whereField !== '' + && $whereValue !== null && $whereValue !== '' && ! preg_match('/^\{(\w+)\}$/', $whereValue) ) { $row = $row->where($whereField, $whereValue); @@ -150,7 +151,8 @@ public function is_unique(?string $str, string $field, array $data): bool ->limit(1); if ( - ! empty($ignoreField) && ! empty($ignoreValue) + $ignoreField !== null && $ignoreField !== '' + && $ignoreValue !== null && $ignoreValue !== '' && ! preg_match('/^\{(\w+)\}$/', $ignoreValue) ) { $row = $row->where("{$ignoreField} !=", $ignoreValue); @@ -276,8 +278,10 @@ public function required_with($str = null, ?string $fields = null, array $data = foreach (explode(',', $fields) as $field) { if ( - (array_key_exists($field, $data) && ! empty($data[$field])) - || (strpos($field, '.') !== false && ! empty(dot_array_search($field, $data))) + (array_key_exists($field, $data) + && ! empty($data[$field])) // @phpstan-ignore-line Use empty() + || (strpos($field, '.') !== false + && ! empty(dot_array_search($field, $data))) // @phpstan-ignore-line Use empty() ) { $requiredFields[] = $field; } @@ -323,7 +327,8 @@ public function required_without( foreach (explode(',', $otherFields) as $otherField) { if ( (strpos($otherField, '.') === false) - && (! array_key_exists($otherField, $data) || empty($data[$otherField])) + && (! array_key_exists($otherField, $data) + || empty($data[$otherField])) // @phpstan-ignore-line Use empty() ) { return false; } @@ -338,7 +343,7 @@ public function required_without( $fieldKey = $fieldSplitArray[1]; if (is_array($fieldData)) { - return ! empty(dot_array_search($otherField, $data)[$fieldKey]); + return ! empty(dot_array_search($otherField, $data)[$fieldKey]); // @phpstan-ignore-line Use empty() } $nowField = str_replace('*', $fieldKey, $otherField); $nowFieldVaule = dot_array_search($nowField, $data); diff --git a/system/Validation/StrictRules/Rules.php b/system/Validation/StrictRules/Rules.php index 70d081975a62..3086ca5a1d95 100644 --- a/system/Validation/StrictRules/Rules.php +++ b/system/Validation/StrictRules/Rules.php @@ -157,7 +157,8 @@ public function is_not_unique($str, string $field, array $data): bool ->limit(1); if ( - ! empty($whereField) && ! empty($whereValue) + $whereField !== null && $whereField !== '' + && $whereValue !== null && $whereValue !== '' && ! preg_match('/^\{(\w+)\}$/', $whereValue) ) { $row = $row->where($whereField, $whereValue); @@ -216,7 +217,8 @@ public function is_unique($str, string $field, array $data): bool ->limit(1); if ( - ! empty($ignoreField) && ! empty($ignoreValue) + $ignoreField !== null && $ignoreField !== '' + && $ignoreValue !== null && $ignoreValue !== '' && ! preg_match('/^\{(\w+)\}$/', $ignoreValue) ) { $row = $row->where("{$ignoreField} !=", $ignoreValue); diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index 6e71aadb23f3..83bb361e7017 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -148,7 +148,7 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup // If no rules exist, we return false to ensure // the developer didn't forget to set the rules. - if (empty($this->rules)) { + if ($this->rules === []) { return false; } @@ -707,7 +707,7 @@ public function showError(string $field, string $template = 'single'): string */ protected function loadRuleSets() { - if (empty($this->ruleSetFiles)) { + if ($this->ruleSetFiles === [] || $this->ruleSetFiles === null) { throw ValidationException::forNoRuleSets(); } @@ -918,7 +918,7 @@ protected function getErrorMessage( $message = str_replace('{field}', ($label === null || $label === '') ? $field : lang($label), $message); $message = str_replace( '{param}', - empty($this->rules[$param]['label']) ? $param : lang($this->rules[$param]['label']), + (! isset($this->rules[$param]['label'])) ? $param : lang($this->rules[$param]['label']), $message ); diff --git a/system/Validation/Views/list.php b/system/Validation/Views/list.php index cc95156cd5c4..3669f038e806 100644 --- a/system/Validation/Views/list.php +++ b/system/Validation/Views/list.php @@ -1,4 +1,4 @@ - +