8000 bug #35532 [Validator] fix access to uninitialized property when gett… · symfony/symfony@aeea451 · GitHub
[go: up one dir, main page]

Skip to content

Commit aeea451

Browse files
committed
bug #35532 [Validator] fix access to uninitialized property when getting value (greedyivan)
This PR was squashed before being merged into the 3.4 branch (closes #35532). Discussion ---------- [Validator] fix access to uninitialized property when getting value | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #35454 | License | MIT | Doc PR | In PHP 7.4, the type-hinted property is [uninitialized](https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties) by default. So it needs to be checked before use. Commits ------- 1edecf7 [Validator] fix access to uninitialized property when getting value
2 parents e50db1f + 1edecf7 commit aeea451

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/Symfony/Component/Validator/Mapping/PropertyMetadata.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public function __construct($class, $name)
4848
*/
4949
8000 public function getPropertyValue($object)
5050
{
51-
return $this->getReflectionMember($object)->getValue($object);
51+
$reflProperty = $this->getReflectionMember($object);
52+
53+
if (\PHP_VERSION_ID >= 70400 && !$reflProperty->isInitialized($object)) {
54+
return null;
55+
}
56+
57+
return $reflProperty->getValue($object);
5258
}
5359

5460
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Fixtures;
4+
5+
class Entity_74
6+
{
7+
public int $uninitialized;
8+
}

src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Mapping\PropertyMetadata;
1616
use Symfony\Component\Validator\Tests\Fixtures\Entity;
17+
use Symfony\Component\Validator\Tests\Fixtures\Entity_74;
1718

1819
class PropertyMetadataTest extends TestCase
1920
{
2021
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
22+
const CLASSNAME_74 = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74';
2123
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
2224

2325
public function testInvalidPropertyName()
@@ -53,4 +55,15 @@ public function testGetPropertyValueFromRemovedProperty()
5355
$this->expectException('Symfony\Component\Validator\Exception\ValidatorException');
5456
$metadata->getPropertyValue($entity);
5557
}
58+
59+
/**
60+
* @requires PHP 7.4
61+
*/
62+
public function testGetPropertyValueFromUninitializedProperty()
63+
{
64+
$entity = new Entity_74();
65+
$metadata = new PropertyMetadata(self::CLASSNAME_74, 'uninitialized');
66+
67+
$this->assertNull($metadata->getPropertyValue($entity));
68+
}
5669
}

0 commit comments

Comments
 (0)
0