Skip to content

Commit f379c13

Browse files
authored
Fix unable to serialize PHP8.4 object with virtual property (#108)
* wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 7a2bf96 commit f379c13

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ php:
33
preset: laravel
44
finder:
55
not-name:
6+
- Php84Test.php
67
- ReflectionClosurePhp80Test.php
78
- ReflectionClosurePhp81Test.php
89
- SerializerPhp81Test.php

phpunit.xml.dist

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,22 @@
1111
backupStaticProperties="false">
1212
<testsuites>
1313
<testsuite name="Tests">
14-
<directory>tests</directory>
14+
<file>tests/ReflectionClosure1Test.php</file>
15+
<file>tests/ReflectionClosure2Test.php</file>
16+
<file>tests/ReflectionClosure3Test.php</file>
17+
<file>tests/ReflectionClosure4Test.php</file>
18+
<file>tests/ReflectionClosure5Test.php</file>
19+
<file>tests/SerializerTest.php</file>
20+
<file>tests/SignedSerializerTest.php</file>
21+
</testsuite>
22+
<testsuite name="php81">
23+
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/ReflectionClosurePhp80Test.php</file>
24+
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/ReflectionClosurePhp81Test.php</file>
25+
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/SerializerPhp80Test.php</file>
26+
<file phpVersion="8.1.0" phpVersionOperator=">=">tests/SerializerPhp81Test.php</file>
27+
</testsuite>
28+
<testsuite name="php84">
29+
<file phpVersion="8.4.0" phpVersionOperator=">=">tests/Php84Test.php</file>
1530
</testsuite>
1631
</testsuites>
1732
</phpunit>

src/Serializers/Native.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Laravel\SerializableClosure\Support\SelfReference;
1313
use Laravel\SerializableClosure\UnsignedSerializableClosure;
1414
use ReflectionObject;
15+
use ReflectionProperty;
1516
use UnitEnum;
1617

1718
class Native implements Serializable
@@ -490,7 +491,7 @@ protected function mapByReference(&$data)
490491
}
491492

492493
foreach ($reflection->getProperties() as $property) {
493-
if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined()) {
494+
if ($property->isStatic() || ! $property->getDeclaringClass()->isUserDefined() || $this->isVirtualProperty($property)) {
494495
continue;
495496
}
496497

@@ -511,4 +512,15 @@ protected function mapByReference(&$data)
511512
} while ($reflection = $reflection->getParentClass());
512513
}
513514
}
515+
516+
/**
517+
* Determine is virtual property.
518+
*
519+
* @param \ReflectionProperty $property
520+
* @return bool
521+
*/
522+
protected function isVirtualProperty(ReflectionProperty $property): bool
523+
{
524+
return method_exists($property, 'isVirtual') && $property->isVirtual();
525+
}
514526
}

tests/Php84Test.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
it('can serialize class with virtual property', function () {
4+
$c = new VirtualPropWithPhp84();
5+
6+
$f = function () use ($c) {
7+
return $c;
8+
};
9+
10+
$s1 = s($f);
11+
12+
expect('test')->toBe($s1()->test);
13+
})->with('serializers');
14+
15+
class VirtualPropWithPhp84 {
16+
public string $test {
17+
get => 'test';
18+
}
19+
}

0 commit comments

Comments
 (0)