From d510a42d68453086b0be115925d40ac8867da992 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 18 Dec 2023 16:11:27 +0900 Subject: [PATCH 01/10] refactor: replace empty() --- system/API/ResponseTrait.php | 2 +- system/Autoloader/FileLocator.php | 6 +++--- system/BaseModel.php | 24 +++++++++++++----------- system/RESTful/ResourceController.php | 1 + 4 files changed, 18 insertions(+), 15 deletions(-) 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/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 */ From d7587744c02f2bcc2acb8aa3ff825ed2dd3fbaa7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 18 Dec 2023 16:38:53 +0900 Subject: [PATCH 02/10] refactor: replace empty() --- app/Config/Kint.php | 6 +++--- system/CodeIgniter.php | 16 ++++++++-------- system/View/Table.php | 14 +++++++------- system/View/View.php | 8 ++++---- 4 files changed, 22 insertions(+), 22 deletions(-) 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/system/CodeIgniter.php b/system/CodeIgniter.php index 8299bfafa992..1dcf55d2fbe6 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,7 +848,7 @@ protected function tryToRouteIt(?RouteCollectionInterface $routes = null) */ protected function determinePath() { - if (! empty($this->path)) { + if (isset($this->path)) { return $this->path; } @@ -892,7 +892,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 +1091,7 @@ public function spoofRequestMethod() $method = $this->request->getPost('_method'); - if (empty($method)) { + if ($method === null) { return; } diff --git a/system/View/Table.php b/system/View/Table.php index 9a259c8beafa..0cab38fb4f39 100644 --- a/system/View/Table.php +++ b/system/View/Table.php @@ -217,7 +217,7 @@ public function addRow() { $tmpRow = $this->_prepArgs(func_get_args()); - if ($this->syncRowsWithHeading && ! empty($this->heading)) { + if ($this->syncRowsWithHeading && $this->heading !== []) { // each key has an index $keyIndex = array_flip(array_keys($this->heading)); @@ -303,7 +303,7 @@ public function generate($tableData = null) { // The table data can optionally be passed to this function // either as a database result object or an array - if (! empty($tableData)) { + if ($tableData !== null && $tableData !== []) { if ($tableData instanceof BaseResult) { $this->_setFromDBResult($tableData); } elseif (is_array($tableData)) { @@ -312,7 +312,7 @@ public function generate($tableData = null) } // Is there anything to display? No? Smite them! - if (empty($this->heading) && $this->rows === []) { + if ($this->heading === [] && $this->rows === []) { return 'Undefined table data'; } @@ -333,7 +333,7 @@ public function generate($tableData = null) } // Is there a table heading to display? - if (! empty($this->heading)) { + if ($this->heading !== []) { $headerTag = null; if (preg_match('/(<)(td|th)(?=\h|>)/i', $this->template['heading_cell_start'], $matches) === 1) { @@ -399,7 +399,7 @@ public function generate($tableData = null) } // Any table footing to display? - if (! empty($this->footing)) { + if ($this->footing !== []) { $footerTag = null; if (preg_match('/(<)(td|th)(?=\h|>)/i', $this->template['footing_cell_start'], $matches)) { @@ -458,7 +458,7 @@ public function clear() protected function _setFromDBResult($object) { // First generate the headings from the table column names - if ($this->autoHeading && empty($this->heading)) { + if ($this->autoHeading && $this->heading === []) { $this->heading = $this->_prepArgs($object->getFieldNames()); } @@ -476,7 +476,7 @@ protected function _setFromDBResult($object) */ protected function _setFromArray($data) { - if ($this->autoHeading && empty($this->heading)) { + if ($this->autoHeading && $this->heading === []) { $this->heading = $this->_prepArgs(array_shift($data)); } diff --git a/system/View/View.php b/system/View/View.php index 9d84e969da5b..2e2823fa2332 100644 --- a/system/View/View.php +++ b/system/View/View.php @@ -178,7 +178,7 @@ public function render(string $view, ?array $options = null, ?bool $saveData = n $fileExt = pathinfo($view, PATHINFO_EXTENSION); // allow Views as .html, .tpl, etc (from CI3) - $this->renderVars['view'] = empty($fileExt) ? $view . '.php' : $view; + $this->renderVars['view'] = ($fileExt === '') ? $view . '.php' : $view; $this->renderVars['options'] = $options ?? []; @@ -207,12 +207,12 @@ public function render(string $view, ?array $options = null, ?bool $saveData = n $this->renderVars['file'] = $this->loader->locateFile( $this->renderVars['view'], 'Views', - empty($fileExt) ? 'php' : $fileExt + ($fileExt === '') ? 'php' : $fileExt ); } - // locateFile will return an empty string if the file cannot be found. - if (empty($this->renderVars['file'])) { + // locateFile() will return false if the file cannot be found. + if ($this->renderVars['file'] === false) { throw ViewException::forInvalidFile($this->renderVars['view']); } From 31c5c141b97e0ac1c744f9675f0648ba4324684f Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 18 Dec 2023 17:32:15 +0900 Subject: [PATCH 03/10] refactor: replace empty() --- system/CodeIgniter.php | 9 ++++----- system/Validation/StrictRules/Rules.php | 6 ++++-- system/Validation/Validation.php | 6 +++--- system/Validation/Views/list.php | 2 +- system/View/Filters.php | 2 +- system/View/Parser.php | 10 +++++----- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 1dcf55d2fbe6..39a3029f3954 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -848,11 +848,10 @@ protected function tryToRouteIt(?RouteCollectionInterface $routes = null) */ protected function determinePath() { - if (isset($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()); } /** 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 @@ - +