From 398526d498c058250d3d9692429e1b3771a18964 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Thu, 30 Oct 2025 17:35:00 +0700 Subject: [PATCH 1/3] refactor: removed duplicate object var read and extracted method --- src/Illuminate/Database/Eloquent/Model.php | 29 ++++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 7a366808a757..236ca620b3a5 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2581,6 +2581,23 @@ public function escapeWhenCastingToString($escape = true) return $this; } + protected function getPropertiesForSerialization(): array + { + if (version_compare(PHP_VERSION, '8.4.0') < 0) { + return array_keys(get_object_vars($this)); + } + + $keys = []; + + foreach ((new ReflectionClass($this))->getProperties() as $property) { + if (!$property->isStatic() && !$property->hasHooks()) { + $keys[] = $property->getName(); + } + } + + return $keys; + } + /** * Prepare the object for serialization. * @@ -2595,17 +2612,7 @@ public function __sleep() $this->relationAutoloadCallback = null; $this->relationAutoloadContext = null; - $keys = get_object_vars($this); - - if (version_compare(PHP_VERSION, '8.4.0', '>=')) { - foreach ((new ReflectionClass($this))->getProperties() as $property) { - if ($property->hasHooks()) { - unset($keys[$property->getName()]); - } - } - } - - return array_keys($keys); + return $this->getPropertiesForSerialization(); } /** From 44f8234b4cf2811c010c69e5faeccb7ef478d33c Mon Sep 17 00:00:00 2001 From: NickSdot <32384907+NickSdot@users.noreply.github.com> Date: Thu, 30 Oct 2025 19:23:10 +0800 Subject: [PATCH 2/3] call `version_compare()` with third argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com> --- src/Illuminate/Database/Eloquent/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 236ca620b3a5..8c1a4a68446b 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2583,7 +2583,7 @@ public function escapeWhenCastingToString($escape = true) protected function getPropertiesForSerialization(): array { - if (version_compare(PHP_VERSION, '8.4.0') < 0) { + if (version_compare(PHP_VERSION, '8.4.0', '<')) { return array_keys(get_object_vars($this)); } From 77a1e7cdf48900ccd98cca076403ae84b50037a9 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Thu, 30 Oct 2025 22:36:41 +0700 Subject: [PATCH 3/3] refactor: added missing phpdoc --- src/Illuminate/Database/Eloquent/Model.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 8c1a4a68446b..8295f72d7331 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2581,6 +2581,11 @@ public function escapeWhenCastingToString($escape = true) return $this; } + /** + * Get the properties for serialization. + * + * @return array + */ protected function getPropertiesForSerialization(): array { if (version_compare(PHP_VERSION, '8.4.0', '<')) {