File tree 3 files changed +48
-2
lines changed
src/Symfony/Component/Validator 3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -50,8 +50,21 @@ public function getPropertyValue($object)
50
50
{
51
51
$ reflProperty = $ this ->getReflectionMember ($ object );
52
52
53
- if (\PHP_VERSION_ID >= 70400 && !$ reflProperty ->isInitialized ($ object )) {
54
- return null ;
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 it is uninitialized.
55
+ // When trying to access an uninitialized property, __get method is triggered in PHP 7.4.0, but not in 7.4.1+.
56
+
57
+ // If __get method is not present, no fallback is possible
58
+ // Otherwise we need to catch a TypeError in case we are trying to access an uninitialized but set property.
59
+ if (!method_exists ($ object , '__get ' )) {
60
+ return null ;
61
+ }
62
+
63
+ try {
64
+ return $ reflProperty ->getValue ($ object );
65
+ } catch (\Error $ e ) {
66
+ return null ;
67
+ }
55
68
}
56
69
57
70
return $ reflProperty ->getValue ($ object );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \Validator \Tests \Fixtures ;
4
+
5
+ class Entity_74_Proxy extends Entity_74
6
+ {
7
+ public string $ notUnset ;
8
+
9
+ public function __construct ()
10
+ {
11
+ unset($ this ->uninitialized );
12
+ }
13
+
14
+ public function __get ($ name )
15
+ {
16
+ return 42 ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change 15
15
use Symfony \Component \Validator \Mapping \PropertyMetadata ;
16
16
use Symfony \Component \Validator \Tests \Fixtures \Entity ;
17
17
use Symfony \Component \Validator \Tests \Fixtures \Entity_74 ;
18
+ use Symfony \Component \Validator \Tests \Fixtures \Entity_74_Proxy ;
18
19
19
20
class PropertyMetadataTest extends TestCase
20
21
{
21
22
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity ' ;
22
23
const CLASSNAME_74 = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74 ' ;
24
+ const CLASSNAME_74_PROXY = 'Symfony\Component\Validator\Tests\Fixtures\Entity_74_Proxy ' ;
23
25
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent ' ;
24
26
25
27
public function testInvalidPropertyName ()
@@ -66,4 +68,17 @@ public function testGetPropertyValueFromUninitializedProperty()
66
68
67
69
$ this ->assertNull ($ metadata ->getPropertyValue ($ entity ));
68
70
}
71
+
72
+ /**
73
+ * @requires PHP 7.4
74
+ */
75
+ public function testGetPropertyValueFromUninitializedPropertyShouldNotReturnNullIfMagicGetIsPresent ()
76
+ {
77
+ $ entity = new Entity_74_Proxy ();
78
+ $ metadata = new PropertyMetadata (self ::CLASSNAME_74_PROXY , 'uninitialized ' );
79
+ $ notUnsetMetadata = new PropertyMetadata (self ::CLASSNAME_74_PROXY , 'notUnset ' );
80
+
81
+ $ this ->assertNull ($ notUnsetMetadata ->getPropertyValue ($ entity ));
82
+ $ this ->assertEquals (42 , $ metadata ->getPropertyValue ($ entity ));
83
+ }
69
84
}
You can’t perform that action at this time.
0 commit comments