Skip to content

Commit 579bb73

Browse files
authored
Merge pull request #144 from SlvrEagle23/php74_compat
#143 -- Add isInitialized check for PHP 7.4 typed properties.
2 parents 9012edb + 7745e2f commit 579bb73

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
- '7.1'
1111
- '7.2'
1212
- '7.3'
13+
- '7.4'
1314
- nightly
1415

1516
matrix:

fixtures/f009/TypedProperty.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DeepCopy\f009;
4+
5+
class TypedProperty
6+
{
7+
public int $foo;
8+
}

src/DeepCopy/DeepCopy.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ function ($object) {
237237
}
238238

239239
$property->setAccessible(true);
240+
241+
// Ignore uninitialized properties (for PHP >7.4)
242+
if (method_exists($property, 'isInitialized') && !$property->isInitialized($object)) {
243+
return;
244+
}
245+
240246
$propertyValue = $property->getValue($object);
241247

242248
// Copy the property

tests/DeepCopyTest/DeepCopyTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use DeepCopy\f006;
1717
use DeepCopy\f007;
1818
use DeepCopy\f008;
19+
use DeepCopy\f009;
1920
use DeepCopy\Filter\KeepFilter;
2021
use DeepCopy\Filter\SetNullFilter;
2122
use DeepCopy\Matcher\PropertyNameMatcher;
@@ -434,6 +435,35 @@ public function test_it_can_prepend_filter()
434435
$this->assertNull($copy->getFoo());
435436
}
436437

438+
/**
439+
* @ticket https://github.com/myclabs/DeepCopy/issues/143
440+
* @requires PHP 7.4
441+
*/
442+
public function test_it_clones_typed_properties()
443+
{
444+
$object = new f009\TypedProperty();
445+
$object->foo = 123;
446+
447+
$deepCopy = new DeepCopy();
448+
$copy = $deepCopy->copy($object);
449+
450+
$this->assertSame(123, $copy->foo);
451+
}
452+
453+
/**
454+
* @ticket https://github.com/myclabs/DeepCopy/issues/143
455+
* @requires PHP 7.4
456+
*/
457+
public function test_it_ignores_uninitialized_typed_properties()
458+
{
459+
$object = new f009\TypedProperty();
460+
461+
$deepCopy = new DeepCopy();
462+
$copy = $deepCopy->copy($object);
463+
464+
$this->assertFalse(isset($copy->foo));
465+
}
466+
437467
private function assertEqualButNotSame($expected, $val)
438468
{
439469
$this->assertEquals($expected, $val);

0 commit comments

Comments
 (0)