From 302f2f177dc1f6d920ddd8a3a23d0f8455897386 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 14 May 2023 10:34:21 +0900 Subject: [PATCH 1/3] refactor: do not reassign $key --- system/Entity/Entity.php | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 92cedeff213f..128f5c3c8191 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -259,19 +259,19 @@ public function hasChanged(?string $key = null): bool return $this->original !== $this->attributes; } - $key = $this->mapProperty($key); + $dbColumn = $this->mapProperty($key); // Key doesn't exist in either - if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes)) { + if (! array_key_exists($dbColumn, $this->original) && ! array_key_exists($dbColumn, $this->attributes)) { return false; } // It's a new element - if (! array_key_exists($key, $this->original) && array_key_exists($key, $this->attributes)) { + if (! array_key_exists($dbColumn, $this->original) && array_key_exists($dbColumn, $this->attributes)) { return true; } - return $this->original[$key] !== $this->attributes[$key]; + return $this->original[$dbColumn] !== $this->attributes[$dbColumn]; } /** @@ -435,19 +435,19 @@ public function cast(?bool $cast = null) */ public function __set(string $key, $value = null) { - $key = $this->mapProperty($key); + $dbColumn = $this->mapProperty($key); // Check if the field should be mutated into a date - if (in_array($key, $this->dates, true)) { + if (in_array($dbColumn, $this->dates, true)) { $value = $this->mutateDate($value); } - $value = $this->castAs($value, $key, 'set'); + $value = $this->castAs($value, $dbColumn, 'set'); // if a set* method exists for this key, use that method to // insert this value. should be outside $isNullable check, // so maybe wants to do sth with null value automatically - $method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key))); + $method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $dbColumn))); if (method_exists($this, $method)) { $this->{$method}($value); @@ -459,7 +459,7 @@ public function __set(string $key, $value = null) // class properties that are undefined, though they cannot be // saved. Useful for grabbing values through joins, assigning // relationships, etc. - $this->attributes[$key] = $value; + $this->attributes[$dbColumn] = $value; return $this; } @@ -480,12 +480,12 @@ public function __set(string $key, $value = null) */ public function __get(string $key) { - $key = $this->mapProperty($key); + $dbColumn = $this->mapProperty($key); $result = null; // Convert to CamelCase for the method - $method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key))); + $method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $dbColumn))); // if a get* method exists for this key, // use that method to insert this value. @@ -495,17 +495,17 @@ public function __get(string $key) // Otherwise return the protected property // if it exists. - elseif (array_key_exists($key, $this->attributes)) { - $result = $this->attributes[$key]; + elseif (array_key_exists($dbColumn, $this->attributes)) { + $result = $this->attributes[$dbColumn]; } // Do we need to mutate this into a date? - if (in_array($key, $this->dates, true)) { + if (in_array($dbColumn, $this->dates, true)) { $result = $this->mutateDate($result); } // Or cast it as something? elseif ($this->_cast) { - $result = $this->castAs($result, $key); + $result = $this->castAs($result, $dbColumn); } return $result; @@ -521,15 +521,15 @@ public function __isset(string $key): bool return false; } - $key = $this->mapProperty($key); + $dbColumn = $this->mapProperty($key); - $method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key))); + $method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $dbColumn))); if (method_exists($this, $method)) { return true; } - return isset($this->attributes[$key]); + return isset($this->attributes[$dbColumn]); } /** @@ -541,9 +541,9 @@ public function __unset(string $key): void return; } - $key = $this->mapProperty($key); + $dbColumn = $this->mapProperty($key); - unset($this->attributes[$key]); + unset($this->attributes[$dbColumn]); } /** From af0c2f174c9b6eccd5b46b273c218064aaab24ef Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 14 May 2023 10:54:00 +0900 Subject: [PATCH 2/3] refactor: rename variable name --- system/Entity/Entity.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 128f5c3c8191..61a54e4c4ea3 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -551,10 +551,10 @@ public function __unset(string $key): void */ protected function isMappedDbColumn(string $key): bool { - $maybeColumnName = $this->mapProperty($key); + $dbColumn = $this->mapProperty($key); // Property name which has mapped column name - if ($key !== $maybeColumnName) { + if ($key !== $dbColumn) { return false; } From 9b7409986a8c824354a0a0b45fe7b7836dd6e14a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 14 May 2023 10:56:33 +0900 Subject: [PATCH 3/3] docs: improve comment --- system/Entity/Entity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Entity/Entity.php b/system/Entity/Entity.php index 61a54e4c4ea3..4247c82b66fe 100644 --- a/system/Entity/Entity.php +++ b/system/Entity/Entity.php @@ -553,7 +553,7 @@ protected function isMappedDbColumn(string $key): bool { $dbColumn = $this->mapProperty($key); - // Property name which has mapped column name + // The $key is a property name which has mapped db column name if ($key !== $dbColumn) { return false; }