|
7 | 7 | use PhpParser\Node\Expr\New_; |
8 | 8 | use PhpParser\Node\Expr\StaticCall; |
9 | 9 | use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Analyser\SpecifiedTypes; |
| 11 | +use PHPStan\Analyser\TypeSpecifier; |
| 12 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 13 | +use PHPStan\Analyser\TypeSpecifierContext; |
10 | 14 | use PHPStan\Reflection\Dummy\ChangedTypeMethodReflection; |
11 | 15 | use PHPStan\Reflection\MethodReflection; |
12 | 16 | use PHPStan\Reflection\ParametersAcceptorSelector; |
|
18 | 22 | use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
19 | 23 | use PHPStan\Type\IntegerType; |
20 | 24 | use PHPStan\Type\IntersectionType; |
| 25 | +use PHPStan\Type\MethodTypeSpecifyingExtension; |
21 | 26 | use PHPStan\Type\NeverType; |
22 | 27 | use PHPStan\Type\ObjectType; |
23 | 28 | use PHPStan\Type\ObjectWithoutClassType; |
@@ -263,3 +268,34 @@ public function getTypeFromStaticMethodCall( |
263 | 268 | return $scope->getType(new New_($methodCall->class)); |
264 | 269 | } |
265 | 270 | } |
| 271 | + |
| 272 | +class Bug7385MethodTypeSpecifyingExtension implements TypeSpecifierAwareExtension, MethodTypeSpecifyingExtension |
| 273 | +{ |
| 274 | + public function getClass(): string |
| 275 | + { |
| 276 | + return \Bug7385\Model::class; |
| 277 | + } |
| 278 | + |
| 279 | + public function isMethodSupported(MethodReflection $methodReflection, MethodCall $methodCall = null, TypeSpecifierContext $context = null): bool |
| 280 | + { |
| 281 | + return $methodReflection->getName() === 'assertHasIface'; |
| 282 | + } |
| 283 | + |
| 284 | + /** @var TypeSpecifier */ |
| 285 | + protected $typeSpecifier; |
| 286 | + |
| 287 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 288 | + { |
| 289 | + $this->typeSpecifier = $typeSpecifier; |
| 290 | + } |
| 291 | + |
| 292 | + public function specifyTypes(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
| 293 | + { |
| 294 | + $type = TypeCombinator::intersect( |
| 295 | + $scope->getType($methodCall->var), |
| 296 | + new ObjectType(\Bug7385\Iface::class) |
| 297 | + ); |
| 298 | + |
| 299 | + return $this->typeSpecifier->create($methodCall->var, $type, TypeSpecifierContext::createNull()); |
| 300 | + } |
| 301 | +} |
0 commit comments