Skip to content

Commit e6d51a8

Browse files
Merge branch '3.4' into 4.4
* 3.4: Fixes sprintf(): Too few arguments in form transformer [Console] Fix QuestionHelper::disableStty() validate subforms in all validation groups Update Hungarian translations Add meaningful message when Process is not installed (ProcessHelper) [PropertyAccess] Fix TypeError parsing again. [Form] add missing Czech validators translation [Validator] add missing Czech translations never directly validate Existence (Required/Optional) constraints
2 parents 4483229 + e1a6c91 commit e6d51a8

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

PropertyAccessor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,15 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
392392
try {
393393
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]}();
394394
} catch (\TypeError $e) {
395+
list($trace) = $e->getTrace();
396+
395397
// handle uninitialized properties in PHP >= 7
396-
if (preg_match((sprintf('/^Return value of %s::%s\(\) must be of (?:the )?type (\w+), null returned$/', preg_quote(\get_class($object)), $access[self::ACCESS_NAME])), $e->getMessage(), $matches)) {
397-
throw new AccessException(sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', \get_class($object), $access[self::ACCESS_NAME], $matches[1]), 0, $e);
398+
if (__FILE__ === $trace['file']
399+
&& $access[self::ACCESS_NAME] === $trace['function']
400+
&& $object instanceof $trace['class']
401+
&& preg_match((sprintf('/Return value (?:of .*::\w+\(\) )?must be of (?:the )?type (\w+), null returned$/')), $e->getMessage(), $matches)
402+
) {
403+
throw new AccessException(sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', false === strpos(\get_class($object), "@anonymous\0") ? \get_class($object) : (get_parent_class($object) ?: 'class').'@anonymous', $access[self::ACCESS_NAME], $matches[1]), 0, $e);
398404
}
399405

400406
throw $e;

Tests/PropertyAccessorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,59 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetter()
155155
$this->propertyAccessor->getValue(new UninitializedPrivateProperty(), 'uninitialized');
156156
}
157157

158+
/**
159+
* @requires PHP 7
160+
*/
161+
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousClass()
162+
{
163+
$this->expectException('Symfony\Component\PropertyAccess\Exception\AccessException');
164+
$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"?');
165+
166+
$object = eval('return new class() {
167+
private $uninitialized;
168+
169+
public function getUninitialized(): array
170+
{
171+
return $this->uninitialized;
172+
}
173+
};');
174+
175+
$this->propertyAccessor->getValue($object, 'uninitialized');
176+
}
177+
178+
/**
179+
* @requires PHP 7
180+
*/
181+
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousStdClass()
182+
{
183+
$this->expectException('Symfony\Component\PropertyAccess\Exception\AccessException');
184+
$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"?');
185+
186+
$object = eval('return new class() extends \stdClass {
187+
private $uninitialized;
188+
189+
public function getUninitialized(): array
190+
{
191+
return $this->uninitialized;
192+
}
193+
};');
194+
195+
$this->propertyAccessor->getValue($object, 'uninitialized');
196+
}
197+
198+
/**
199+
* @requires PHP 7
200+
*/
201+
public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAnonymousChildClass()
202+
{
203+
$this->expectException('Symfony\Component\PropertyAccess\Exception\AccessException');
204+
$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"?');
205+
206+
$object = eval('return new class() extends \Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty {};');
207+
208+
$this->propertyAccessor->getValue($object, 'uninitialized');
209+
}
210+
158211
public function testGetValueThrowsExceptionIfNotArrayAccess()
159212
{
160213
$this->expectException('Symfony\Component\PropertyAccess\Exception\NoSuchIndexException');

0 commit comments

Comments
 (0)