From be71f93541271f1d4b71a9623533b1b4e76e758e Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 11 Oct 2023 20:22:45 +0900 Subject: [PATCH 1/4] refactor!: remove deprecated methods --- system/BaseModel.php | 64 ---------------------------------- system/Model.php | 82 -------------------------------------------- 2 files changed, 146 deletions(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 63a87e587828..6fa02fe4bd23 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -484,17 +484,6 @@ abstract protected function doReplace(?array $data = null, bool $returnSQL = fal */ abstract protected function doErrors(); - /** - * Returns the id value for the data array or object. - * - * @param array|object $data Data - * - * @return array|int|string|null - * - * @deprecated Add an override on getIdValue() instead. Will be removed in version 5.0. - */ - abstract protected function idValue($data); - /** * Public getter to return the id value using the idValue() method. * For example with SQL this will return $data->$this->primaryKey. @@ -1798,59 +1787,6 @@ public function __call(string $name, array $params) return null; } - /** - * Replace any placeholders within the rules with the values that - * match the 'key' of any properties being set. For example, if - * we had the following $data array: - * - * [ 'id' => 13 ] - * - * and the following rule: - * - * 'required|is_unique[users,email,id,{id}]' - * - * The value of {id} would be replaced with the actual id in the form data: - * - * 'required|is_unique[users,email,id,13]' - * - * @param array $rules Validation rules - * @param array $data Data - * - * @codeCoverageIgnore - * - * @deprecated use fillPlaceholders($rules, $data) from Validation instead - */ - protected function fillPlaceholders(array $rules, array $data): array - { - $replacements = []; - - foreach ($data as $key => $value) { - $replacements['{' . $key . '}'] = $value; - } - - if (! empty($replacements)) { - foreach ($rules as &$rule) { - if (is_array($rule)) { - foreach ($rule as &$row) { - // Should only be an `errors` array - // which doesn't take placeholders. - if (is_array($row)) { - continue; - } - - $row = strtr($row, $replacements); - } - - continue; - } - - $rule = strtr($rule, $replacements); - } - } - - return $rules; - } - /** * Sets $allowEmptyInserts. */ diff --git a/system/Model.php b/system/Model.php index b8c4de4470a2..272653b8d7a6 100644 --- a/system/Model.php +++ b/system/Model.php @@ -25,9 +25,7 @@ use CodeIgniter\I18n\Time; use CodeIgniter\Validation\ValidationInterface; use Config\Database; -use ReflectionClass; use ReflectionException; -use ReflectionProperty; /** * The Model class extends BaseModel and provides additional @@ -505,20 +503,6 @@ protected function doErrors() return [get_class($this->db) => $error['message']]; } - /** - * Returns the id value for the data array or object - * - * @param array|object $data Data - * - * @return array|int|string|null - * - * @deprecated Use getIdValue() instead. Will be removed in version 5.0. - */ - protected function idValue($data) - { - return $this->getIdValue($data); - } - /** * Returns the id value for the data array or object * @@ -889,70 +873,4 @@ private function checkBuilderMethod(string $name): void throw ModelException::forMethodNotAvailable(static::class, $name . '()'); } } - - /** - * Takes a class an returns an array of it's public and protected - * properties as an array suitable for use in creates and updates. - * - * @param object|string $data - * @param string|null $primaryKey - * - * @throws ReflectionException - * - * @codeCoverageIgnore - * - * @deprecated 4.1.0 - */ - public static function classToArray($data, $primaryKey = null, string $dateFormat = 'datetime', bool $onlyChanged = true): array - { - if (method_exists($data, 'toRawArray')) { - $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})) { - $properties[$primaryKey] = $data->{$primaryKey}; - } - } else { - $mirror = new ReflectionClass($data); - $props = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); - - $properties = []; - - // Loop over each property, - // saving the name/value in a new array we can return. - foreach ($props as $prop) { - // Must make protected values accessible. - $prop->setAccessible(true); - $properties[$prop->getName()] = $prop->getValue($data); - } - } - - // Convert any Time instances to appropriate $dateFormat - if ($properties) { - foreach ($properties as $key => $value) { - if ($value instanceof Time) { - switch ($dateFormat) { - case 'datetime': - $converted = $value->format('Y-m-d H:i:s'); - break; - - case 'date': - $converted = $value->format('Y-m-d'); - break; - - case 'int': - $converted = $value->getTimestamp(); - break; - - default: - $converted = (string) $value; - } - - $properties[$key] = $converted; - } - } - } - - return $properties; - } } From ed3c52a6c3101396ad176d4f55ba3986fbafc004 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 11 Oct 2023 20:23:08 +0900 Subject: [PATCH 2/4] refactor!: make getIdValue() abstract --- system/BaseModel.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 6fa02fe4bd23..4add1836cde8 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -491,13 +491,8 @@ abstract protected function doErrors(); * @param array|object $data * * @return array|int|string|null - * - * @todo: Make abstract in version 5.0 */ - public function getIdValue($data) - { - return $this->idValue($data); - } + abstract public function getIdValue($data); /** * Override countAllResults to account for soft deleted accounts. From bbb4a1fa13471011b7ddcf25c9ab77c3a1669c93 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 12 Oct 2023 10:34:44 +0900 Subject: [PATCH 3/4] chore: update phpstan-baseline --- phpstan-baseline.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 64f07d9caeab..8fa88e921662 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -38,7 +38,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 16, + 'count' => 15, 'path' => __DIR__ . '/system/BaseModel.php', ]; $ignoreErrors[] = [ @@ -2793,7 +2793,7 @@ ]; $ignoreErrors[] = [ 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 21, + 'count' => 18, 'path' => __DIR__ . '/system/Model.php', ]; $ignoreErrors[] = [ From 8c2df758ea2eb9f5ff5a90dc6e04f6cd9a955c32 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 12 Oct 2023 10:43:23 +0900 Subject: [PATCH 4/4] docs: add changelog and upgrade --- user_guide_src/source/changelogs/v4.5.0.rst | 9 +++++++++ user_guide_src/source/installation/upgrade_450.rst | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 89910fa84f3d..ad96b7574b92 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -35,6 +35,7 @@ Others have been fixed to ``void`` to follow the PSR-3 interface. - **Autoloader:** The prefix ``\`` in the fully qualified classname returned by ``FileLocator::findQualifiedNameFromPath()`` has been removed. +- **BaseModel:** The ``getIdValue()`` method has been changed to ``abstract``. Interface Changes ================= @@ -85,6 +86,14 @@ Others - **Config:** The deprecated ``CodeIgniter\Config\Config`` class has been removed. +Model +----- + +- ``BaseModel::idValue()`` +- ``BaseModel::fillPlaceholders()`` +- ``Model::idValue()`` +- ``Model::classToArray()`` + Enhancements ************ diff --git a/user_guide_src/source/installation/upgrade_450.rst b/user_guide_src/source/installation/upgrade_450.rst index 0a868831503c..878ecbd1ed21 100644 --- a/user_guide_src/source/installation/upgrade_450.rst +++ b/user_guide_src/source/installation/upgrade_450.rst @@ -80,6 +80,14 @@ Qualified Classnames with a leading ``\``. Now the leading ``\`` has been remove If you have code that expects a leading ``\``, fix it. +BaseModel::getIdValue() +======================= + +The ``BaseModel::getIdValue()`` has been changed to ``abstract``, and the implementation +has been removed. + +If you extneds ``BaseModel``, implement the ``getIdValue()`` method in the child class. + Removed Deprecated Items ========================