Skip to content

Commit 4e89dc3

Browse files
committed
Ignore readonly properties
1 parent 57f2365 commit 4e89dc3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/DeepCopy/DeepCopy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private function copyObjectProperty($object, ReflectionProperty $property)
225225
}
226226

227227
// Ignore readonly properties
228-
if (method_exists($property, 'isReadOnly') && !$property->isReadOnly()) {
228+
if (method_exists($property, 'isReadOnly') && $property->isReadOnly()) {
229229
return;
230230
}
231231

tests/DeepCopyTest/DeepCopyTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,27 @@ public function test_it_can_copy_property_after_applying_doctrine_proxy_filter_w
542542
$this->assertNotEquals($copy->getFoo(), $object->getFoo());
543543
}
544544

545+
/**
546+
* @requires PHP 8.1
547+
*/
548+
public function test_it_can_copy_object_with_private_property()
549+
{
550+
$object = new class extends \stdClass {
551+
public readonly string $foo;
552+
553+
public function __construct()
554+
{
555+
$this->foo = 'foo';
556+
}
557+
};
558+
559+
$deepCopy = new DeepCopy();
560+
561+
$copy = $deepCopy->copy($object);
562+
563+
$this->assertEqualButNotSame($object, $copy);
564+
}
565+
545566
private function assertEqualButNotSame($expected, $val)
546567
{
547568
$this->assertEquals($expected, $val);

0 commit comments

Comments
 (0)