@@ -207,6 +207,29 @@ public function getTypesFromConstructor(string $class, string $property): ?array
207
207
208
208
public function getType (string $ class , string $ property , array $ context = []): ?Type
209
209
{
210
+ // BC layer, remove context key in 8.0
211
+ $ extractPublicPropertiesFirst = array_key_exists ('extract_public_properties_first ' , $ context );
212
+ if (!$ extractPublicPropertiesFirst ) {
213
+ trigger_deprecation ('symfony/property-info ' , '7.3 ' , 'Not setting "extract_public_properties_first" context key is deprecated, it will default to "true" in 8.0. ' );
214
+ }
215
+
216
+ if ($ extractPublicPropertiesFirst ) {
217
+ try {
218
+ $ reflectionClass = new \ReflectionClass ($ class );
219
+ $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
220
+ } catch (\ReflectionException ) {
221
+ return null ;
222
+ }
223
+
224
+ if ($ reflectionProperty ->isPublic ()) {
225
+ try {
226
+ return $ this ->typeResolver ->resolve ($ reflectionProperty );
227
+ } catch (UnsupportedException $ e ) {
228
+ throw $ e ;
229
+ }
230
+ }
231
+ }
232
+
210
233
[$ mutatorReflection , $ prefix ] = $ this ->getMutatorMethod ($ class , $ property );
211
234
212
235
if ($ mutatorReflection ) {
@@ -240,27 +263,30 @@ public function getType(string $class, string $property, array $context = []): ?
240
263
}
241
264
}
242
265
243
- try {
244
- $ reflectionClass = new \ReflectionClass ($ class );
245
- $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
246
- } catch (\ReflectionException ) {
247
- return null ;
248
- }
266
+ // BC layer, remove block in 8.0
267
+ if (!$ extractPublicPropertiesFirst ) {
268
+ try {
269
+ $ reflectionClass = new \ReflectionClass ($ class );
270
+ $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
271
+ } catch (\ReflectionException ) {
272
+ return null ;
273
+ }
249
274
250
- try {
251
- return $ this ->typeResolver ->resolve ($ reflectionProperty );
252
- } catch (UnsupportedException ) {
253
- }
275
+ try {
276
+ return $ this ->typeResolver ->resolve ($ reflectionProperty );
277
+ } catch (UnsupportedException ) {
278
+ }
254
279
255
- if (null === $ defaultValue = ($ reflectionClass ->getDefaultProperties ()[$ property ] ?? null )) {
256
- return null ;
257
- }
280
+ if (null === $ defaultValue = ($ reflectionClass ->getDefaultProperties ()[$ property ] ?? null )) {
281
+ return null ;
282
+ }
258
283
259
- $ typeIdentifier = TypeIdentifier::from (static ::MAP_TYPES [\gettype ($ defaultValue )] ?? \gettype ($ defaultValue ));
260
- $ type = 'array ' === $ typeIdentifier ->value ? Type::array () : Type::builtin ($ typeIdentifier );
284
+ $ typeIdentifier = TypeIdentifier::from (static ::MAP_TYPES [\gettype ($ defaultValue )] ?? \gettype ($ defaultValue ));
285
+ $ type = 'array ' === $ typeIdentifier ->value ? Type::array () : Type::builtin ($ typeIdentifier );
261
286
262
- if ($ this ->isNullableProperty ($ class , $ property )) {
263
- $ type = Type::nullable ($ type );
287
+ if ($ this ->isNullableProperty ($ class , $ property )) {
288
+ $ type = Type::nullable ($ type );
289
+ }
264
290
}
265
291
266
292
return $ type ;
@@ -738,7 +764,7 @@ private function isAllowedProperty(string $class, string $property, bool $writeA
738
764
/**
739
765
* Gets the accessor method.
740
766
*
741
- * Returns an array with a the instance of \ReflectionMethod as first key
767
+ * Returns an array with the instance of \ReflectionMethod as first key
742
768
* and the prefix of the method as second or null if not found.
743
769
*/
744
770
private function getAccessorMethod (string $ class , string $ property ): ?array
@@ -764,7 +790,7 @@ private function getAccessorMethod(string $class, string $property): ?array
764
790
}
765
791
766
792
/**
767
- * Returns an array with a the instance of \ReflectionMethod as first key
793
+ * Returns an array with the instance of \ReflectionMethod as first key
768
794
* and the prefix of the method as second or null if not found.
769
795
*/
770
796
private function getMutatorMethod (string $ class , string $ property ): ?array
0 commit comments