@@ -59,7 +59,8 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
59
59
private $ accessorPrefixes ;
60
60
private $ arrayMutatorPrefixes ;
61
61
private $ enableConstructorExtraction ;
62
- private $ accessFlags ;
62
+ private $ methodReflectionFlags ;
63
+ private $ propertyReflectionFlags ;
63
64
64
65
/**
65
66
* @param string[]|null $mutatorPrefixes
@@ -72,7 +73,8 @@ public function __construct(array $mutatorPrefixes = null, array $accessorPrefix
72
73
$ this ->accessorPrefixes = null !== $ accessorPrefixes ? $ accessorPrefixes : self ::$ defaultAccessorPrefixes ;
73
74
$ this ->arrayMutatorPrefixes = null !== $ arrayMutatorPrefixes ? $ arrayMutatorPrefixes : self ::$ defaultArrayMutatorPrefixes ;
74
75
$ this ->enableConstructorExtraction = $ enableConstructorExtraction ;
75
- $ this ->accessFlags = $ accessFlags ;
76
+ $ this ->methodReflectionFlags = $ this ->getMethodsFlags ($ accessFlags );
77
+ $ this ->propertyReflectionFlags = $ this ->getPropertyFlags ($ accessFlags );
76
78
}
77
79
78
80
/**
@@ -86,34 +88,16 @@ public function getProperties($class, array $context = [])
86
88
return ;
87
89
}
88
90
89
- $ propertyFlags = 0 ;
90
- $ methodFlags = 0 ;
91
-
92
- if ($ this ->accessFlags & self ::ALLOW_PUBLIC ) {
93
- $ propertyFlags = $ propertyFlags | \ReflectionProperty::IS_PUBLIC ;
94
- $ methodFlags = $ methodFlags | \ReflectionMethod::IS_PUBLIC ;
95
- }
96
-
97
- if ($ this ->accessFlags & self ::ALLOW_PRIVATE ) {
98
- $ propertyFlags = $ propertyFlags | \ReflectionProperty::IS_PRIVATE ;
99
- $ methodFlags = $ methodFlags | \ReflectionMethod::IS_PRIVATE ;
100
- }
101
-
102
- if ($ this ->accessFlags & self ::ALLOW_PROTECTED ) {
103
- $ propertyFlags = $ propertyFlags | \ReflectionProperty::IS_PROTECTED ;
104
- $ methodFlags = $ methodFlags | \ReflectionMethod::IS_PROTECTED ;
105
- }
106
-
107
91
$ reflectionProperties = $ reflectionClass ->getProperties ();
108
92
109
93
$ properties = [];
110
94
foreach ($ reflectionProperties as $ reflectionProperty ) {
111
- if ($ reflectionProperty ->getModifiers () & $ propertyFlags ) {
95
+ if ($ reflectionProperty ->getModifiers () & $ this -> propertyReflectionFlags ) {
112
96
$ properties [$ reflectionProperty ->name ] = $ reflectionProperty ->name ;
113
97
}
114
98
}
115
99
116
- foreach ($ reflectionClass ->getMethods ($ methodFlags ) as $ reflectionMethod ) {
100
+ foreach ($ reflectionClass ->getMethods ($ this -> methodReflectionFlags ) as $ reflectionMethod ) {
117
101
if ($ reflectionMethod ->isStatic ()) {
118
102
continue ;
119
103
}
@@ -232,26 +216,26 @@ public function getReadAccessor(string $class, string $property, array $context
232
216
$ accessPrivate = false ;
233
217
$ accessStatic = false ;
234
218
235
- if ($ reflClass ->hasMethod ($ getter ) && $ reflClass ->getMethod ($ getter )->isPublic ( )) {
219
+ if ($ reflClass ->hasMethod ($ getter ) && ( $ reflClass ->getMethod ($ getter )->getModifiers () & $ this -> methodReflectionFlags )) {
236
220
$ accessType = ReadAccessor::TYPE_METHOD ;
237
221
$ accessName = $ getter ;
238
222
$ accessStatic = $ reflClass ->getMethod ($ getter )->isStatic ();
239
- } elseif ($ reflClass ->hasMethod ($ getsetter ) && $ reflClass ->getMethod ($ getsetter )->isPublic ( )) {
223
+ } elseif ($ reflClass ->hasMethod ($ getsetter ) && ( $ reflClass ->getMethod ($ getsetter )->getModifiers () & $ this -> methodReflectionFlags )) {
240
224
$ accessType = ReadAccessor::TYPE_METHOD ;
241
225
$ accessName = $ getsetter ;
242
226
$ accessStatic = $ reflClass ->getMethod ($ getsetter )->isStatic ();
243
- } elseif ($ reflClass ->hasMethod ($ isser ) && $ reflClass ->getMethod ($ isser )->isPublic ( )) {
227
+ } elseif ($ reflClass ->hasMethod ($ isser ) && ( $ reflClass ->getMethod ($ isser )->getModifiers () & $ this -> methodReflectionFlags )) {
244
228
$ accessType = ReadAccessor::TYPE_METHOD ;
245
229
$ accessName = $ isser ;
246
230
$ accessStatic = $ reflClass ->getMethod ($ isser )->isStatic ();
247
- } elseif ($ reflClass ->hasMethod ($ hasser ) && $ reflClass ->getMethod ($ hasser )->isPublic ( )) {
231
+ } elseif ($ reflClass ->hasMethod ($ hasser ) && ( $ reflClass ->getMethod ($ hasser )->getModifiers () & $ this -> methodReflectionFlags )) {
248
232
$ accessType = ReadAccessor::TYPE_METHOD ;
249
233
$ accessName = $ hasser ;
250
234
$ accessStatic = $ reflClass ->getMethod ($ hasser )->isStatic ();
251
- } elseif ($ reflClass ->hasMethod ('__get ' ) && $ reflClass ->getMethod ('__get ' )->isPublic ( )) {
235
+ } elseif ($ reflClass ->hasMethod ('__get ' ) && ( $ reflClass ->getMethod ('__get ' )->getModifiers () & $ this -> methodReflectionFlags )) {
252
236
$ accessType = ReadAccessor::TYPE_PROPERTY ;
253
237
$ accessName = $ property ;
254
- } elseif ($ hasProperty ) {
238
+ } elseif ($ hasProperty && ( $ reflClass -> getProperty ( $ property )-> getModifiers () & $ this -> propertyReflectionFlags ) ) {
255
239
$ accessType = ReadAccessor::TYPE_PROPERTY ;
256
240
$ accessName = $ property ;
257
241
$ accessStatic = $ reflClass ->getProperty ($ property )->isStatic ();
@@ -315,7 +299,7 @@ public function getWriteMutator(string $class, string $property, array $context
315
299
} elseif ($ this ->isMethodAccessible ($ reflClass , '__set ' , 2 )) {
316
300
$ accessType = WriteMutator::TYPE_PROPERTY ;
317
301
$ accessName = $ property ;
318
- } elseif ($ hasProperty ) {
302
+ } elseif ($ hasProperty && ( $ reflClass -> getProperty ( $ property )-> getModifiers () & $ this -> propertyReflectionFlags ) ) {
319
303
$ accessType = WriteMutator::TYPE_PROPERTY ;
320
304
$ accessName = $ property ;
321
305
$ accessPrivate = !$ reflClass ->getProperty ($ property )->isPublic ();
@@ -464,19 +448,7 @@ private function isAllowedProperty(string $class, string $property): bool
464
448
try {
465
449
$ reflectionProperty = new \ReflectionProperty ($ class , $ property );
466
450
467
- if ($ this ->accessFlags & self ::ALLOW_PUBLIC && $ reflectionProperty ->isPublic ()) {
468
- return true ;
469
- }
470
-
471
- if ($ this ->accessFlags & self ::ALLOW_PROTECTED && $ reflectionProperty ->isProtected ()) {
472
- return true ;
473
- }
474
-
475
- if ($ this ->accessFlags & self ::ALLOW_PRIVATE && $ reflectionProperty ->isPrivate ()) {
476
- return true ;
477
- }
478
-
479
- return false ;
451
+ return $ reflectionProperty ->getModifiers () & $ this ->propertyReflectionFlags ;
480
452
} catch (\ReflectionException $ e ) {
481
453
// Return false if the property doesn't exist
482
454
}
@@ -601,7 +573,7 @@ private function isMethodAccessible(\ReflectionClass $class, string $methodName,
601
573
if ($ class ->hasMethod ($ methodName )) {
602
574
$ method = $ class ->getMethod ($ methodName );
603
575
604
- if ($ method ->isPublic ( )
576
+ if (( $ method ->getModifiers () & $ this -> methodReflectionFlags )
605
577
&& $ method ->getNumberOfRequiredParameters () <= $ parameters
606
578
&& $ method ->getNumberOfParameters () >= $ parameters ) {
607
579
return true ;
@@ -618,4 +590,48 @@ private function camelize(string $string): string
618
590
{
619
591
return str_replace (' ' , '' , ucwords (str_replace ('_ ' , ' ' , $ string )));
620
592
}
593
+
594
+ /**
595
+ * Return allowed reflection method flags.
596
+ */
597
+ private function getMethodsFlags (int $ accessFlags ): int
598
+ {
599
+ $ methodFlags = 0 ;
600
+
601
+ if ($ accessFlags & self ::ALLOW_PUBLIC ) {
602
+ $ methodFlags |= \ReflectionMethod::IS_PUBLIC ;
603
+ }
604
+
605
+ if ($ accessFlags & self ::ALLOW_PRIVATE ) {
606
+ $ methodFlags |= \ReflectionMethod::IS_PRIVATE ;
607
+ }
608
+
609
+ if ($ accessFlags & self ::ALLOW_PROTECTED ) {
610
+ $ methodFlags |= \ReflectionMethod::IS_PROTECTED ;
611
+ }
612
+
613
+ return $ methodFlags ;
614
+ }
615
+
616
+ /**
617
+ * Return allowed reflection property flags.
618
+ */
619
+ private function getPropertyFlags (int $ accessFlags ): int
620
+ {
621
+ $ propertyFlags = 0 ;
622
+
623
+ if ($ accessFlags & self ::ALLOW_PUBLIC ) {
624
+ $ propertyFlags |= \ReflectionProperty::IS_PUBLIC ;
625
+ }
626
+
627
+ if ($ accessFlags & self ::ALLOW_PRIVATE ) {
628
+ $ propertyFlags |= \ReflectionProperty::IS_PRIVATE ;
629
+ }
630
+
631
+ if ($ accessFlags & self ::ALLOW_PROTECTED ) {
632
+ $ propertyFlags |= \ReflectionProperty::IS_PROTECTED ;
633
+ }
634
+
635
+ return $ propertyFlags ;
636
+ }
621
637
}
0 commit comments