Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore readonly properties #195

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions fixtures/f014/ReadonlyObjectProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace DeepCopy\f014;

class ReadonlyObjectProperty
{
public readonly ReadonlyScalarProperty $foo;

public function __construct()
{
$this->foo = new ReadonlyScalarProperty();
}
}
17 changes: 17 additions & 0 deletions fixtures/f014/ReadonlyScalarProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace DeepCopy\f014;

class ReadonlyScalarProperty
{
public readonly string $foo;
public readonly int $bar;
public readonly array $baz;

public function __construct()
{
$this->foo = 'foo';
$this->bar = 1;
$this->baz = [];
}
}
5 changes: 5 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
18 changes: 18 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -542,6 +543,23 @@ 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_readonly_property()
{
$scalarProperties = new f014\ReadonlyScalarProperty();
$objectProperties = new f014\ReadonlyObjectProperty();

$deepCopy = new DeepCopy();

$scalarPropertiesCopy = $deepCopy->copy($scalarProperties);
$objectPropertiesCopy = $deepCopy->copy($objectProperties);

$this->assertEqualButNotSame($scalarProperties, $scalarPropertiesCopy);
$this->assertEqualButNotSame($objectProperties, $objectPropertiesCopy);
}

private function assertEqualButNotSame($expected, $val)
{
$this->assertEquals($expected, $val);
Expand Down