Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unable to serialize PHP8.4 object with virtual property #108

Merged
merged 6 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}