Skip to content

[PropertyAccess] Fix handling of uninitialized property of anonymous class #45002

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
Jan 12, 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
6 changes: 3 additions & 3 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
}
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ([\w\\\]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
$r = new \ReflectionProperty($matches[1], $matches[2]);
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ('.preg_quote(get_debug_type($object), '/').')::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
Copy link
Contributor

@filiplikavcan filiplikavcan Jan 31, 2022

Choose a reason for hiding this comment

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

@nicolas-grekas

This change introduced a new bug:

class X { public int $a; }
class Y extends X {}
(new Y)->a;

This throws:

Typed property X::$a must not be accessed before initialization

but since get_debug_type(new Y) returns Y the error message does not match

Typed property Y::$a must not be accessed before initialization"

Here is a PR which fixes this: #45255

$r = new \ReflectionProperty($class, $matches[2]);
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;

throw new AccessException(sprintf('The property "%s::$%s" is not readable because it is typed "%s". You should initialize it or declare a default value instead.', $r->getDeclaringClass()->getName(), $r->getName(), $type), 0, $e);
throw new AccessException(sprintf('The property "%s::$%s" is not readable because it is typed "%s". You should initialize it or declare a default value instead.', $matches[1], $r->getName(), $type), 0, $e);
}

throw $e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,41 @@ public function getUninitialized(): array
$this->propertyAccessor->getValue($object, 'uninitialized');
}

/**
* @requires PHP 7.4
*/
public function testGetValueThrowsExceptionIfUninitializedNotNullablePropertyWithGetterOfAnonymousClass()
{
$this->expectException(AccessException::class);
$this->expectExceptionMessage('The property "class@anonymous::$uninitialized" is not readable because it is typed "string". You should initialize it or declare a default value instead.');

$object = eval('return new class() {
private string $uninitialized;

public function getUninitialized(): string
{
return $this->uninitialized;
}
};');

$this->propertyAccessor->getValue($object, 'uninitialized');
}

/**
* @requires PHP 7.4
*/
public function testGetValueThrowsExceptionIfUninitializedPropertyOfAnonymousClass()
{
$this->expectException(AccessException::class);
$this->expectExceptionMessage('The property "class@anonymous::$uninitialized" is not readable because it is typed "string". You should initialize it or declare a default value instead.');

$object = eval('return new class() {
public string $uninitialized;
};');

$this->propertyAccessor->getValue($object, 'uninitialized');
}

public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousStdClass()
{
$this->expectException(AccessException::class);
Expand Down