Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3272353

Browse files
Merge branch '4.4' into 5.3
* 4.4: [HttpClient] Remove deprecated usage of `GuzzleHttp\Promise\queue` [PropertyAccess] Fix handling of uninitialized property of anonymous class [DependencyInjection] fix test ResolveBindingsPass remove loading of class iterable [FrameworkBundle] Avoid calling rtrim(null, '/') in AssetsInstallCommand [DependencyInjection] Fix nested env var with resolve processor Allow OutputFormatter::escape() to be used for escaping URLs used in <href> allow a zero time-limit [DependencyInjection] Ignore argument type check in CheckTypeDeclarationsPass if it's a Definition with a factory [Validators] Add translations for Slovak #43735
2 parents e9e54b9 + 28466f1 commit 3272353

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

PropertyAccessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,11 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
482482
}
483483
} catch (\Error $e) {
484484
// handle uninitialized properties in PHP >= 7.4
485-
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ([\w\\\]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
486-
$r = new \ReflectionProperty($matches[1], $matches[2]);
485+
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)) {
486+
$r = new \ReflectionProperty($class, $matches[2]);
487487
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
488488

489-
throw new UninitializedPropertyException(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);
489+
throw new UninitializedPropertyException(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);
490490
}
491491

492492
throw $e;

Tests/PropertyAccessorTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1616
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17-
use Symfony\Component\PropertyAccess\Exception\AccessException;
1817
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
1918
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
2019
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
@@ -191,7 +190,7 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetter()
191190

192191
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousClass()
193192
{
194-
$this->expectException(AccessException::class);
193+
$this->expectException(UninitializedPropertyException::class);
195194
$this->expectExceptionMessage('The method "class@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
196195

197196
$object = eval('return new class() {
@@ -206,9 +205,44 @@ public function getUninitialized(): array
206205
$this->propertyAccessor->getValue($object, 'uninitialized');
207206
}
208207

208+
/**
209+
* @requires PHP 7.4
210+
*/
211+
public function testGetValueThrowsExceptionIfUninitializedNotNullablePropertyWithGetterOfAnonymousClass()
212+
{
213+
$this->expectException(UninitializedPropertyException::class);
214+
$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.');
215+
216+
$object = eval('return new class() {
217+
private string $uninitialized;
218+
219+
public function getUninitialized(): string
220+
{
221+
return $this->uninitialized;
222+
}
223+
};');
224+
225+
$this->propertyAccessor->getValue($object, 'uninitialized');
226+
}
227+
228+
/**
229+
* @requires PHP 7.4
230+
*/
231+
public function testGetValueThrowsExceptionIfUninitializedPropertyOfAnonymousClass()
232+
{
233+
$this->expectException(UninitializedPropertyException::class);
234+
$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.');
235+
236+
$object = eval('return new class() {
237+
public string $uninitialized;
238+
};');
239+
240+
$this->propertyAccessor->getValue($object, 'uninitialized');
241+
}
242+
209243
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousStdClass()
210244
{
211-
$this->expectException(AccessException::class);
245+
$this->expectException(UninitializedPropertyException::class);
212246
$this->expectExceptionMessage('The method "stdClass@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
213247

214248
$object = eval('return new class() extends \stdClass {
@@ -225,7 +259,7 @@ public function getUninitialized(): array
225259

226260
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousChildClass()
227261
{
228-
$this->expectException(AccessException::class);
262+
$this->expectException(UninitializedPropertyException::class);
229263
$this->expectExceptionMessage('The method "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty@anonymous::getUninitialized()" returned "null", but expected type "array". Did you forget to initialize a property or to make the return type nullable using "?array"?');
230264

231265
$object = eval('return new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty {};');

0 commit comments

Comments
 (0)