1717use PHPStan \Reflection \MethodReflection ;
1818use PHPStan \Reflection \MethodsClassReflectionExtension ;
1919use PHPStan \Reflection \Php \DummyParameter ;
20- use PHPStan \Reflection \TrivialParametersAcceptor ;
20+ use PHPStan \Type \ArrayType ;
21+ use PHPStan \Type \BooleanType ;
2122use PHPStan \Type \Generic \TemplateTypeMap ;
23+ use PHPStan \Type \IntegerType ;
2224use PHPStan \Type \MixedType ;
25+ use PHPStan \Type \NullType ;
26+ use PHPStan \Type \ObjectType ;
27+ use PHPStan \Type \StringType ;
28+ use PHPStan \Type \UnionType ;
2329
2430abstract class AbstractMagicMethodReflectionExtension implements MethodsClassReflectionExtension
2531{
@@ -30,47 +36,143 @@ abstract class AbstractMagicMethodReflectionExtension implements MethodsClassRef
3036 */
3137 public function getMethod (ClassReflection $ classReflection , string $ methodName ): MethodReflection
3238 {
33- if (strpos ($ methodName , 'get ' ) === 0 ) {
34- return $ this ->returnGetMagicMethodReflection ($ classReflection , $ methodName );
39+ $ methodPrefix = substr ($ methodName , 0 , 3 );
40+ switch ($ methodPrefix ) {
41+ case 'get ' :
42+ return $ this ->returnGetMagicMethod ($ classReflection , $ methodName );
43+ case 'set ' :
44+ return $ this ->returnSetMagicMethod ($ classReflection , $ methodName );
45+ case 'uns ' :
46+ return $ this ->returnUnsetMagicMethod ($ classReflection , $ methodName );
47+ case 'has ' :
48+ return $ this ->returnHasMagicMethod ($ classReflection , $ methodName );
49+ default :
50+ break ;
3551 }
36-
37- return $ this ->returnMagicMethodReflection ($ classReflection , $ methodName );
3852 }
3953
4054 /**
41- * Helper method to create magic method for get calls. The method call does not accept any parameter and will return
42- * mixed type.
55+ * Helper method to create magic method reflection for get() calls.
4356 *
4457 * @param ClassReflection $classReflection
4558 * @param string $methodName
4659 * @return MethodReflection
4760 */
48- protected function returnGetMagicMethodReflection (
49- ClassReflection $ classReflection ,
50- string $ methodName
51- ): MethodReflection {
52- $ variants = new TrivialParametersAcceptor ();
61+ private function returnGetMagicMethod (ClassReflection $ classReflection , string $ methodName ): MethodReflection
62+ {
63+ $ params = [
64+ new DummyParameter (
65+ 'key ' ,
66+ new StringType (),
67+ true ,
68+ null ,
69+ false ,
70+ null
71+ ),
72+ new DummyParameter (
73+ 'index ' ,
74+ new UnionType ([new StringType (), new IntegerType (), new NullType ()]),
75+ true ,
76+ null ,
77+ false ,
78+ null
79+ )
80+ ];
81+
82+ $ returnType = new MixedType ();
83+
84+ $ variants = new FunctionVariant (
85+ TemplateTypeMap::createEmpty (),
86+ null ,
87+ $ params ,
88+ false ,
89+ $ returnType
90+ );
91+
5392 return new MagicMethodReflection ($ methodName , $ classReflection , [$ variants ]);
5493 }
5594
56- /**
57- * Helper method to create magic reflection method for set, unset and has calls. Those method calls accept one
58- * mixed parameter and return mixed type.
59- *
60- * @param ClassReflection $classReflection
61- * @param string $methodName
62- * @return MethodReflection
63- */
64- protected function returnMagicMethodReflection (
65- ClassReflection $ classReflection ,
66- string $ methodName
67- ): MethodReflection {
95+ private function returnSetMagicMethod (ClassReflection $ classReflection , string $ methodName ): MethodReflection
96+ {
97+ $ params = [
98+ new DummyParameter (
99+ 'key ' ,
100+ new UnionType ([new StringType (), new ArrayType (new MixedType (), new MixedType ())]),
101+ true ,
102+ null ,
103+ false ,
104+ null
105+ ),
106+ new DummyParameter (
107+ 'value ' ,
108+ new MixedType (),
109+ true ,
110+ null ,
111+ false ,
112+ null
113+ )
114+ ];
115+
116+ $ returnType = new ObjectType ($ classReflection ->getName ());
117+
118+ $ variants = new FunctionVariant (
119+ TemplateTypeMap::createEmpty (),
120+ null ,
121+ $ params ,
122+ false ,
123+ $ returnType
124+ );
125+
126+ return new MagicMethodReflection ($ methodName , $ classReflection , [$ variants ]);
127+ }
128+
129+ private function returnUnsetMagicMethod (ClassReflection $ classReflection , string $ methodName ): MethodReflection
130+ {
131+ $ params = [
132+ new DummyParameter (
133+ 'key ' ,
134+ new UnionType ([new NullType (), new StringType (), new ArrayType (new MixedType (), new MixedType ())]),
135+ true ,
136+ null ,
137+ false ,
138+ null
139+ )
140+ ];
141+
142+ $ returnType = new ObjectType ($ classReflection ->getName ());
143+
144+ $ variants = new FunctionVariant (
145+ TemplateTypeMap::createEmpty (),
146+ null ,
147+ $ params ,
148+ false ,
149+ $ returnType
150+ );
151+
152+ return new MagicMethodReflection ($ methodName , $ classReflection , [$ variants ]);
153+ }
154+
155+ private function returnHasMagicMethod (ClassReflection $ classReflection , string $ methodName ): MethodReflection
156+ {
157+ $ params = [
158+ new DummyParameter (
159+ 'key ' ,
160+ new StringType (),
161+ true ,
162+ null ,
163+ false ,
164+ null
165+ )
166+ ];
167+
168+ $ returnType = new BooleanType ();
169+
68170 $ variants = new FunctionVariant (
69171 TemplateTypeMap::createEmpty (),
70172 null ,
71- [ new DummyParameter ( ' name ' , new MixedType (), false , null , false , null ),] ,
173+ $ params ,
72174 false ,
73- new MixedType ()
175+ $ returnType
74176 );
75177
76178 return new MagicMethodReflection ($ methodName , $ classReflection , [$ variants ]);
0 commit comments