Skip to content

Commit

Permalink
Fix unable to serialize PHP8.4 object with virtual property (#108)
Browse files Browse the repository at this point in the history
* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Feb 11, 2025
1 parent 7a2bf96 commit f379c13
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ php:
preset: laravel
finder:
not-name:
- Php84Test.php
- ReflectionClosurePhp80Test.php
- ReflectionClosurePhp81Test.php
- SerializerPhp81Test.php
17 changes: 16 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
backupStaticProperties="false">
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
<file>tests/ReflectionClosure1Test.php</file>
<file>tests/ReflectionClosure2Test.php</file>
<file>tests/ReflectionClosure3Test.php</file>
<file>tests/ReflectionClosure4Test.php</file>
<file>tests/ReflectionClosure5Test.php</file>
<file>tests/SerializerTest.php</file>
<file>tests/SignedSerializerTest.php</file>
</testsuite>
<testsuite name="php81">
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/ReflectionClosurePhp80Test.php</file>
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/ReflectionClosurePhp81Test.php</file>
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/SerializerPhp80Test.php</file>
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/SerializerPhp81Test.php</file>
</testsuite>
<testsuite name="php84">
<file phpVersion="8.4.0" phpVersionOperator=">=">tests/Php84Test.php</file>
</testsuite>
</testsuites>
</phpunit>
14 changes: 13 additions & 1 deletion src/Serializers/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laravel\SerializableClosure\Support\SelfReference;
use Laravel\SerializableClosure\UnsignedSerializableClosure;
use ReflectionObject;
use ReflectionProperty;
use UnitEnum;

class Native implements Serializable
Expand Down Expand Up @@ -490,7 +491,7 @@ protected function mapByReference(&$data)
}

foreach ($reflection->getProperties() as $property) {
if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined()) {
if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined() || $this->isVirtualProperty($property)) {
continue;
}

Expand All @@ -511,4 +512,15 @@ protected function mapByReference(&$data)
} while ($reflection = $reflection->getParentClass());
}
}

/**
* Determine is virtual property.
*
* @param \ReflectionProperty $property
* @return bool
*/
protected function isVirtualProperty(ReflectionProperty $property): bool
{
return method_exists($property, 'isVirtual') && $property->isVirtual();
}
}
19 changes: 19 additions & 0 deletions tests/Php84Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

it('can serialize class with virtual property', function () {
$c = new VirtualPropWithPhp84();

$f = function () use ($c) {
return $c;
};

$s1 = s($f);

expect('test')->toBe($s1()->test);
})->with('serializers');

class VirtualPropWithPhp84 {
public string $test {
get => 'test';
}
}

0 comments on commit f379c13

Please sign in to comment.