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

Skip to content

Commit 428ffd9

Browse files
Merge branch '5.4' into 6.0
* 5.4: [HttpClient] Remove deprecated usage of `GuzzleHttp\Promise\queue` [PropertyAccess] Fix handling of uninitialized property of anonymous class [FrameworkBundle] Allow default cache pools to be overwritten by user [DependencyInjection] fix test ResolveBindingsPass remove loading of class iterable [FrameworkBundle] Avoid calling rtrim(null, '/') in AssetsInstallCommand Optimization of resolveEnvPlaceholders Fix incorrect format [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 Use correct tag for ExpoTransportFactory service [DependencyInjection] Ignore argument type check in CheckTypeDeclarationsPass if it's a Definition with a factory [Validators] Add translations for Slovak #43735
2 parents 300edcb + 4bb27fa commit 428ffd9

2 files changed

Lines changed: 42 additions & 16 deletions

File tree

PropertyAccessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,11 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
427427
}
428428
} catch (\Error $e) {
429429
// handle uninitialized properties in PHP >= 7.4
430-
if (preg_match('/^Typed property ([\w\\\]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
431-
$r = new \ReflectionProperty($matches[1], $matches[2]);
430+
if (preg_match('/^Typed property ('.preg_quote(get_debug_type($object), '/').')::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
431+
$r = new \ReflectionProperty($class, $matches[2]);
432432
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
433433

434-
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);
434+
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);
435435
}
436436

437437
throw $e;

Tests/PropertyAccessorTest.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16-
use Symfony\Component\PropertyAccess\Exception\AccessException;
1716
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
1817
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1918
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
@@ -144,44 +143,73 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetter()
144143

145144
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousClass()
146145
{
147-
$this->expectException(AccessException::class);
146+
$this->expectException(UninitializedPropertyException::class);
148147
$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"?');
149148

150-
$object = eval('return new class() {
149+
$object = new class() {
151150
private $uninitialized;
152151

153152
public function getUninitialized(): array
154153
{
155154
return $this->uninitialized;
156155
}
157-
};');
156+
};
157+
158+
$this->propertyAccessor->getValue($object, 'uninitialized');
159+
}
160+
161+
public function testGetValueThrowsExceptionIfUninitializedNotNullablePropertyWithGetterOfAnonymousClass()
162+
{
163+
$this->expectException(UninitializedPropertyException::class);
164+
$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.');
165+
166+
$object = new class() {
167+
private string $uninitialized;
168+
169+
public function getUninitialized(): string
170+
{
171+
return $this->uninitialized;
172+
}
173+
};
174+
175+
$this->propertyAccessor->getValue($object, 'uninitialized');
176+
}
177+
178+
public function testGetValueThrowsExceptionIfUninitializedPropertyOfAnonymousClass()
179+
{
180+
$this->expectException(UninitializedPropertyException::class);
181+
$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.');
182+
183+
$object = new class() {
184+
public string $uninitialized;
185+
};
158186

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

162190
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousStdClass()
163191
{
164-
$this->expectException(AccessException::class);
192+
$this->expectException(UninitializedPropertyException::class);
165193
$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"?');
166194

167-
$object = eval('return new class() extends \stdClass {
195+
$object = new class() extends \stdClass {
168196
private $uninitialized;
169197

170198
public function getUninitialized(): array
171199
{
172200
return $this->uninitialized;
173201
}
174-
};');
202+
};
175203

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

179207
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousChildClass()
180208
{
181-
$this->expectException(AccessException::class);
209+
$this->expectException(UninitializedPropertyException::class);
182210
$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"?');
183211

184-
$object = eval('return new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty {};');
212+
$object = new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty {};
185213

186214
$this->propertyAccessor->getValue($object, 'uninitialized');
187215
}
@@ -684,7 +712,7 @@ public function testAnonymousClassWrite()
684712

685713
private function generateAnonymousClass($value)
686714
{
687-
$obj = eval('return new class($value)
715+
return new class($value)
688716
{
689717
private $foo;
690718

@@ -708,9 +736,7 @@ public function setFoo($foo)
708736
{
709737
$this->foo = $foo;
710738
}
711-
};');
712-
713-
return $obj;
739+
};
714740
}
715741

716742
public function testThrowTypeErrorInsideSetterCall()

0 commit comments

Comments
 (0)