Skip to content

[VarExporter] Fix support for asymmetric visibility #59884

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

foreach ($properties as $name => &$value) {
[$scope, $name, $readonlyScope] = $propertyScopes[$name] ?? [$class, $name, $class];
$scopedProperties[$readonlyScope ?? $scope][$name] = &$value;
[$scope, $name, $writeScope] = $propertyScopes[$name] ?? [$class, $name, $class];
$scopedProperties[$writeScope ?? $scope][$name] = &$value;
}
unset($value);
}
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 @@ -90,7 +90,8 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
$properties = $serializeProperties;
} else {
foreach ($serializeProperties as $n => $v) {
$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';
$properties[$c][$n] = $v;
}
}
Expand Down
26 changes: 15 additions & 11 deletions src/Symfony/Component/VarExporter/Internal/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,19 @@
$name = $property->name;

if (\ReflectionProperty::IS_PRIVATE & $flags) {
$readonlyScope = null;
if ($flags & \ReflectionProperty::IS_READONLY) {
$readonlyScope = $class;
$writeScope = null;
if (\PHP_VERSION_ID >= 80400 ? $property->isPrivateSet() : ($flags & \ReflectionProperty::IS_READONLY)) {

Check failure on line 275 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:275:59: UndefinedMethod: Method ReflectionProperty::isPrivateSet does not exist (see https://psalm.dev/022)

Check failure on line 275 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:275:59: UndefinedMethod: Method ReflectionProperty::isPrivateSet does not exist (see https://psalm.dev/022)
$writeScope = $class;
}
$propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, $readonlyScope, $property];
$propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, $writeScope, $property];

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

Check failure on line 283 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:283:55: UndefinedMethod: Method ReflectionProperty::isProtectedSet does not exist (see https://psalm.dev/022)

Check failure on line 283 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:283:86: UndefinedMethod: Method ReflectionProperty::isPrivateSet does not exist (see https://psalm.dev/022)

Check failure on line 283 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:283:55: UndefinedMethod: Method ReflectionProperty::isProtectedSet does not exist (see https://psalm.dev/022)
$writeScope = $property->class;
}
$propertyScopes[$name] = [$class, $name, $readonlyScope, $property];
$propertyScopes[$name] = [$class, $name, $writeScope, $property];

if (\ReflectionProperty::IS_PROTECTED & $flags) {
$propertyScopes["\0*\0$name"] = $propertyScopes[$name];
Expand All @@ -298,9 +298,13 @@
foreach ($r->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
if (!$property->isStatic()) {
$name = $property->name;
$readonlyScope = $property->isReadOnly() ? $class : null;
$propertyScopes["\0$class\0$name"] = [$class, $name, $readonlyScope, $property];
$propertyScopes[$name] ??= [$class, $name, $readonlyScope, $property];
if (\PHP_VERSION_ID < 80400) {
$writeScope = $property->isReadOnly() ? $class : null;
} else {
$writeScope = $property->isPrivateSet() ? $class : null;

Check failure on line 304 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:304:50: UndefinedMethod: Method ReflectionProperty::isPrivateSet does not exist (see https://psalm.dev/022)
}
$propertyScopes["\0$class\0$name"] = [$class, $name, $writeScope, $property];
$propertyScopes[$name] ??= [$class, $name, $writeScope, $property];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function getClassResetters($class)
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
}

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

if ($k !== $key || "\0$class\0lazyObjectState" === $k) {
Expand All @@ -68,7 +68,7 @@ public static function getClassResetters($class)
if ($k === $name && ($propertyScopes[$k][4] ?? false)) {
$hookedProperties[$k] = true;
} else {
$classProperties[$readonlyScope ?? $scope][$name] = $key;
$classProperties[$writeScope ?? $scope][$name] = $key;
}
}

Expand Down Expand Up @@ -138,17 +138,17 @@ public static function getParentMethods($class)
return $methods;
}

public static function getScope($propertyScopes, $class, $property, $readonlyScope = null)
public static function getScope($propertyScopes, $class, $property, $writeScope = null)
{
if (null === $readonlyScope && !isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
if (null === $writeScope && !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 === $readonlyScope && '*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
if (null === $writeScope && '*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
}
$properties = (array) $instance;
foreach ($values as $key => $value) {
if (!\array_key_exists($key, $properties) && [$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
if (!\array_key_exists($key, $properties) && [$scope, $name, $writeScope] = $propertyScopes[$key] ?? null) {

Check failure on line 74 in src/Symfony/Component/VarExporter/Internal/LazyObjectState.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArrayOffset

src/Symfony/Component/VarExporter/Internal/LazyObjectState.php:74:66: InvalidArrayOffset: Cannot destructure non-array of type mixed|null (see https://psalm.dev/115)
$scope = $writeScope ?? ('*' !== $scope ? $scope : $class);
$accessor = LazyObjectRegistry::$classAccessors[$scope] ??= LazyObjectRegistry::getClassAccessors($scope);
$accessor['set']($instance, $name, $value);

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

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

if ($k === $key && (null !== $readonlyScope || !\array_key_exists($k, $properties))) {
if ($k === $key && (null !== $writeScope || !\array_key_exists($k, $properties))) {
$skippedProperties[$k] = true;
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function initializeLazyObject(): static
$properties = (array) $this;
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
foreach ($state->initializer as $key => $initializer) {
if (\array_key_exists($key, $properties) || ![$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
if (\array_key_exists($key, $properties) || ![$scope, $name, $writeScope] = $propertyScopes[$key] ?? null) {
continue;
}
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
$scope = $writeScope ?? ('*' !== $scope ? $scope : $class);

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

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

Expand All @@ -175,7 +175,7 @@ public function &__get($name): mixed
$property = null;
}

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

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

return $accessor['get']($this, $name, null !== $readonlyScope);
return $accessor['get']($this, $name, null !== $writeScope);
} 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 +223,7 @@ public function &__get($name): mixed

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

return $accessor['get']($this, $name, null !== $readonlyScope);
return $accessor['get']($this, $name, null !== $writeScope);
} 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,15 +239,15 @@ public function __set($name, $value): void
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;

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

if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
if ($state && ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
&& LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
) {
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
$state->initialize($this, $name, $readonlyScope ?? $scope);
$state->initialize($this, $name, $writeScope ?? $scope);
}
goto set_in_scope;
}
Expand All @@ -274,13 +274,13 @@ public function __isset($name): bool
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;

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

if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
&& LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $writeScope ?? $scope)
) {
goto isset_in_scope;
}
Expand All @@ -305,15 +305,15 @@ public function __unset($name): void
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;

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

if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
if ($state && ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
&& LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
) {
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
$state->initialize($this, $name, $readonlyScope ?? $scope);
$state->initialize($this, $name, $writeScope ?? $scope);
}
goto unset_in_scope;
}
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Component/VarExporter/LazyProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
$scope = null;
$instance = $this;

if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {

Check failure on line 92 in src/Symfony/Component/VarExporter/LazyProxyTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArrayOffset

src/Symfony/Component/VarExporter/LazyProxyTrait.php:92:13: InvalidArrayOffset: Cannot destructure non-array of type mixed|null (see https://psalm.dev/115)
$scope = Registry::getScope($propertyScopes, $class, $name);

if (null === $scope || isset($propertyScopes["\0$scope\0$name"])) {
Expand Down Expand Up @@ -122,7 +122,7 @@

try {
if (null === $scope) {
if (null === $readonlyScope && 1 !== $parent) {
if (null === $writeScope && 1 !== $parent) {
return $instance->$name;
}
$value = $instance->$name;
Expand All @@ -131,7 +131,7 @@
}
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);

return $accessor['get']($instance, $name, null !== $readonlyScope || 1 === $parent);
return $accessor['get']($instance, $name, null !== $writeScope || 1 === $parent);
} catch (\Error $e) {
if (\Error::class !== $e::class || !str_starts_with($e->getMessage(), 'Cannot access uninitialized non-nullable property')) {
throw $e;
Expand All @@ -146,7 +146,7 @@

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

return $accessor['get']($instance, $name, null !== $readonlyScope || 1 === $parent);
return $accessor['get']($instance, $name, null !== $writeScope || 1 === $parent);
} catch (\Error) {
throw $e;
}
Expand All @@ -159,10 +159,10 @@
$scope = null;
$instance = $this;

if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {

Check failure on line 162 in src/Symfony/Component/VarExporter/LazyProxyTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArrayOffset

src/Symfony/Component/VarExporter/LazyProxyTrait.php:162:13: InvalidArrayOffset: Cannot destructure non-array of type mixed|null (see https://psalm.dev/115)
$scope = Registry::getScope($propertyScopes, $class, $name, $writeScope);

if ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"])) {
if ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"])) {
if ($state = $this->lazyObjectState ?? null) {
$instance = $state->realInstance ??= ($state->initializer)();
}
Expand Down Expand Up @@ -227,10 +227,10 @@
$scope = null;
$instance = $this;

if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {

Check failure on line 230 in src/Symfony/Component/VarExporter/LazyProxyTrait.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArrayOffset

src/Symfony/Component/VarExporter/LazyProxyTrait.php:230:13: InvalidArrayOffset: Cannot destructure non-array of type mixed|null (see https://psalm.dev/115)
$scope = Registry::getScope($propertyScopes, $class, $name, $writeScope);

if ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"])) {
if ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"])) {
if ($state = $this->lazyObjectState ?? null) {
$instance = $state->realInstance ??= ($state->initializer)();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy;

class AsymmetricVisibility
{
public private(set) int $foo;

public function __construct(int $foo)
{
$this->foo = $foo;
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AsymmetricVisibility;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked;
use Symfony\Component\VarExporter\Tests\Fixtures\SimpleObject;

Expand Down Expand Up @@ -504,6 +505,21 @@ public function testPropertyHooks()
$this->assertSame(345, $object->backed);
}

/**
* @requires PHP 8.4
*/
public function testAsymmetricVisibility()
{
$initialized = false;
$object = $this->createLazyGhost(AsymmetricVisibility::class, function ($instance) use (&$initialized) {
$initialized = true;

$instance->__construct(123);
});

$this->assertSame(123, $object->foo);
}

/**
* @template T
*
Expand Down
17 changes: 16 additions & 1 deletion src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\LazyProxyTrait;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\RegularClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AbstractHooked;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\AsymmetricVisibility;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\Hooked;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\FinalPublicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass;
Expand Down Expand Up @@ -364,6 +364,21 @@ public function testAbstractPropertyHooks()
$this->assertTrue($initialized);
}

/**
* @requires PHP 8.4
*/
public function testAsymmetricVisibility()
{
$initialized = false;
$object = $this->createLazyProxy(AsymmetricVisibility::class, function () use (&$initialized) {
$initialized = true;

return new AsymmetricVisibility(123);
});

$this->assertSame(123, $object->foo);
}

/**
* @template T
*
Expand Down
Loading