@@ -156,7 +156,13 @@ public function getProperties(string $class, array $context = []): ?array
156156
157157 public function getType (string $ class , string $ property , array $ context = []): ?Type
158158 {
159- [$ mutatorReflection , $ prefix ] = $ this ->getMutatorMethod ($ class , $ property );
159+ try {
160+ $ refClass = new \ReflectionClass ($ class );
161+ } catch (\ReflectionException ) {
162+ return null ;
163+ }
164+
165+ [$ mutatorReflection , $ prefix ] = $ this ->getMutatorMethod ($ refClass , $ property );
160166
161167 if ($ mutatorReflection ) {
162168 try {
@@ -171,7 +177,7 @@ public function getType(string $class, string $property, array $context = []): ?
171177 }
172178 }
173179
174- [$ accessorReflection , $ prefix ] = $ this ->getAccessorMethod ($ class , $ property );
180+ [$ accessorReflection , $ prefix ] = $ this ->getAccessorMethod ($ refClass , $ property );
175181 $ allowedPrefixes = array_diff ($ this ->accessorPrefixes , ['is ' , 'can ' , 'has ' ]);
176182 if ($ accessorReflection && (\in_array ($ prefix , $ allowedPrefixes , true ) || !property_exists ($ class , $ property ))) {
177183 try {
@@ -181,18 +187,13 @@ public function getType(string $class, string $property, array $context = []): ?
181187 }
182188
183189 if ($ context ['enable_constructor_extraction ' ] ?? $ this ->enableConstructorExtraction ) {
184- try {
185- $ reflectionClass = new \ReflectionClass ($ class );
186- if ($ type = $ this ->extractTypeFromConstructor ($ reflectionClass , $ property )) {
187- return $ type ;
188- }
189- } catch (\ReflectionException ) {
190+ if ($ type = $ this ->extractTypeFromConstructor ($ refClass , $ property )) {
191+ return $ type ;
190192 }
191193 }
192194
193195 try {
194- $ reflectionClass = new \ReflectionClass ($ class );
195- $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
196+ $ reflectionProperty = $ refClass ->getProperty ($ property );
196197 } catch (\ReflectionException ) {
197198 return null ;
198199 }
@@ -210,15 +211,15 @@ public function getType(string $class, string $property, array $context = []): ?
210211 }
211212
212213 $ allowedPrefixes = array_diff ($ this ->accessorPrefixes , $ allowedPrefixes );
213- [$ accessorReflection , $ prefix ] = $ this ->getAccessorMethod ($ class , $ property );
214+ [$ accessorReflection , $ prefix ] = $ this ->getAccessorMethod ($ refClass , $ property );
214215 if ($ accessorReflection && \in_array ($ prefix , $ allowedPrefixes , true )) {
215216 try {
216217 return $ this ->typeResolver ->resolve ($ accessorReflection );
217218 } catch (UnsupportedException ) {
218219 }
219220 }
220221
221- if (null === $ defaultValue = ($ reflectionClass ->getDefaultProperties ()[$ property ] ?? null )) {
222+ if (null === $ defaultValue = ($ refClass ->getDefaultProperties ()[$ property ] ?? null )) {
222223 return null ;
223224 }
224225
@@ -280,14 +281,20 @@ public function isWritable(string $class, string $property, array $context = [])
280281 return true ;
281282 }
282283
284+ try {
285+ $ refClass = new \ReflectionClass ($ class );
286+ } catch (\ReflectionException ) {
287+ return null ;
288+ }
289+
283290 // First test with the camelized property name
284- [$ reflectionMethod ] = $ this ->getMutatorMethod ($ class , $ this ->camelize ($ property ));
291+ [$ reflectionMethod ] = $ this ->getMutatorMethod ($ refClass , $ this ->camelize ($ property ));
285292 if (null !== $ reflectionMethod ) {
286293 return true ;
287294 }
288295
289296 // Otherwise check for the old way
290- [$ reflectionMethod ] = $ this ->getMutatorMethod ($ class , $ property );
297+ [$ reflectionMethod ] = $ this ->getMutatorMethod ($ refClass , $ property );
291298
292299 return null !== $ reflectionMethod ;
293300 }
@@ -375,10 +382,7 @@ public function getWriteInfo(string $class, string $property, array $context = [
375382 $ allowConstruct = $ context ['enable_constructor_extraction ' ] ?? $ this ->enableConstructorExtraction ;
376383 $ allowAdderRemover = $ context ['enable_adder_remover_extraction ' ] ?? true ;
377384
378- $ camelized = $ this ->camelize ($ property );
379- $ nonCamelized = ucfirst ($ property );
380385 $ constructor = $ reflClass ->getConstructor ();
381- $ singulars = $ this ->inflector ->singularize ($ camelized );
382386 $ errors = [];
383387
384388 if (null !== $ constructor && $ allowConstruct ) {
@@ -389,7 +393,10 @@ public function getWriteInfo(string $class, string $property, array $context = [
389393 }
390394 }
391395
392- [$ adderAccessName , $ removerAccessName , $ adderAndRemoverErrors ] = $ this ->findAdderAndRemover ($ reflClass , $ singulars );
396+ $ camelized = $ this ->camelize ($ property );
397+ $ nonCamelized = ucfirst ($ property );
398+
399+ [$ adderAccessName , $ removerAccessName , $ adderAndRemoverErrors ] = $ this ->findAdderAndRemover ($ reflClass , $ camelized );
393400 if ($ allowAdderRemover && null !== $ adderAccessName && null !== $ removerAccessName ) {
394401 $ adderMethod = $ reflClass ->getMethod ($ adderAccessName );
395402 $ removerMethod = $ reflClass ->getMethod ($ removerAccessName );
@@ -581,13 +588,13 @@ private function isAllowedProperty(string $class, string $property, bool $writeA
581588 * Returns an array with an instance of \ReflectionMethod as the first key
582589 * and the prefix of the method as the second, or null if not found.
583590 */
584- private function getAccessorMethod (string $ class , string $ property ): ?array
591+ private function getAccessorMethod (\ ReflectionClass $ refClass , string $ property ): ?array
585592 {
586593 $ ucProperty = ucfirst ($ property );
587594
588595 foreach ($ this ->accessorPrefixes as $ prefix ) {
589596 try {
590- $ reflectionMethod = new \ ReflectionMethod ( $ class , $ prefix .$ ucProperty );
597+ $ reflectionMethod = $ refClass -> getMethod ( $ prefix .$ ucProperty );
591598 if ($ reflectionMethod ->isStatic ()) {
592599 continue ;
593600 }
@@ -607,7 +614,7 @@ private function getAccessorMethod(string $class, string $property): ?array
607614 * Returns an array with an instance of \ReflectionMethod as the first key
608615 * and the prefix of the method as the second, or null if not found.
609616 */
610- private function getMutatorMethod (string $ class , string $ property ): ?array
617+ private function getMutatorMethod (\ ReflectionClass $ refClass , string $ property ): ?array
611618 {
612619 $ ucProperty = ucfirst ($ property );
613620 $ ucSingulars = $ this ->inflector ->singularize ($ ucProperty );
@@ -622,7 +629,7 @@ private function getMutatorMethod(string $class, string $property): ?array
622629
623630 foreach ($ names as $ name ) {
624631 try {
625- $ reflectionMethod = new \ ReflectionMethod ( $ class , $ prefix .$ name );
632+ $ reflectionMethod = $ refClass -> getMethod ( $ prefix .$ name );
626633 if ($ reflectionMethod ->isStatic ()) {
627634 continue ;
628635 }
@@ -667,11 +674,10 @@ private function getPropertyName(string $methodName, array $reflectionProperties
667674 * Searches for add and remove methods.
668675 *
669676 * @param \ReflectionClass $reflClass The reflection class for the given object
670- * @param array $singulars The singular form of the property name or null
671677 *
672678 * @return array An array containing the adder and remover when found and errors
673679 */
674- private function findAdderAndRemover (\ReflectionClass $ reflClass , array $ singulars ): array
680+ private function findAdderAndRemover (\ReflectionClass $ reflClass , string $ property ): array
675681 {
676682 if (2 !== \count ($ this ->arrayMutatorPrefixes )) {
677683 return [null , null , []];
@@ -680,7 +686,7 @@ private function findAdderAndRemover(\ReflectionClass $reflClass, array $singula
680686 [$ addPrefix , $ removePrefix ] = $ this ->arrayMutatorPrefixes ;
681687 $ errors = [];
682688
683- foreach ($ singulars as $ singular ) {
689+ foreach ($ this -> inflector -> singularize ( $ property ) as $ singular ) {
684690 $ addMethod = $ addPrefix .$ singular ;
685691 $ removeMethod = $ removePrefix .$ singular ;
686692
0 commit comments