Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove deprecated methods in Model #8032

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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[] = [
Expand Down
71 changes: 1 addition & 70 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,31 +484,15 @@ 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.
*
* @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.
Expand Down Expand Up @@ -1798,59 +1782,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.
*/
Expand Down
82 changes: 0 additions & 82 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=================
Expand Down Expand Up @@ -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
************

Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/installation/upgrade_450.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
========================

Expand Down
Loading