Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8a9b841

Browse filesBrowse files
[VarExporter] Fix exporting objects with readonly properties
1 parent 780efff commit 8a9b841
Copy full SHA for 8a9b841

File tree

Expand file treeCollapse file tree

4 files changed

+48
-3
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+48
-3
lines changed

‎src/Symfony/Component/VarExporter/Internal/Exporter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/Internal/Exporter.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
140140
$i = 0;
141141
$n = (string) $name;
142142
if ('' === $n || "\0" !== $n[0]) {
143-
$c = 'stdClass';
143+
$c = \PHP_VERSION_ID >= 80100 && $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
144144
} elseif ('*' === $n[1]) {
145145
$n = substr($n, 3);
146146
$c = $reflector->getProperty($n)->class;
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarExporter\Tests\Fixtures;
13+
14+
class FooReadonly
15+
{
16+
public function __construct(
17+
public readonly string $name,
18+
public readonly string $value,
19+
) {
20+
}
21+
}
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
4+
$o = [
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\Fixtures\\FooReadonly'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\Fixtures\\FooReadonly')),
6+
],
7+
null,
8+
[
9+
'Symfony\\Component\\VarExporter\\Tests\\Fixtures\\FooReadonly' => [
10+
'name' => [
11+
'k',
12+
],
13+
'value' => [
14+
'v',
15+
],
16+
],
17+
],
18+
$o[0],
19+
[]
20+
);

‎src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
1717
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
1818
use Symfony\Component\VarExporter\Internal\Registry;
19+
use Symfony\Component\VarExporter\Tests\Fixtures\FooReadonly;
1920
use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable;
2021
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
2122
use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable;
@@ -244,9 +245,12 @@ public function provideExport()
244245

245246
yield ['php74-serializable', new Php74Serializable()];
246247

247-
if (\PHP_VERSION_ID >= 80100) {
248-
yield ['unit-enum', [FooUnitEnum::Bar], true];
248+
if (\PHP_VERSION_ID < 80100) {
249+
return;
249250
}
251+
252+
yield ['unit-enum', [FooUnitEnum::Bar], true];
253+
yield ['readonly', new FooReadonly('k', 'v')];
250254
}
251255

252256
public function testUnicodeDirectionality()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.