Skip to content

Commit

Permalink
Merge pull request #144 from SlvrEagle23/php74_compat
Browse files Browse the repository at this point in the history
#143 -- Add isInitialized check for PHP 7.4 typed properties.
  • Loading branch information
mnapoli authored Dec 15, 2019
2 parents 9012edb + 7745e2f commit 579bb73
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- nightly

matrix:
Expand Down
8 changes: 8 additions & 0 deletions fixtures/f009/TypedProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace DeepCopy\f009;

class TypedProperty
{
public int $foo;
}
6 changes: 6 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ function ($object) {
}

$property->setAccessible(true);

// Ignore uninitialized properties (for PHP >7.4)
if (method_exists($property, 'isInitialized') && !$property->isInitialized($object)) {
return;
}

$propertyValue = $property->getValue($object);

// Copy the property
Expand Down
30 changes: 30 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DeepCopy\f006;
use DeepCopy\f007;
use DeepCopy\f008;
use DeepCopy\f009;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\PropertyNameMatcher;
Expand Down Expand Up @@ -434,6 +435,35 @@ public function test_it_can_prepend_filter()
$this->assertNull($copy->getFoo());
}

/**
* @ticket https://github.com/myclabs/DeepCopy/issues/143
* @requires PHP 7.4
*/
public function test_it_clones_typed_properties()
{
$object = new f009\TypedProperty();
$object->foo = 123;

$deepCopy = new DeepCopy();
$copy = $deepCopy->copy($object);

$this->assertSame(123, $copy->foo);
}

/**
* @ticket https://github.com/myclabs/DeepCopy/issues/143
* @requires PHP 7.4
*/
public function test_it_ignores_uninitialized_typed_properties()
{
$object = new f009\TypedProperty();

$deepCopy = new DeepCopy();
$copy = $deepCopy->copy($object);

$this->assertFalse(isset($copy->foo));
}

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

0 comments on commit 579bb73

Please sign in to comment.