From f60028e45fe15e075cbdd285d68c6e3833b94b7a Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Thu, 17 Feb 2022 16:06:11 +0100 Subject: [PATCH 1/4] Keep PHP 8.1 enums as is --- fixtures/f012/Suit.php | 11 +++++++++++ src/DeepCopy/DeepCopy.php | 5 +++++ tests/DeepCopyTest/DeepCopyTest.php | 13 +++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 fixtures/f012/Suit.php diff --git a/fixtures/f012/Suit.php b/fixtures/f012/Suit.php new file mode 100644 index 0000000..9d77ddb --- /dev/null +++ b/fixtures/f012/Suit.php @@ -0,0 +1,11 @@ +copyObject($var); } diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index a079c23..8d11c5b 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -19,6 +19,7 @@ use DeepCopy\f008; use DeepCopy\f009; use DeepCopy\f011; +use DeepCopy\f012\Suit; use DeepCopy\Filter\KeepFilter; use DeepCopy\Filter\SetNullFilter; use DeepCopy\Matcher\PropertyNameMatcher; @@ -495,6 +496,18 @@ public function test_it_ignores_uninitialized_typed_properties() $this->assertFalse(isset($copy->foo)); } + /** + * @requires PHP 8.1 + */ + public function test_it_keeps_enums() + { + $enum = Suit::Clubs; + + $copy = (new DeepCopy())->copy($enum); + + $this->assertSame($enum, $copy); + } + private function assertEqualButNotSame($expected, $val) { $this->assertEquals($expected, $val); From c78e0516c19a72dd75aa258d55c1ede43b1ed9c2 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Fri, 18 Feb 2022 08:26:49 +0100 Subject: [PATCH 2/4] enum_exists may have been manually declared on lower PHP versions, use strict PHP version check --- src/DeepCopy/DeepCopy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 863c0ca..678df40 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -140,8 +140,8 @@ private function recursiveCopy($var) return $var; } - // PHP 8.1 Enum - if (function_exists('enum_exists') && enum_exists($var::class)) { + // Enum + if (PHP_VERSION_ID >= 80100 && enum_exists($var::class)) { return $var; } From fe20811c6c719a4d5616755dd7feb7ae7a5c8bb0 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Thu, 3 Mar 2022 09:17:33 +0100 Subject: [PATCH 3/4] Backward compatibility --- src/DeepCopy/DeepCopy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 678df40..9aeea6d 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -141,7 +141,7 @@ private function recursiveCopy($var) } // Enum - if (PHP_VERSION_ID >= 80100 && enum_exists($var::class)) { + if (PHP_VERSION_ID >= 80100 && is_object($var) && enum_exists(get_class($var))) { return $var; } From 07364389e116387a81545f31854da87ebcff5b0e Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Thu, 3 Mar 2022 09:18:48 +0100 Subject: [PATCH 4/4] Remove redundant check --- src/DeepCopy/DeepCopy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 9aeea6d..5e68c64 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -141,7 +141,7 @@ private function recursiveCopy($var) } // Enum - if (PHP_VERSION_ID >= 80100 && is_object($var) && enum_exists(get_class($var))) { + if (PHP_VERSION_ID >= 80100 && enum_exists(get_class($var))) { return $var; }