Skip to content
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

[VarExporter] Work around php/php-src#12695 for lazy objects, fixing nullsafe-related behavior #52762

Merged
merged 1 commit into from
Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Symfony/Component/VarExporter/Internal/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ public static function getPropertyScopes($class)
$name = $property->name;

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

if (\ReflectionProperty::IS_PROTECTED & $flags) {
$propertyScopes["\0*\0$name"] = $propertyScopes[$name];
Expand All @@ -288,8 +288,8 @@ public static function getPropertyScopes($class)
if (!$property->isStatic()) {
$name = $property->name;
$readonlyScope = $property->isReadOnly() ? $class : null;
$propertyScopes["\0$class\0$name"] = [$class, $name, $readonlyScope];
$propertyScopes[$name] ??= [$class, $name, $readonlyScope];
$propertyScopes["\0$class\0$name"] = [$class, $name, $readonlyScope, $property];
$propertyScopes[$name] ??= [$class, $name, $readonlyScope, $property];
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectState.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,32 @@ public function initialize($instance, $propertyName, $propertyScope)
return $this->status = self::STATUS_INITIALIZED_PARTIAL;
}

$status = self::STATUS_UNINITIALIZED_PARTIAL;

if ($initializer = $this->initializer["\0"] ?? null) {
if (!\is_array($values = $initializer($instance, LazyObjectRegistry::$defaultProperties[$class]))) {
throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
}
$properties = (array) $instance;
foreach ($values as $key => $value) {
if ($k === $key) {
$status = self::STATUS_INITIALIZED_PARTIAL;
}
if (!\array_key_exists($key, $properties) && [$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
$accessor = LazyObjectRegistry::$classAccessors[$scope] ??= LazyObjectRegistry::getClassAccessors($scope);
$accessor['set']($instance, $name, $value);

if ($k === $key) {
$this->status = self::STATUS_INITIALIZED_PARTIAL;
}
}
}
}

return $status;
return $this->status;
}

if (self::STATUS_INITIALIZED_PARTIAL === $this->status) {
return self::STATUS_INITIALIZED_PARTIAL;
}

$this->status = self::STATUS_INITIALIZED_FULL;
$this->status = self::STATUS_INITIALIZED_PARTIAL;

try {
if ($defaultProperties = array_diff_key(LazyObjectRegistry::$defaultProperties[$instance::class], $this->skippedProperties)) {
Expand All @@ -102,7 +105,7 @@ public function initialize($instance, $propertyName, $propertyScope)
throw $e;
}

return self::STATUS_INITIALIZED_FULL;
return $this->status = self::STATUS_INITIALIZED_FULL;
}

public function reset($instance): void
Expand Down
25 changes: 19 additions & 6 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,18 @@ public function &__get($name): mixed
$scope = Registry::getScope($propertyScopes, $class, $name);
$state = $this->lazyObjectState ?? null;

if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
) {
goto get_in_scope;
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
if (LazyObjectState::STATUS_INITIALIZED_FULL === $state->status) {
// Work around php/php-src#12695
$property = $propertyScopes[null === $scope ? $name : "\0$scope\0$name"][3]
?? (Hydrator::$propertyScopes[$this::class] = Hydrator::getPropertyScopes($this::class))[3];
} else {
$property = null;
}

if ($property?->isInitialized($this) ?? LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)) {
goto get_in_scope;
}
}
}

Expand Down Expand Up @@ -237,7 +245,9 @@ public function __set($name, $value): void
$scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
$state = $this->lazyObjectState ?? null;

if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
if ($state && ($readonlyScope === $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);
}
Expand Down Expand Up @@ -271,6 +281,7 @@ public function __isset($name): bool
$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)
) {
goto isset_in_scope;
Expand Down Expand Up @@ -300,7 +311,9 @@ public function __unset($name): void
$scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
$state = $this->lazyObjectState ?? null;

if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
if ($state && ($readonlyScope === $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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/VarExporter/ProxyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ private static function exportPropertyScopes(string $parent): string
{
$propertyScopes = Hydrator::$propertyScopes[$parent] ??= Hydrator::getPropertyScopes($parent);
uksort($propertyScopes, 'strnatcmp');
foreach ($propertyScopes as $k => $v) {
unset($propertyScopes[$k][3]);
}
$propertyScopes = VarExporter::export($propertyScopes);
$propertyScopes = str_replace(VarExporter::export($parent), 'parent::class', $propertyScopes);
$propertyScopes = preg_replace("/(?|(,)\n( ) |\n |,\n (\]))/", '$1$2', $propertyScopes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ public function testUnsetPublic()

$this->assertSame(["\0".TestClass::class."\0lazyObjectState"], array_keys((array) $instance));
unset($instance->public);
$this->assertFalse(isset($instance->public));
$this->assertSame(4, $instance->publicReadonly);
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('__isset(public)');
isset($instance->public);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case was asserting a behavior that relied on a work around with buggy side effects.
It's now asserting the correct behavior.
Fixing that incorrect behavior is how I spotted the root issue: other existing test cases that use the nullsafe operator were failing.
This means the work around for php/php-src#12695 implemented here is already covered by tests.

}

public function testSetPublic()
Expand Down