Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef4dcdb

Browse files
committed
bug#35546 [Validator] check for __get method existence if property is uninitialized (alekitto)
This PR was merged into the 3.4 branch.Discussion----------[Validator] check for __get method existence if property is uninitialized| Q | A| ------------- | ---| Branch? | 3.4| Bug fix? | yes| New feature? | no| Deprecations? | no| Tickets |Fix#35544| License | MITResolve bug#35544.On PHP 7.4, check if object implements `__get` magic method if property is reported as uninitialized before returning null.Commits-------427bc3a [Validator] try to call __get method if property is uninitialized
2 parentsaf46fd6 +427bc3a commitef4dcdb

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,21 @@ public function getPropertyValue($object)
5050
{
5151
$reflProperty =$this->getReflectionMember($object);
5252

53-
if (\PHP_VERSION_ID >=70400 && !$reflProperty->isInitialized($object)) {
54-
returnnull;
53+
if (\PHP_VERSION_ID >=70400 &&$reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
54+
// There is no way to check if a property has been unset or if it is uninitialized.
55+
// When trying to access an uninitialized property, __get method is triggered.
56+
57+
// If __get method is not present, no fallback is possible
58+
// Otherwise we need to catch an Error in case we are trying to access an uninitialized but set property.
59+
if (!method_exists($object,'__get')) {
60+
returnnull;
61+
}
62+
63+
try {
64+
return$reflProperty->getValue($object);
65+
}catch (\Error$e) {
66+
returnnull;
67+
}
5568
}
5669

5770
return$reflProperty->getValue($object);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Validator\Tests\Fixtures;
4+
5+
class Entity_74_Proxyextends Entity_74
6+
{
7+
publicstring$notUnset;
8+
9+
publicfunction__construct()
10+
{
11+
unset($this->uninitialized);
12+
}
13+
14+
publicfunction__get($name)
15+
{
16+
return42;
17+
}
18+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
useSymfony\Component\Validator\Mapping\PropertyMetadata;
1616
useSymfony\Component\Validator\Tests\Fixtures\Entity;
1717
useSymfony\Component\Validator\Tests\Fixtures\Entity_74;
18+
useSymfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy;
1819

1920
class PropertyMetadataTestextends TestCase
2021
{
2122
constCLASSNAME ='Symfony\Component\Validator\Tests\Fixtures\Entity';
2223
constCLASSNAME_74 ='Symfony\Component\Validator\Tests\Fixtures\Entity_74';
24+
constCLASSNAME_74_PROXY ='Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy';
2325
constPARENTCLASS ='Symfony\Component\Validator\Tests\Fixtures\EntityParent';
2426

2527
publicfunctiontestInvalidPropertyName()
@@ -66,4 +68,17 @@ public function testGetPropertyValueFromUninitializedProperty()
6668

6769
$this->assertNull($metadata->getPropertyValue($entity));
6870
}
71+
72+
/**
73+
* @requires PHP 7.4
74+
*/
75+
publicfunctiontestGetPropertyValueFromUninitializedPropertyShouldNotReturnNullIfMagicGetIsPresent()
76+
{
77+
$entity =newEntity_74_Proxy();
78+
$metadata =newPropertyMetadata(self::CLASSNAME_74_PROXY,'uninitialized');
79+
$notUnsetMetadata =newPropertyMetadata(self::CLASSNAME_74_PROXY,'notUnset');
80+
81+
$this->assertNull($notUnsetMetadata->getPropertyValue($entity));
82+
$this->assertEquals(42,$metadata->getPropertyValue($entity));
83+
}
6984
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp