From 57f23652f23c919461a6c1323d3ec5db638b7c79 Mon Sep 17 00:00:00 2001 From: llupa <41073314+llupa@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:59:24 +0200 Subject: [PATCH 1/4] Ignore `readonly` properties --- src/DeepCopy/DeepCopy.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 6e766d8..345005f 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -224,6 +224,11 @@ private function copyObjectProperty($object, ReflectionProperty $property) return; } + // Ignore readonly properties + if (method_exists($property, 'isReadOnly') && !$property->isReadOnly()) { + return; + } + // Apply the filters foreach ($this->filters as $item) { /** @var Matcher $matcher */ From 4e89dc39b79219a1620dba68d8b6c469e686e56f Mon Sep 17 00:00:00 2001 From: llupa <41073314+llupa@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:20:54 +0000 Subject: [PATCH 2/4] Ignore `readonly` properties --- src/DeepCopy/DeepCopy.php | 2 +- tests/DeepCopyTest/DeepCopyTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 345005f..084858e 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -225,7 +225,7 @@ private function copyObjectProperty($object, ReflectionProperty $property) } // Ignore readonly properties - if (method_exists($property, 'isReadOnly') && !$property->isReadOnly()) { + if (method_exists($property, 'isReadOnly') && $property->isReadOnly()) { return; } diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index 0b44648..b3112f8 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -542,6 +542,27 @@ public function test_it_can_copy_property_after_applying_doctrine_proxy_filter_w $this->assertNotEquals($copy->getFoo(), $object->getFoo()); } + /** + * @requires PHP 8.1 + */ + public function test_it_can_copy_object_with_private_property() + { + $object = new class extends \stdClass { + public readonly string $foo; + + public function __construct() + { + $this->foo = 'foo'; + } + }; + + $deepCopy = new DeepCopy(); + + $copy = $deepCopy->copy($object); + + $this->assertEqualButNotSame($object, $copy); + } + private function assertEqualButNotSame($expected, $val) { $this->assertEquals($expected, $val); From 156b10c70a0868d2cdf6d3b1588fb2a02d1fbd57 Mon Sep 17 00:00:00 2001 From: llupa <41073314+llupa@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:22:29 +0200 Subject: [PATCH 3/4] Update DeepCopyTest.php --- tests/DeepCopyTest/DeepCopyTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index b3112f8..a69c4c6 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -545,7 +545,7 @@ public function test_it_can_copy_property_after_applying_doctrine_proxy_filter_w /** * @requires PHP 8.1 */ - public function test_it_can_copy_object_with_private_property() + public function test_it_can_copy_object_with_readonly_property() { $object = new class extends \stdClass { public readonly string $foo; From 3306d5f386e40e82a5caa50ce9b1b0706230190e Mon Sep 17 00:00:00 2001 From: llupa Date: Wed, 12 Jun 2024 15:05:32 +0200 Subject: [PATCH 4/4] Add test fixtures --- fixtures/f014/ReadonlyObjectProperty.php | 13 +++++++++++++ fixtures/f014/ReadonlyScalarProperty.php | 17 +++++++++++++++++ tests/DeepCopyTest/DeepCopyTest.php | 17 +++++++---------- 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 fixtures/f014/ReadonlyObjectProperty.php create mode 100644 fixtures/f014/ReadonlyScalarProperty.php diff --git a/fixtures/f014/ReadonlyObjectProperty.php b/fixtures/f014/ReadonlyObjectProperty.php new file mode 100644 index 0000000..4e0c78e --- /dev/null +++ b/fixtures/f014/ReadonlyObjectProperty.php @@ -0,0 +1,13 @@ +foo = new ReadonlyScalarProperty(); + } +} \ No newline at end of file diff --git a/fixtures/f014/ReadonlyScalarProperty.php b/fixtures/f014/ReadonlyScalarProperty.php new file mode 100644 index 0000000..d21be67 --- /dev/null +++ b/fixtures/f014/ReadonlyScalarProperty.php @@ -0,0 +1,17 @@ +foo = 'foo'; + $this->bar = 1; + $this->baz = []; + } +} \ No newline at end of file diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index a69c4c6..8890391 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -21,6 +21,7 @@ use DeepCopy\f011; use DeepCopy\f012\Suit; use DeepCopy\f013; +use DeepCopy\f014; use DeepCopy\Filter\ChainableFilter; use DeepCopy\Filter\Doctrine\DoctrineProxyFilter; use DeepCopy\Filter\KeepFilter; @@ -547,20 +548,16 @@ public function test_it_can_copy_property_after_applying_doctrine_proxy_filter_w */ public function test_it_can_copy_object_with_readonly_property() { - $object = new class extends \stdClass { - public readonly string $foo; - - public function __construct() - { - $this->foo = 'foo'; - } - }; + $scalarProperties = new f014\ReadonlyScalarProperty(); + $objectProperties = new f014\ReadonlyObjectProperty(); $deepCopy = new DeepCopy(); - $copy = $deepCopy->copy($object); + $scalarPropertiesCopy = $deepCopy->copy($scalarProperties); + $objectPropertiesCopy = $deepCopy->copy($objectProperties); - $this->assertEqualButNotSame($object, $copy); + $this->assertEqualButNotSame($scalarProperties, $scalarPropertiesCopy); + $this->assertEqualButNotSame($objectProperties, $objectPropertiesCopy); } private function assertEqualButNotSame($expected, $val)