|
5 | 5 | namespace ARiddlestone\PHPStanCakePHP2; |
6 | 6 |
|
7 | 7 | use Exception; |
8 | | -use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod; |
9 | 8 | use PHPStan\Reflection\ClassReflection; |
10 | | -use PHPStan\Reflection\ExtendedMethodReflection; |
11 | 9 | use PHPStan\Reflection\MethodReflection; |
12 | 10 | use PHPStan\Reflection\MethodsClassReflectionExtension; |
13 | | -use PHPStan\Reflection\ParameterReflection; |
14 | | -use PHPStan\Reflection\ParametersAcceptor; |
15 | 11 | use PHPStan\Reflection\ReflectionProvider; |
16 | | -use PHPStan\Type\ObjectType; |
17 | 12 |
|
18 | 13 | /** |
19 | 14 | * Adds methods to {@link Model}s from {@link ModelBehavior} classes. |
@@ -100,100 +95,23 @@ private function getBehaviorMethods(): array |
100 | 95 | ); |
101 | 96 | $this->behaviorMethods = []; |
102 | 97 | foreach ($classReflections as $classReflection) { |
| 98 | + $modelBehaviorMethodExtractor = |
| 99 | + new ModelBehaviorMethodExtractor($classReflection); |
103 | 100 | $this->behaviorMethods = array_merge( |
104 | 101 | $this->behaviorMethods, |
105 | | - $this->getModelBehaviorMethods($classReflection) |
| 102 | + $modelBehaviorMethodExtractor->getModelBehaviorMethods() |
106 | 103 | ); |
107 | 104 | } |
108 | 105 | } |
109 | 106 | return $this->behaviorMethods; |
110 | 107 | } |
111 | 108 |
|
112 | | - /** |
113 | | - * Returns all methods for the class which have a first parameter of Model, |
114 | | - * with that parameter removed. |
115 | | - * |
116 | | - * Also filters out private, protected, and static methods. |
117 | | - * |
118 | | - * @return array<MethodReflection> |
119 | | - */ |
120 | | - private function getModelBehaviorMethods( |
121 | | - ClassReflection $classReflection |
122 | | - ): array { |
123 | | - $methodNames = array_map( |
124 | | - [$this, 'getMethodReflectionName'], |
125 | | - $classReflection->getNativeReflection()->getMethods() |
126 | | - ); |
127 | | - /** @var array<ExtendedMethodReflection> $methodReflections */ |
128 | | - $methodReflections = array_filter( |
129 | | - array_map([$classReflection, 'getNativeMethod'], $methodNames), |
130 | | - [$this, 'filterBehaviorMethods'] |
131 | | - ); |
132 | | - return array_map([$this, 'wrapBehaviorMethod'], $methodReflections); |
133 | | - } |
134 | | - |
135 | | - /** |
136 | | - * Returns true if the given method would be made available as a behavior |
137 | | - * method. |
138 | | - * |
139 | | - * Specifically it must meet the following conditions: |
140 | | - * * Be public |
141 | | - * * Not be static |
142 | | - * * Have at least one variant which accepts a Model as its first parameter |
143 | | - */ |
144 | | - private function filterBehaviorMethods( |
145 | | - ExtendedMethodReflection $methodReflection |
146 | | - ): bool { |
147 | | - return $methodReflection->isPublic() |
148 | | - && ! $methodReflection->isStatic() |
149 | | - && array_filter( |
150 | | - $methodReflection->getVariants(), |
151 | | - [$this, 'filterBehaviorMethodVariants'] |
152 | | - ); |
153 | | - } |
154 | | - |
155 | | - /** |
156 | | - * Returns true if the given method variant accepts a Model as its first |
157 | | - * parameter. |
158 | | - */ |
159 | | - private function filterBehaviorMethodVariants( |
160 | | - ParametersAcceptor $parametersAcceptor |
161 | | - ): bool { |
162 | | - $parameters = $parametersAcceptor->getParameters(); |
163 | | - /** @var ParameterReflection|null $firstParameter */ |
164 | | - $firstParameter = array_shift($parameters); |
165 | | - |
166 | | - if (! $firstParameter) { |
167 | | - return false; |
168 | | - } |
169 | | - |
170 | | - if ( |
171 | | - $firstParameter->getType() |
172 | | - ->isSuperTypeOf(new ObjectType('Model')) |
173 | | - ->no() |
174 | | - ) { |
175 | | - return false; |
176 | | - } |
177 | | - |
178 | | - return true; |
179 | | - } |
180 | | - |
181 | | - /** |
182 | | - * Wraps a method reflection in a wrapper which removes the first parameter. |
183 | | - */ |
184 | | - private function wrapBehaviorMethod( |
185 | | - MethodReflection $methodReflection |
186 | | - ): MethodReflection { |
187 | | - return new ModelBehaviorMethodWrapper($methodReflection); |
188 | | - } |
189 | | - |
190 | 109 | /** |
191 | 110 | * Returns the name of a method from its reflection. |
192 | | - * |
193 | | - * @param MethodReflection|ReflectionMethod $methodReflection |
194 | 111 | */ |
195 | | - private function getMethodReflectionName($methodReflection): string |
196 | | - { |
| 112 | + private function getMethodReflectionName( |
| 113 | + MethodReflection $methodReflection |
| 114 | + ): string { |
197 | 115 | return $methodReflection->getName(); |
198 | 116 | } |
199 | 117 | } |
0 commit comments