Skip to content

[PropertyAccess] Fixes getValue() on an unitialized object property on a lazy ghost #53891

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
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.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private function readProperty(array $zval, string $property, bool $ignoreInvalid
}
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
if (preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches) || preg_match('/^Cannot access uninitialized non-nullable property ([\w\\\\@]+)::\$(\w+) by reference$/', $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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 UninitializedObjectProperty
{
public \DateTimeInterface $uninitialized;
private \DateTimeInterface $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 @@ -37,8 +37,10 @@
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestSingularAndPluralProps;
use Symfony\Component\PropertyAccess\Tests\Fixtures\Ticket5775Object;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TypeHinted;
use Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty;
use Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedPrivateProperty;
use Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedProperty;
use Symfony\Component\VarExporter\ProxyHelper;

class PropertyAccessorTest extends TestCase
{
Expand Down Expand Up @@ -225,7 +227,8 @@ public function testGetValueThrowsExceptionIfUninitializedPropertyWithGetterOfAn
$this->expectException(UninitializedPropertyException::class);
$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"?');

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

$this->propertyAccessor->getValue($object, 'uninitialized');
}
Expand Down Expand Up @@ -958,4 +961,54 @@ public function testCastDateTimeImmutable()

$this->assertInstanceOf(\DateTime::class, $object->getDate());
}

public function testGetValuePropertyThrowsExceptionIfUninitializedWithoutLazyGhost()
{
$this->expectException(UninitializedPropertyException::class);
$this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$uninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.');

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

public function testGetValueGetterThrowsExceptionIfUninitializedWithoutLazyGhost()
{
$this->expectException(UninitializedPropertyException::class);
$this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$privateUninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.');

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

private function createUninitializedObjectPropertyGhost(): UninitializedObjectProperty
{
$class = 'UninitializedObjectPropertyGhost';

if (!class_exists($class)) {
eval('class '.$class.ProxyHelper::generateLazyGhost(new \ReflectionClass(UninitializedObjectProperty::class)));
}

$this->assertTrue(class_exists($class));

return $class::createLazyGhost(initializer: function ($instance) {
});
}

public function testGetValuePropertyThrowsExceptionIfUninitializedWithLazyGhost()
{
$this->expectException(UninitializedPropertyException::class);
$this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$uninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.');

$lazyGhost = $this->createUninitializedObjectPropertyGhost();

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

public function testGetValueGetterThrowsExceptionIfUninitializedWithLazyGhost()
{
$this->expectException(UninitializedPropertyException::class);
$this->expectExceptionMessage('The property "Symfony\Component\PropertyAccess\Tests\Fixtures\UninitializedObjectProperty::$privateUninitialized" is not readable because it is typed "DateTimeInterface". You should initialize it or declare a default value instead.');

$lazyGhost = $this->createUninitializedObjectPropertyGhost();

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