From 628cc7645d38d7d443bb35fd5480e910350af67c Mon Sep 17 00:00:00 2001 From: abderrazzak oxa <44305005+abderrazzak-oxa@users.noreply.github.com> Date: Sun, 6 Sep 2020 18:53:55 +0200 Subject: [PATCH] Fix Problem with non initialization property in 7.4 PHP (#77) --- src/SerializableClosure.php | 12 ++++++++++ tests/ReflectionClosure5Test.php | 40 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/SerializableClosure.php b/src/SerializableClosure.php index ed8f9b5..f61c6ae 100644 --- a/src/SerializableClosure.php +++ b/src/SerializableClosure.php @@ -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); @@ -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); @@ -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( @@ -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); diff --git a/tests/ReflectionClosure5Test.php b/tests/ReflectionClosure5Test.php index f42115e..acdc675 100644 --- a/tests/ReflectionClosure5Test.php +++ b/tests/ReflectionClosure5Test.php @@ -143,4 +143,42 @@ public function testSerialize() $this->assertEquals(40, $c3(4)); $this->assertEquals(48, $c3(4, 6)); } -} \ No newline at end of file + + 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; + } +}