Skip to content

Commit

Permalink
Fix bug related to readonly properties (#87)
Browse files Browse the repository at this point in the history
* fix: Fix bug related to readonly properties

* feat: Skip readonly properties for PHP below 8.1

* test: Add test for readonly properties from parent scope variable
  • Loading branch information
rust17 authored Aug 1, 2024
1 parent d715a63 commit b88b3bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Serializers/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ protected function mapPointers(&$data)
continue;
}

if (PHP_VERSION >= 8.1 && $property->isReadOnly()) {
continue;
}

$item = $property->getValue($data);

if ($item instanceof SerializableClosure || $item instanceof UnsignedSerializableClosure || ($item instanceof SelfReference && $item->hash === $this->code['self'])) {
Expand Down
14 changes: 14 additions & 0 deletions tests/SerializerPhp81Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ enum SerializerScopedBackedEnum: string {
});
})->with('serializers');

test('readonly properties from parent scope variable', function () {
$controller = new SerializerPhp81Controller();

$f = static function () use ($controller) {
return $controller;
};

$f = s($f);

expect($f()->service)->toBeInstanceOf(
SerializerPhp81Service::class,
);
})->with('serializers');

test('first-class callable with closures', function () {
$f = function ($value) {
return $value;
Expand Down

0 comments on commit b88b3bf

Please sign in to comment.