From 27c6110a514efe07bf025e4c4beee06bf53ab1cc Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Wed, 7 Feb 2024 15:35:31 +0100 Subject: [PATCH 01/13] chore: fix ci deprecations add testruns for PHP 8.2 and 8.3 --- .github/workflows/ci.yaml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7b00964..671f60d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,7 +20,7 @@ jobs: steps: - name: "Checkout repository" - uses: "actions/checkout@v3" + uses: "actions/checkout@v4" - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -31,10 +31,10 @@ jobs: - name: "Get composer cache directory" id: "composercache" - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: "Cache dependencies" - uses: "actions/cache@v3" + uses: "actions/cache@v4" with: path: ${{ steps.composercache.outputs.dir }} key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-${{ hashFiles('**/composer.json') }} @@ -63,13 +63,15 @@ jobs: - "7.4" - "8.0" - "8.1" + - "8.2" + - "8.3" dependencies: - "lowest" - "highest" steps: - name: "Checkout repository" - uses: "actions/checkout@v3" + uses: "actions/checkout@v4" - name: "Install PHP" uses: "shivammathur/setup-php@v2" @@ -79,10 +81,10 @@ jobs: - name: "Get composer cache directory" id: "composercache" - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: "Cache dependencies" - uses: "actions/cache@v3" + uses: "actions/cache@v4" with: path: ${{ steps.composercache.outputs.dir }} key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-${{ hashFiles('**/composer.json') }} 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 02/13] 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 03/13] 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 04/13] 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 05/13] 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) From 47aebd6a159e62635fa08ee00476792a7dc1a826 Mon Sep 17 00:00:00 2001 From: michalananapps <132371766+michalananapps@users.noreply.github.com> Date: Wed, 6 Nov 2024 14:34:57 +0100 Subject: [PATCH 06/13] bug: #196 Introduce a custom DatePeriodFilter that safely copies DatePeriod objects without modifying their readonly properties. --- src/DeepCopy/DeepCopy.php | 3 + .../TypeFilter/Date/DatePeriodFilter.php | 36 ++++++++++ tests/DeepCopyTest/DeepCopyTest.php | 3 + .../TypeFilter/Date/DatePeriodFilterTest.php | 67 +++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php create mode 100644 tests/DeepCopyTest/TypeFilter/Date/DatePeriodFilterTest.php diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 084858e..f739d92 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -4,6 +4,7 @@ use ArrayObject; use DateInterval; +use DatePeriod; use DateTimeInterface; use DateTimeZone; use DeepCopy\Exception\CloneException; @@ -12,6 +13,7 @@ use DeepCopy\Matcher\Matcher; use DeepCopy\Reflection\ReflectionHelper; use DeepCopy\TypeFilter\Date\DateIntervalFilter; +use DeepCopy\TypeFilter\Date\DatePeriodFilter; use DeepCopy\TypeFilter\Spl\ArrayObjectFilter; use DeepCopy\TypeFilter\Spl\SplDoublyLinkedListFilter; use DeepCopy\TypeFilter\TypeFilter; @@ -64,6 +66,7 @@ public function __construct($useCloneMethod = false) $this->addTypeFilter(new ArrayObjectFilter($this), new TypeMatcher(ArrayObject::class)); $this->addTypeFilter(new DateIntervalFilter(), new TypeMatcher(DateInterval::class)); + $this->addTypeFilter(new DatePeriodFilter(), new TypeMatcher(DatePeriod::class)); $this->addTypeFilter(new SplDoublyLinkedListFilter($this), new TypeMatcher(SplDoublyLinkedList::class)); } diff --git a/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php b/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php new file mode 100644 index 0000000..82532b3 --- /dev/null +++ b/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php @@ -0,0 +1,36 @@ += 80200 && $element->include_end_date) { + $options |= DatePeriod::INCLUDE_END_DATE; + } + if (!$element->include_start_date) { + $options |= DatePeriod::EXCLUDE_START_DATE; + } + + if ($element->getEndDate()) { + return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $element->getEndDate(), $options); + } + + return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $element->getRecurrences(), $options); + } +} diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index 8890391..2b31536 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -4,6 +4,7 @@ use ArrayObject; use DateInterval; +use DatePeriod; use DateTime; use DateTimeImmutable; use DateTimeZone; @@ -158,6 +159,7 @@ public function test_it_can_copy_an_object_with_a_date_object_property() $object->d2 = new DateTimeImmutable(); $object->dtz = new DateTimeZone('UTC'); $object->di = new DateInterval('P2D'); + $object->dp = new DatePeriod(new DateTime(), new DateInterval('P2D'), 3); $copy = deep_copy($object); @@ -165,6 +167,7 @@ public function test_it_can_copy_an_object_with_a_date_object_property() $this->assertEqualButNotSame($object->d2, $copy->d2); $this->assertEqualButNotSame($object->dtz, $copy->dtz); $this->assertEqualButNotSame($object->di, $copy->di); + $this->assertEqualButNotSame($object->dp, $copy->dp); } /** diff --git a/tests/DeepCopyTest/TypeFilter/Date/DatePeriodFilterTest.php b/tests/DeepCopyTest/TypeFilter/Date/DatePeriodFilterTest.php new file mode 100644 index 0000000..e816aa1 --- /dev/null +++ b/tests/DeepCopyTest/TypeFilter/Date/DatePeriodFilterTest.php @@ -0,0 +1,67 @@ +apply($object); + + $this->assertEquals($object, $copy); + $this->assertNotSame($object, $copy); + } + + public function test_it_deep_copies_a_DatePeriod_with_exclude_start_date() + { + $object = new DatePeriod(new DateTime(), new DateInterval('P2D'), 3, DatePeriod::EXCLUDE_START_DATE); + + $filter = new DatePeriodFilter(); + + $copy = $filter->apply($object); + + $this->assertEquals($object, $copy); + $this->assertNotSame($object, $copy); + } + + /** + * @requires PHP 8.2 + */ + public function test_it_deep_copies_a_DatePeriod_with_include_end_date() + { + $object = new DatePeriod(new DateTime(), new DateInterval('P2D'), 3, DatePeriod::INCLUDE_END_DATE); + + $filter = new DatePeriodFilter(); + + $copy = $filter->apply($object); + + $this->assertEquals($object, $copy); + $this->assertNotSame($object, $copy); + } + + public function test_it_deep_copies_a_DatePeriod_with_end_date() + { + $object = new DatePeriod(new DateTime(), new DateInterval('P2D'), new DateTime('+2 days')); + + $filter = new DatePeriodFilter(); + + $copy = $filter->apply($object); + + $this->assertEquals($object, $copy); + $this->assertNotSame($object, $copy); + } +} From bed190bf6727048f666e7e60151b2572096778ab Mon Sep 17 00:00:00 2001 From: michalananapps <132371766+michalananapps@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:48:22 +0100 Subject: [PATCH 07/13] bug: #196 support php7.1 --- src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php b/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php index 82532b3..6bd2f7e 100644 --- a/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php +++ b/src/DeepCopy/TypeFilter/Date/DatePeriodFilter.php @@ -31,6 +31,12 @@ public function apply($element) return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $element->getEndDate(), $options); } - return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $element->getRecurrences(), $options); + if (PHP_VERSION_ID >= 70217) { + $recurrences = $element->getRecurrences(); + } else { + $recurrences = $element->recurrences - $element->include_start_date; + } + + return new DatePeriod($element->getStartDate(), $element->getDateInterval(), $recurrences, $options); } } From 67ebab1470064e86ccb959a6c9efd0ceecad1c47 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Mon, 25 Nov 2024 19:44:07 +0100 Subject: [PATCH 08/13] Run tests on PHP 8.4 --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 671f60d..e8ca429 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -65,6 +65,7 @@ jobs: - "8.1" - "8.2" - "8.3" + - "8.4" dependencies: - "lowest" - "highest" From 2356a16814929085f162cf690479f6e479886aab Mon Sep 17 00:00:00 2001 From: Alex Zamponi <562324+alexz707@users.noreply.github.com> Date: Tue, 11 Feb 2025 16:54:17 +0100 Subject: [PATCH 09/13] Add PrependTypeFilter method --- src/DeepCopy/DeepCopy.php | 8 ++++++++ tests/DeepCopyTest/DeepCopyTest.php | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index f739d92..cc82df6 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -122,6 +122,14 @@ public function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher) ]; } + public function prependTypeFilter(TypeFilter $filter, TypeMatcher $matcher) + { + array_unshift($this->typeFilters, [ + 'matcher' => $matcher, + 'filter' => $filter, + ]); + } + private function recursiveCopy($var) { // Matches Type Filter diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index 2b31536..ae35cbc 100644 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -30,6 +30,7 @@ use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher; use DeepCopy\Matcher\PropertyNameMatcher; use DeepCopy\Matcher\PropertyTypeMatcher; +use DeepCopy\TypeFilter\ReplaceFilter; use DeepCopy\TypeFilter\ShallowCopyFilter; use DeepCopy\TypeMatcher\TypeMatcher; use PHPUnit\Framework\TestCase; @@ -475,6 +476,21 @@ public function test_it_can_prepend_filter() $this->assertNull($copy->getFoo()); } + public function test_it_can_prepend_type_filter() + { + $object = new f008\A('bar'); + $deepCopy = new DeepCopy(); + $deepCopy->addTypeFilter(new ReplaceFilter(function ($object) { + return new f008\A('baz'); + }), new TypeMatcher(f008\A::class)); + $deepCopy->prependTypeFilter(new ReplaceFilter(function ($object) { + return new f008\A('foo'); + }), new TypeMatcher(f008\A::class)); + + $copy = $deepCopy->copy($object); + $this->assertEquals('foo',$copy->getFoo()); + } + /** * @ticket https://github.com/myclabs/DeepCopy/issues/143 * @requires PHP 7.4 From 3424b4d8b08835c075dcc8f4a1bf386e8fcc2180 Mon Sep 17 00:00:00 2001 From: SpazzMarticus Date: Tue, 29 Apr 2025 08:21:16 +0200 Subject: [PATCH 10/13] Fix return types for Doctrine\Persistence\Proxy implementations PHP Fatal error: Declaration of DeepCopyTest\Matcher\Doctrine\FooProxy::__load() must be compatible with Doctrine\Persistence\Proxy::__load(): void --- fixtures/f013/A.php | 6 ++++-- fixtures/f013/B.php | 6 ++++-- .../Matcher/Doctrine/DoctrineProxyMatcherTest.php | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/fixtures/f013/A.php b/fixtures/f013/A.php index aa0393d..ac70988 100644 --- a/fixtures/f013/A.php +++ b/fixtures/f013/A.php @@ -2,6 +2,7 @@ namespace DeepCopy\f013; +use BadMethodCallException; use Doctrine\Persistence\Proxy; class A implements Proxy @@ -11,14 +12,15 @@ class A implements Proxy /** * @inheritdoc */ - public function __load() + public function __load(): void { } /** * @inheritdoc */ - public function __isInitialized() + public function __isInitialized(): bool { + throw new BadMethodCallException(); } } diff --git a/fixtures/f013/B.php b/fixtures/f013/B.php index 89425d8..d1382e6 100644 --- a/fixtures/f013/B.php +++ b/fixtures/f013/B.php @@ -2,6 +2,7 @@ namespace DeepCopy\f013; +use BadMethodCallException; use Doctrine\Persistence\Proxy; class B implements Proxy @@ -11,15 +12,16 @@ class B implements Proxy /** * @inheritdoc */ - public function __load() + public function __load(): void { } /** * @inheritdoc */ - public function __isInitialized() + public function __isInitialized(): bool { + throw new BadMethodCallException(); } public function getFoo() diff --git a/tests/DeepCopyTest/Matcher/Doctrine/DoctrineProxyMatcherTest.php b/tests/DeepCopyTest/Matcher/Doctrine/DoctrineProxyMatcherTest.php index 22e380d..38de911 100644 --- a/tests/DeepCopyTest/Matcher/Doctrine/DoctrineProxyMatcherTest.php +++ b/tests/DeepCopyTest/Matcher/Doctrine/DoctrineProxyMatcherTest.php @@ -39,7 +39,7 @@ class FooProxy implements Proxy /** * @inheritdoc */ - public function __load() + public function __load(): void { throw new BadMethodCallException(); } @@ -47,7 +47,7 @@ public function __load() /** * @inheritdoc */ - public function __isInitialized() + public function __isInitialized(): bool { throw new BadMethodCallException(); } From f13e8ee6ea859137f8b7a20077c0931bf112e1a3 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Thu, 3 Jul 2025 15:55:33 +0200 Subject: [PATCH 11/13] Add generic to DeepCopy::copy method This makes it possible for PHPStan to understand the return type. --- src/DeepCopy/DeepCopy.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index cc82df6..5456ec8 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -87,9 +87,11 @@ public function skipUncloneable($skipUncloneable = true) /** * Deep copies the given object. * - * @param mixed $object + * @template TObject of object * - * @return mixed + * @param TObject $object + * + * @return TObject */ public function copy($object) { From 4f05aba9561c74d419e19a9b307b0fa038a4429f Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Sat, 5 Jul 2025 08:56:27 +0200 Subject: [PATCH 12/13] Change TObject to mixed See https://github.com/myclabs/DeepCopy/pull/205#issuecomment-3038236873 --- 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 5456ec8..421e9a3 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -87,7 +87,7 @@ public function skipUncloneable($skipUncloneable = true) /** * Deep copies the given object. * - * @template TObject of object + * @template TObject * * @param TObject $object * From 8c42cfd41b5b01d33e4809e2255a2625b642eb15 Mon Sep 17 00:00:00 2001 From: Worma Date: Fri, 1 Aug 2025 06:08:20 +0200 Subject: [PATCH 13/13] setAccessible() has no effect as of PHP 8.1 --- src/DeepCopy/DeepCopy.php | 4 +++- src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php | 4 +++- .../Filter/Doctrine/DoctrineEmptyCollectionFilter.php | 4 +++- src/DeepCopy/Filter/ReplaceFilter.php | 4 +++- src/DeepCopy/Filter/SetNullFilter.php | 4 +++- src/DeepCopy/Matcher/PropertyTypeMatcher.php | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index 421e9a3..a944697 100644 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -267,7 +267,9 @@ function ($object) { } } - $property->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $property->setAccessible(true); + } // Ignore uninitialized properties (for PHP >7.4) if (method_exists($property, 'isInitialized') && !$property->isInitialized($object)) { diff --git a/src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php b/src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php index e6d9377..66e91e5 100644 --- a/src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php +++ b/src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php @@ -19,7 +19,9 @@ public function apply($object, $property, $objectCopier) { $reflectionProperty = ReflectionHelper::getProperty($object, $property); - $reflectionProperty->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $reflectionProperty->setAccessible(true); + } $oldCollection = $reflectionProperty->getValue($object); $newCollection = $oldCollection->map( diff --git a/src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php b/src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php index 7b33fd5..fa1c034 100644 --- a/src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php +++ b/src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php @@ -21,7 +21,9 @@ class DoctrineEmptyCollectionFilter implements Filter public function apply($object, $property, $objectCopier) { $reflectionProperty = ReflectionHelper::getProperty($object, $property); - $reflectionProperty->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $reflectionProperty->setAccessible(true); + } $reflectionProperty->setValue($object, new ArrayCollection()); } diff --git a/src/DeepCopy/Filter/ReplaceFilter.php b/src/DeepCopy/Filter/ReplaceFilter.php index 7aca593..fda8e72 100644 --- a/src/DeepCopy/Filter/ReplaceFilter.php +++ b/src/DeepCopy/Filter/ReplaceFilter.php @@ -30,7 +30,9 @@ public function __construct(callable $callable) public function apply($object, $property, $objectCopier) { $reflectionProperty = ReflectionHelper::getProperty($object, $property); - $reflectionProperty->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $reflectionProperty->setAccessible(true); + } $value = call_user_func($this->callback, $reflectionProperty->getValue($object)); diff --git a/src/DeepCopy/Filter/SetNullFilter.php b/src/DeepCopy/Filter/SetNullFilter.php index bea86b8..6722272 100644 --- a/src/DeepCopy/Filter/SetNullFilter.php +++ b/src/DeepCopy/Filter/SetNullFilter.php @@ -18,7 +18,9 @@ public function apply($object, $property, $objectCopier) { $reflectionProperty = ReflectionHelper::getProperty($object, $property); - $reflectionProperty->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $reflectionProperty->setAccessible(true); + } $reflectionProperty->setValue($object, null); } } diff --git a/src/DeepCopy/Matcher/PropertyTypeMatcher.php b/src/DeepCopy/Matcher/PropertyTypeMatcher.php index c7f4690..7980bfa 100644 --- a/src/DeepCopy/Matcher/PropertyTypeMatcher.php +++ b/src/DeepCopy/Matcher/PropertyTypeMatcher.php @@ -39,7 +39,9 @@ public function matches($object, $property) return false; } - $reflectionProperty->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $reflectionProperty->setAccessible(true); + } // Uninitialized properties (for PHP >7.4) if (method_exists($reflectionProperty, 'isInitialized') && !$reflectionProperty->isInitialized($object)) {