Skip to content

[VarExporter] Fix accessing readonly properties by reference #46852

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

Merged
merged 1 commit into from
Jul 5, 2022
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
8 changes: 4 additions & 4 deletions src/Symfony/Component/VarExporter/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public static function hydrate(object $instance, array $properties = [], array $
$propertyScopes = InternalHydrator::$propertyScopes[$class] ??= InternalHydrator::getPropertyScopes($class);

foreach ($properties as $name => &$value) {
[$scope, $name] = $propertyScopes[$name] ?? [$class, $name];
$scopedProperties[$scope][$name] = &$value;
[$scope, $name, $readonlyScope] = $propertyScopes[$name] ?? [$class, $name, $class];
$scopedProperties[$readonlyScope ?? $scope][$name] = &$value;
}
unset($value);
}

foreach ($scopedProperties as $class => $properties) {
foreach ($scopedProperties as $scope => $properties) {
if ($properties) {
(InternalHydrator::$simpleHydrators[$class] ??= InternalHydrator::getSimpleHydrator($class))($properties, $instance);
(InternalHydrator::$simpleHydrators[$scope] ??= InternalHydrator::getSimpleHydrator($scope))($properties, $instance);
}
}

Expand Down
36 changes: 27 additions & 9 deletions src/Symfony/Component/VarExporter/Internal/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,35 @@ public static function getHydrator($class)

public static function getSimpleHydrator($class)
{
$baseHydrator = self::$simpleHydrators['stdClass'] ??= static function ($properties, $object) {
$baseHydrator = self::$simpleHydrators['stdClass'] ??= (function ($properties, $object) {
$readonly = (array) $this;

foreach ($properties as $name => &$value) {
$object->$name = &$value;
$object->$name = $value;

if (!($readonly[$name] ?? false)) {
$object->$name = &$value;
}
}
};
})->bindTo(new \stdClass());

switch ($class) {
case 'stdClass':
return $baseHydrator;

case 'ErrorException':
return $baseHydrator->bindTo(null, new class() extends \ErrorException {
return $baseHydrator->bindTo(new \stdClass(), new class() extends \ErrorException {
});

case 'TypeError':
return $baseHydrator->bindTo(null, new class() extends \Error {
return $baseHydrator->bindTo(new \stdClass(), new class() extends \Error {
});

case 'SplObjectStorage':
return static function ($properties, $object) {
foreach ($properties as $name => &$value) {
if ("\0" !== $name) {
$object->$name = $value;
$object->$name = &$value;
continue;
}
Expand All @@ -202,14 +209,22 @@ public static function getSimpleHydrator($class)
if ("\0" === $name) {
$constructor($object, $value);
} else {
$object->$name = $value;
$object->$name = &$value;
}
}
};
}

if (!$classReflector->isInternal()) {
return $baseHydrator->bindTo(null, $class);
$readonly = new \stdClass();
foreach ($classReflector->getProperties(\ReflectionProperty::IS_READONLY) as $propertyReflector) {
if ($class === $propertyReflector->class) {
$readonly->{$propertyReflector->name} = true;
}
}

return $baseHydrator->bindTo($readonly, $class);
}

if ($classReflector->name !== $class) {
Expand All @@ -232,6 +247,7 @@ public static function getSimpleHydrator($class)
if ($setValue = $propertySetters[$name] ?? null) {
$setValue($object, $value);
} else {
$object->$name = $value;
$object->$name = &$value;
}
}
Expand All @@ -252,10 +268,10 @@ public static function getPropertyScopes($class)
$name = $property->name;

if (\ReflectionProperty::IS_PRIVATE & $flags) {
$propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name];
$propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, $flags & \ReflectionProperty::IS_READONLY ? $class : null];
continue;
}
$propertyScopes[$name] = [$flags & \ReflectionProperty::IS_READONLY ? $property->class : $class, $name];
$propertyScopes[$name] = [$class, $name, $flags & \ReflectionProperty::IS_READONLY ? $property->class : null];

if (\ReflectionProperty::IS_PROTECTED & $flags) {
$propertyScopes["\0*\0$name"] = $propertyScopes[$name];
Expand All @@ -268,7 +284,9 @@ public static function getPropertyScopes($class)
foreach ($r->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
if (!$property->isStatic()) {
$name = $property->name;
$propertyScopes["\0$class\0$name"] = [$class, $name];
$readonlyScope = $property->isReadOnly() ? $class : null;
$propertyScopes["\0$class\0$name"] = [$class, $name, $readonlyScope];
$propertyScopes[$name] ??= [$class, $name, $readonlyScope];
}
}
}
Expand Down