8000 [PropertyAccess] Fix handling of uninitialized property of parent class by filiplikavcan · Pull Request #45255 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Fix handling of uninitialized property of parent class #45255

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
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. 10000
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 @@ -401,7 +401,7 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
&& $object instanceof $trace['class']
&& preg_match('/Return value (?:of .*::\w+\(\) )?must be of (?:the )?type (\w+), null returned$/', $e->getMessage(), $matches)
) {
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"?', !str_contains(\get_class($object), "@anonymous\0") ? \get_class($object) : (get_parent_class($object) ?: 'class').'@anonymous', $access[self::ACCESS_NAME], $matches[1]), 0, $e);
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_debug_type($object), $access[self::ACCESS_NAME], $matches[1]), 0, $e);
}

throw $e;
Expand Down Expand Up @@ -436,8 +436,8 @@ 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 ('.preg_quote(get_debug_type($object), '/').')::\$(\w+) must not be acce 10000 ssed before initialization$/', $e->getMessage(), $matches)) {
$r = new \ReflectionProperty($class, $matches[2]);
if (\PHP_VERSION_ID >= 70400 && preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
$r = new \ReflectionProperty(str_contains($matches[1], '@anonymous') ? $class : $matches[1], $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.', $matches[1], $r->getName(), $type), 0, $e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

class ExtendedUninitializedProperty extends UninitializedProperty
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
class UninitializedProperty
{
public string $uninitialized;
private string $privateUninitialized;

public function getPrivateUninitialized(): string
{
return $this->privateUninitialized;
}

public function setPrivateUninitialized(string $privateUninitialized): void
{
$this->privateUninitialized = $privateUninitialized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ExtendedUninitializedProperty;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderRemoverInvalidArgumentLength;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderRemoverInvalidMethods;
Expand Down Expand Up @@ -211,6 +212,28 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyOfAnonymousCla
$this->propertyAccessor->getValue($object, 'uninitialized');
}

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

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

/**
* @requires PHP 7.4
*/
public function testGetValueThrowsExceptionIfUninitializedNotNullablePropertyWithGetterOfParentClass()
{
$this->expectException(AccessException::class);
$this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedProperty::$privateUninitialized" is not readable because it is typed "string". You should initialize it or declare a default value instead.');

$this->propertyAccessor->getValue(new ExtendedUninitializedProperty(), 'privateUninitialized');
}

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