17
17
use PHPStan \Reflection \MethodReflection ;
18
18
use PHPStan \Reflection \MethodsClassReflectionExtension ;
19
19
use PHPStan \Reflection \Php \DummyParameter ;
20
- use PHPStan \Reflection \TrivialParametersAcceptor ;
20
+ use PHPStan \Type \ArrayType ;
21
+ use PHPStan \Type \BooleanType ;
21
22
use PHPStan \Type \Generic \TemplateTypeMap ;
23
+ use PHPStan \Type \IntegerType ;
22
24
use PHPStan \Type \MixedType ;
25
+ use PHPStan \Type \NullType ;
26
+ use PHPStan \Type \ObjectType ;
27
+ use PHPStan \Type \StringType ;
28
+ use PHPStan \Type \UnionType ;
23
29
24
30
abstract class AbstractMagicMethodReflectionExtension implements MethodsClassReflectionExtension
25
31
{
@@ -30,47 +36,143 @@ abstract class AbstractMagicMethodReflectionExtension implements MethodsClassRef
30
36
*/
31
37
public function getMethod (ClassReflection $ classReflection , string $ methodName ): MethodReflection
32
38
{
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 ;
35
51
}
36
-
37
- return $ this ->returnMagicMethodReflection ($ classReflection , $ methodName );
38
52
}
39
53
40
54
/**
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.
43
56
*
44
57
* @param ClassReflection $classReflection
45
58
* @param string $methodName
46
59
* @return MethodReflection
47
60
*/
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
+
53
92
return new MagicMethodReflection ($ methodName , $ classReflection , [$ variants ]);
54
93
}
55
94
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
+
68
170
$ variants = new FunctionVariant (
69
171
TemplateTypeMap::createEmpty (),
70
172
null ,
71
- [ new DummyParameter ( ' name ' , new MixedType (), false , null , false , null ),] ,
173
+ $ params ,
72
174
false ,
73
- new MixedType ()
175
+ $ returnType
74
176
);
75
177
76
178
return new MagicMethodReflection ($ methodName , $ classReflection , [$ variants ]);
0 commit comments