Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e55df72

Browse filesBrowse files
filiplikavcannicolas-grekas
authored andcommitted
[PropertyAccess] Fix handling of uninitialized property of anonymous class
1 parent 4d04a2d commit e55df72
Copy full SHA for e55df72

File tree

2 files changed

+38
-3
lines changed
Filter options

2 files changed

+38
-3
lines changed

‎src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyAccess/PropertyAccessor.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
436436
}
437437
} catch (\Error $e) {
438438
// handle uninitialized properties in PHP >= 7.4
439-
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ([\w\\\]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
440-
$r = new \ReflectionProperty($matches[1], $matches[2]);
439+
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ('.preg_quote(\PHP_VERSION_ID < 80000 ? $class : get_debug_type($object), '/').')::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
440+
$r = new \ReflectionProperty($class, $matches[2]);
441441
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
442442

443-
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);
443+
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);
444444
}
445445

446446
throw $e;

‎src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,41 @@ public function getUninitialized(): array
176176
$this->propertyAccessor->getValue($object, 'uninitialized');
177177
}
178178

179+
/**
180+
* @requires PHP 7.4
181+
*/
182+
public function testGetValueThrowsExceptionIfUninitializedNotNullablePropertyWithGetterOfAnonymousClass()
183+
{
184+
$this->expectException(AccessException::class);
185+
$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.');
186+
187+
$object = eval('return new class() {
188+
private string $uninitialized;
189+
190+
public function getUninitialized(): string
191+
{
192+
return $this->uninitialized;
193+
}
194+
};');
195+
196+
$this->propertyAccessor->getValue($object, 'uninitialized');
197+
}
198+
199+
/**
200+
* @requires PHP 7.4
201+
*/
202+
public function testGetValueThrowsExceptionIfUninitializedPropertyOfAnonymousClass()
203+
{
204+
$this->expectException(AccessException::class);
205+
$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.');
206+
207+
$object = eval('return new class() {
208+
public string $uninitialized;
209+
};');
210+
211+
$this->propertyAccessor->getValue($object, 'uninitialized');
212+
}
213+
179214
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousStdClass()
180215
{
181216
$this->expectException(AccessException::class);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.