Skip to content

Commit

Permalink
Fix Problem with non initialization property in 7.4 PHP (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrzakoxa authored Sep 6, 2020
1 parent e8d34df commit 628cc76
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/SerializableClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ public static function wrapClosures(&$data, SplObjectStorage $storage = null)
continue;
}
$property->setAccessible(true);
if (PHP_VERSION >= 7.4 && !$property->isInitialized($instance)) {
continue;
}
$value = $property->getValue($instance);
if(is_array($value) || is_object($value)){
static::wrapClosures($value, $storage);
Expand Down Expand Up @@ -480,6 +483,9 @@ public static function unwrapClosures(&$data, SplObjectStorage $storage = null)
continue;
}
$property->setAccessible(true);
if (PHP_VERSION >= 7.4 && !$property->isInitialized($data)) {
continue;
}
$value = $property->getValue($data);
if(is_array($value) || is_object($value)){
static::unwrapClosures($value, $storage);
Expand Down Expand Up @@ -561,6 +567,9 @@ protected function mapPointers(&$data)
continue;
}
$property->setAccessible(true);
if (PHP_VERSION >= 7.4 && !$property->isInitialized($data)) {
continue;
}
$item = $property->getValue($data);
if ($item instanceof SerializableClosure || ($item instanceof SelfReference && $item->hash === $this->code['self'])) {
$this->code['objects'][] = array(
Expand Down Expand Up @@ -653,6 +662,9 @@ protected function mapByReference(&$data)
continue;
}
$property->setAccessible(true);
if (PHP_VERSION >= 7.4 && !$property->isInitialized($instance)) {
continue;
}
$value = $property->getValue($instance);
if(is_array($value) || is_object($value)){
$this->mapByReference($value);
Expand Down
40 changes: 39 additions & 1 deletion tests/ReflectionClosure5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,42 @@ public function testSerialize()
$this->assertEquals(40, $c3(4));
$this->assertEquals(48, $c3(4, 6));
}
}

public function testTypedProperties()
{
$user = new User();
$s = $this->s(function () use ($user) {
return true;
});
$this->assertTrue($s());

$user = new User();
$product = new Product();
$product->name = "PC";
$user->setProduct($product);

$u = $this->s(function () use ($user) {
return $user->getProduct()->name;
});

$this->assertEquals('PC', $u());
}
}

class Product {
public string $name;
}

class User {
protected Product $product;

public function getProduct(): Product
{
return $this->product;
}

public function setProduct(Product $product): void
{
$this->product = $product;
}
}

0 comments on commit 628cc76

Please sign in to comment.