From 3bc23ae0df18767c3f5f0551f29379c6826816d0 Mon Sep 17 00:00:00 2001 From: Ivan Grigoriev Date: Fri, 31 Jan 2020 00:43:04 +0300 Subject: [PATCH] [Validator] fix access to uninitialized property when getting value --- Mapping/PropertyMetadata.php | 8 +++++++- Tests/Fixtures/Entity_74.php | 8 ++++++++ Tests/Mapping/PropertyMetadataTest.php | 13 +++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Tests/Fixtures/Entity_74.php diff --git a/Mapping/PropertyMetadata.php b/Mapping/PropertyMetadata.php index b03a059f8..872bd067b 100644 --- a/Mapping/PropertyMetadata.php +++ b/Mapping/PropertyMetadata.php @@ -48,7 +48,13 @@ public function __construct($class, $name) */ public function getPropertyValue($object) { - return $this->getReflectionMember($object)->getValue($object); + $reflProperty = $this->getReflectionMember($object); + + if (\PHP_VERSION_ID >= 70400 && !$reflProperty->isInitialized($object)) { + return null; + } + + return $reflProperty->getValue($object); } /** diff --git a/Tests/Fixtures/Entity_74.php b/Tests/Fixtures/Entity_74.php new file mode 100644 index 000000000..cb22fb7f7 --- /dev/null +++ b/Tests/Fixtures/Entity_74.php @@ -0,0 +1,8 @@ +expectException('Symfony\Component\Validator\Exception\ValidatorException'); $metadata->getPropertyValue($entity); } + + /** + * @requires PHP 7.4 + */ + public function testGetPropertyValueFromUninitializedProperty() + { + $entity = new Entity_74(); + $metadata = new PropertyMetadata(self::CLASSNAME_74, 'uninitialized'); + + $this->assertNull($metadata->getPropertyValue($entity)); + } }