Skip to content

[VarExporter] Fix support for hooks and asymmetric visibility #59965

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
Mar 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class WitherProxy580fe0f extends \Symfony\Component\DependencyInjection\Tests\Co
use \Symfony\Component\VarExporter\LazyProxyTrait;

private const LAZY_OBJECT_PROPERTY_SCOPES = [
'foo' => [parent::class, 'foo', null],
'foo' => [parent::class, 'foo', null, 4],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class WitherProxyDd381be extends \Symfony\Component\DependencyInjection\Tests\Co
use \Symfony\Component\VarExporter\LazyProxyTrait;

private const LAZY_OBJECT_PROPERTY_SCOPES = [
'foo' => [parent::class, 'foo', null],
'foo' => [parent::class, 'foo', null, 4],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"psr/container": "^1.1|^2.0",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/service-contracts": "^2.5|^3.0",
"symfony/var-exporter": "^6.2.10|^7.0"
"symfony/var-exporter": "^6.4.20|^7.2.5"
},
"require-dev": {
"symfony/yaml": "^5.4|^6.0|^7.0",
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/VarExporter/Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
$i = 0;
$n = (string) $name;
if ('' === $n || "\0" !== $n[0]) {
$c = $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
$p = $reflector->hasProperty($n) ? $reflector->getProperty($n) : null;
$c = $p && (\PHP_VERSION_ID >= 80400 ? $p->isProtectedSet() || $p->isPrivateSet() : $p->isReadOnly()) ? $p->class : 'stdClass';
} elseif ('*' === $n[1]) {
$n = substr($n, 3);
$c = $reflector->getProperty($n)->class;
Expand Down
73 changes: 44 additions & 29 deletions src/Symfony/Component/VarExporter/Internal/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
class Hydrator
{
public const PROPERTY_HAS_HOOKS = 1;
public const PROPERTY_NOT_BY_REF = 2;

public static array $hydrators = [];
public static array $simpleHydrators = [];
public static array $propertyScopes = [];
Expand Down Expand Up @@ -156,13 +159,16 @@
public static function getSimpleHydrator($class)
{
$baseHydrator = self::$simpleHydrators['stdClass'] ??= (function ($properties, $object) {
$readonly = (array) $this;
$notByRef = (array) $this;

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

if (!($readonly[$name] ?? false)) {
if (!$noRef = $notByRef[$name] ?? false) {
$object->$name = $value;
$object->$name = &$value;
} elseif (true !== $noRef) {
$notByRef($object, $value);
} else {
$object->$name = $value;
}
}
})->bindTo(new \stdClass());
Expand Down Expand Up @@ -217,14 +223,19 @@
}

if (!$classReflector->isInternal()) {
$readonly = new \stdClass();
foreach ($classReflector->getProperties(\ReflectionProperty::IS_READONLY) as $propertyReflector) {
if ($class === $propertyReflector->class) {
$readonly->{$propertyReflector->name} = true;
$notByRef = new \stdClass();
foreach ($classReflector->getProperties() as $propertyReflector) {
if ($propertyReflector->isStatic()) {
continue;
}
if (\PHP_VERSION_ID >= 80400 && !$propertyReflector->isAbstract() && $propertyReflector->getHooks()) {

Check failure on line 231 in src/Symfony/Component/VarExporter/Internal/Hydrator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarExporter/Internal/Hydrator.php:231:70: UndefinedMethod: Method ReflectionProperty::isAbstract does not exist (see https://psalm.dev/022)
$notByRef->{$propertyReflector->name} = $propertyReflector->setRawValue(...);

Check failure on line 232 in src/Symfony/Component/VarExporter/Internal/Hydrator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarExporter/Internal/Hydrator.php:232:81: UndefinedMethod: Method ReflectionProperty::setrawvalue does not exist (see https://psalm.dev/022)
} elseif ($propertyReflector->isReadOnly()) {
$notByRef->{$propertyReflector->name} = true;
}
}

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

if ($classReflector->name !== $class) {
Expand Down Expand Up @@ -269,43 +280,47 @@
continue;
}
$name = $property->name;
$access = ($flags << 2) | ($flags & \ReflectionProperty::IS_READONLY ? self::PROPERTY_NOT_BY_REF : 0);

if (\PHP_VERSION_ID >= 80400 && !$property->isAbstract() && $h = $property->getHooks()) {

Check failure on line 285 in src/Symfony/Component/VarExporter/Internal/Hydrator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarExporter/Internal/Hydrator.php:285:57: UndefinedMethod: Method ReflectionProperty::isAbstract does not exist (see https://psalm.dev/022)

Check failure on line 285 in src/Symfony/Component/VarExporter/Internal/Hydrator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarExporter/Internal/Hydrator.php:285:89: UndefinedMethod: Method ReflectionProperty::getHooks does not exist (see https://psalm.dev/022)
$access |= self::PROPERTY_HAS_HOOKS | (isset($h['get']) && !$h['get']->returnsReference() ? self::PROPERTY_NOT_BY_REF : 0);
}

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

continue;
}
$writeScope = null;
if (\PHP_VERSION_ID >= 80400 ? $property->isProtectedSet() || $property->isPrivateSet() : ($flags & \ReflectionProperty::IS_READONLY)) {
$writeScope = $property->class;

$propertyScopes[$name] = [$class, $name, null, $access, $property];

if ($flags & (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY)) {

Check failure on line 297 in src/Symfony/Component/VarExporter/Internal/Hydrator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedConstant

src/Symfony/Component/VarExporter/Internal/Hydrator.php:297:54: UndefinedConstant: Constant ReflectionProperty::IS_PRIVATE_SET is not defined (see https://psalm.dev/020)
$propertyScopes[$name][2] = $property->class;
}
$propertyScopes[$name] = [$class, $name, $writeScope, $property];

if (\ReflectionProperty::IS_PROTECTED & $flags) {
$propertyScopes["\0*\0$name"] = $propertyScopes[$name];
} elseif (\PHP_VERSION_ID >= 80400 && $property->getHooks()) {
$propertyScopes[$name][4] = true;
}
}

while ($r = $r->getParentClass()) {
$class = $r->name;

foreach ($r->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
if (!$property->isStatic()) {
$name = $property->name;
if (\PHP_VERSION_ID < 80400) {
$writeScope = $property->isReadOnly() ? $class : null;
} else {
$writeScope = $property->isPrivateSet() ? $class : null;
}
$propertyScopes["\0$class\0$name"] = [$class, $name, $writeScope, $property];
$propertyScopes[$name] ??= [$class, $name, $writeScope, $property];
$flags = $property->getModifiers();

if (\ReflectionProperty::IS_STATIC & $flags) {
continue;
}
$name = $property->name;
$access = ($flags << 2) | ($flags & \ReflectionProperty::IS_READONLY ? self::PROPERTY_NOT_BY_REF : 0);

if (\PHP_VERSION_ID >= 80400 && $h = $property->getHooks()) {

Check failure on line 318 in src/Symfony/Component/VarExporter/Internal/Hydrator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarExporter/Internal/Hydrator.php:318:65: UndefinedMethod: Method ReflectionProperty::getHooks does not exist (see https://psalm.dev/022)
$access |= self::PROPERTY_HAS_HOOKS | (isset($h['get']) && !$h['get']->returnsReference() ? self::PROPERTY_NOT_BY_REF : 0);
}

$propertyScopes["\0$class\0$name"] = [$class, $name, null, $access, $property];
$propertyScopes[$name] ??= $propertyScopes["\0$class\0$name"];
}
}

Expand Down
34 changes: 27 additions & 7 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
}

foreach ($propertyScopes as $key => [$scope, $name, $writeScope]) {
foreach ($propertyScopes as $key => [$scope, $name, $writeScope, $access]) {
$propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;

if ($k !== $key || "\0$class\0lazyObjectState" === $k) {
continue;
}

if ($k === $name && ($propertyScopes[$k][4] ?? false)) {
if ($access & Hydrator::PROPERTY_HAS_HOOKS) {
$hookedProperties[$k] = true;
} else {
$classProperties[$writeScope ?? $scope][$name] = $key;
Expand Down Expand Up @@ -101,8 +101,8 @@
public static function getClassAccessors($class)
{
return \Closure::bind(static fn () => [
'get' => static function &($instance, $name, $readonly) {
if (!$readonly) {
'get' => static function &($instance, $name, $notByRef) {
if (!$notByRef) {
return $instance->$name;
}
$value = $instance->$name;
Expand Down Expand Up @@ -138,17 +138,37 @@
return $methods;
}

public static function getScope($propertyScopes, $class, $property, $writeScope = null)
public static function getScopeForRead($propertyScopes, $class, $property)
{
if (null === $writeScope && !isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
if (!isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
return null;
}
$frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];

if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
$scope = $frame['object']->class;
}
if (null === $writeScope && '*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
if ('*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
return null;
}

return $scope;
}

public static function getScopeForWrite($propertyScopes, $class, $property, $flags)
{
if (!($flags & (\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_READONLY | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET | \ReflectionProperty::IS_PROTECTED_SET : 0)))) {

Check failure on line 160 in src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedConstant

src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php:160:158: UndefinedConstant: Constant ReflectionProperty::IS_PRIVATE_SET is not defined (see https://psalm.dev/020)

Check failure on line 160 in src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedConstant

src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php:160:196: UndefinedConstant: Constant ReflectionProperty::IS_PROTECTED_SET is not defined (see https://psalm.dev/020)
return null;
}
$frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];

if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
$scope = $frame['object']->class;
}
if ($flags & (\ReflectionProperty::IS_PRIVATE | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY))) {

Check failure on line 168 in src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedConstant

src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php:168:85: UndefinedConstant: Constant ReflectionProperty::IS_PRIVATE_SET is not defined (see https://psalm.dev/020)
return $scope;
}
if ($flags & (\ReflectionProperty::IS_PROTECTED | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PROTECTED_SET : 0)) && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {

Check failure on line 171 in src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedConstant

src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php:171:87: UndefinedConstant: Constant ReflectionProperty::IS_PROTECTED_SET is not defined (see https://psalm.dev/020)
return null;
}

Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectState.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ public function __construct(public readonly \Closure|array $initializer, $skippe
$this->status = \is_array($initializer) ? self::STATUS_UNINITIALIZED_PARTIAL : self::STATUS_UNINITIALIZED_FULL;
}

public function initialize($instance, $propertyName, $propertyScope)
public function initialize($instance, $propertyName, $writeScope)
{
if (self::STATUS_INITIALIZED_FULL === $this->status) {
return self::STATUS_INITIALIZED_FULL;
}

if (\is_array($this->initializer)) {
$class = $instance::class;
$propertyScope ??= $class;
$writeScope ??= $class;
$propertyScopes = Hydrator::$propertyScopes[$class];
$propertyScopes[$k = "\0$propertyScope\0$propertyName"] ?? $propertyScopes[$k = "\0*\0$propertyName"] ?? $k = $propertyName;
$propertyScopes[$k = "\0$writeScope\0$propertyName"] ?? $propertyScopes[$k = "\0*\0$propertyName"] ?? $k = $propertyName;

if ($initializer = $this->initializer[$k] ?? null) {
$value = $initializer(...[$instance, $propertyName, $propertyScope, LazyObjectRegistry::$defaultProperties[$class][$k] ?? null]);
$accessor = LazyObjectRegistry::$classAccessors[$propertyScope] ??= LazyObjectRegistry::getClassAccessors($propertyScope);
$value = $initializer(...[$instance, $propertyName, $writeScope, LazyObjectRegistry::$defaultProperties[$class][$k] ?? null]);
$accessor = LazyObjectRegistry::$classAccessors[$writeScope] ??= LazyObjectRegistry::getClassAccessors($writeScope);
$accessor['set']($instance, $propertyName, $value);

return $this->status = self::STATUS_INITIALIZED_PARTIAL;
Expand All @@ -72,7 +72,7 @@ public function initialize($instance, $propertyName, $propertyScope)
$properties = (array) $instance;
foreach ($values as $key => $value) {
if (!\array_key_exists($key, $properties) && [$scope, $name, $writeScope] = $propertyScopes[$key] ?? null) {
$scope = $writeScope ?? ('*' !== $scope ? $scope : $class);
$scope = $writeScope ?? $scope;
$accessor = LazyObjectRegistry::$classAccessors[$scope] ??= LazyObjectRegistry::getClassAccessors($scope);
$accessor['set']($instance, $name, $value);

Expand Down Expand Up @@ -116,10 +116,10 @@ public function reset($instance): void
$properties = (array) $instance;
$onlyProperties = \is_array($this->initializer) ? $this->initializer : null;

foreach ($propertyScopes as $key => [$scope, $name, $writeScope]) {
foreach ($propertyScopes as $key => [$scope, $name, , $access]) {
$propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;

if ($k === $key && (null !== $writeScope || !\array_key_exists($k, $properties))) {
if ($k === $key && ($access & Hydrator::PROPERTY_HAS_HOOKS || ($access >> 2) & \ReflectionProperty::IS_READONLY || !\array_key_exists($k, $properties))) {
$skippedProperties[$k] = true;
}
}
Expand Down
32 changes: 19 additions & 13 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function initializeLazyObject(): static
if (\array_key_exists($key, $properties) || ![$scope, $name, $writeScope] = $propertyScopes[$key] ?? null) {
continue;
}
$scope = $writeScope ?? ('*' !== $scope ? $scope : $class);
$scope = $writeScope ?? $scope;

if (null === $values) {
if (!\is_array($values = ($state->initializer["\0"])($this, Registry::$defaultProperties[$class]))) {
Expand Down Expand Up @@ -160,20 +160,26 @@ public function &__get($name): mixed
{
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;
$notByRef = 0;

if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name);
if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScopeForRead($propertyScopes, $class, $name);
$state = $this->lazyObjectState ?? null;

if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
$notByRef = $access & Hydrator::PROPERTY_NOT_BY_REF;

if (LazyObjectState::STATUS_INITIALIZED_FULL === $state->status) {
// Work around php/php-src#12695
$property = null === $scope ? $name : "\0$scope\0$name";
$property = $propertyScopes[$property][3]
?? Hydrator::$propertyScopes[$this::class][$property][3] = new \ReflectionProperty($scope ?? $class, $name);
$property = $propertyScopes[$property][4]
?? Hydrator::$propertyScopes[$this::class][$property][4] = new \ReflectionProperty($scope ?? $class, $name);
} else {
$property = null;
}
if (\PHP_VERSION_ID >= 80400 && !$notByRef && ($access >> 2) & \ReflectionProperty::IS_PRIVATE_SET) {
$scope ??= $writeScope;
}

if ($property?->isInitialized($this) ?? LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $writeScope ?? $scope)) {
goto get_in_scope;
Expand All @@ -199,7 +205,7 @@ public function &__get($name): mixed

try {
if (null === $scope) {
if (null === $writeScope) {
if (!$notByRef) {
return $this->$name;
}
$value = $this->$name;
Expand All @@ -208,7 +214,7 @@ public function &__get($name): mixed
}
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);

return $accessor['get']($this, $name, null !== $writeScope);
return $accessor['get']($this, $name, $notByRef);
} catch (\Error $e) {
if (\Error::class !== $e::class || !str_starts_with($e->getMessage(), 'Cannot access uninitialized non-nullable property')) {
throw $e;
Expand All @@ -223,7 +229,7 @@ public function &__get($name): mixed

$accessor['set']($this, $name, []);

return $accessor['get']($this, $name, null !== $writeScope);
return $accessor['get']($this, $name, $notByRef);
} catch (\Error) {
if (preg_match('/^Cannot access uninitialized non-nullable property ([^ ]++) by reference$/', $e->getMessage(), $matches)) {
throw new \Error('Typed property '.$matches[1].' must not be accessed before initialization', $e->getCode(), $e->getPrevious());
Expand All @@ -239,8 +245,8 @@ public function __set($name, $value): void
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;

if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name, $writeScope);
if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScopeForWrite($propertyScopes, $class, $name, $access >> 2);
$state = $this->lazyObjectState ?? null;

if ($state && ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
Expand Down Expand Up @@ -275,7 +281,7 @@ public function __isset($name): bool
$scope = null;

if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name);
$scope = Registry::getScopeForRead($propertyScopes, $class, $name);
$state = $this->lazyObjectState ?? null;

if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
Expand Down Expand Up @@ -305,8 +311,8 @@ public function __unset($name): void
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;

if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name, $writeScope);
if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScopeForWrite($propertyScopes, $class, $name, $access >> 2);
$state = $this->lazyObjectState ?? null;

if ($state && ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
Expand Down
Loading
Loading