Skip to content
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

[Validator] check for __get method existence if property is uninitialized #35546

Merged
merged 1 commit into from Feb 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
Expand Up @@ -50,8 +50,21 @@ public function getPropertyValue($object)
{
$reflProperty = $this->getReflectionMember($object);

if (\PHP_VERSION_ID >= 70400 && !$reflProperty->isInitialized($object)) {
return null;
if (\PHP_VERSION_ID >= 70400 && $reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
// There is no way to check if a property has been unset or if it is uninitialized.
// When trying to access an uninitialized property, __get method is triggered.

// If __get method is not present, no fallback is possible
// Otherwise we need to catch an Error in case we are trying to access an uninitialized but set property.
if (!method_exists($object, '__get')) {
return null;
}

try {
return $reflProperty->getValue($object);
} catch (\Error $e) {
return null;
}
}

return $reflProperty->getValue($object);
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Validator/Tests/Fixtures/Entity_74_Proxy.php
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\Validator\Tests\Fixtures;

class Entity_74_Proxy extends Entity_74
{
public string $notUnset;

public function __construct()
{
unset($this->uninitialized);
}

public function __get($name)
{
return 42;
}
}
Expand Up @@ -15,11 +15,13 @@
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Tests\Fixtures\Entity_74;
use Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy;

class PropertyMetadataTest extends TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const CLASSNAME_74 = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74';
const CLASSNAME_74_PROXY = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';

public function testInvalidPropertyName()
Expand Down Expand Up @@ -66,4 +68,17 @@ public function testGetPropertyValueFromUninitializedProperty()

$this->assertNull($metadata->getPropertyValue($entity));
}

/**
* @requires PHP 7.4
*/
public function testGetPropertyValueFromUninitializedPropertyShouldNotReturnNullIfMagicGetIsPresent()
{
$entity = new Entity_74_Proxy();
$metadata = new PropertyMetadata(self::CLASSNAME_74_PROXY, 'uninitialized');
$notUnsetMetadata = new PropertyMetadata(self::CLASSNAME_74_PROXY, 'notUnset');

$this->assertNull($notUnsetMetadata->getPropertyValue($entity));
$this->assertEquals(42, $metadata->getPropertyValue($entity));
}
}