|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Methods; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\FuncCall; |
| 6 | +use PhpParser\Node\Name\FullyQualified; |
| 7 | +use PhpParser\Node\Stmt\Class_; |
| 8 | +use PHPStan\Analyser\DependencyTracker; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\FunctionReflection; |
| 11 | +use PHPStan\Reflection\ParameterReflection; |
| 12 | +use PHPStan\Reflection\ReflectionProvider; |
| 13 | +use PHPStan\Type\FunctionParameterClosureThisExtension; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use function in_array; |
| 17 | + |
| 18 | +class ClosureThisSubclassExtension implements FunctionParameterClosureThisExtension |
| 19 | +{ |
| 20 | + |
| 21 | + public function __construct(private ReflectionProvider $reflectionProvider) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool |
| 26 | + { |
| 27 | + return in_array($functionReflection->getName(), ['ClosureThisSubclass\\runInSubclass', 'ClosureThisSubclass\\runInBase'], true) |
| 28 | + && $parameter->getName() === 'callback'; |
| 29 | + } |
| 30 | + |
| 31 | + public function getClosureThisTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope&DependencyTracker $scope): ?Type |
| 32 | + { |
| 33 | + if ($functionReflection->getName() === 'ClosureThisSubclass\\runInBase') { |
| 34 | + return new ObjectType('ClosureThisSubclass\\BaseTestCase'); |
| 35 | + } |
| 36 | + |
| 37 | + $classNode = new Class_(null, [ |
| 38 | + 'extends' => new FullyQualified('ClosureThisSubclass\\BaseTestCase'), |
| 39 | + ], $functionCall->getAttributes()); |
| 40 | + $classReflection = $this->reflectionProvider->getAnonymousClassReflection($classNode, $scope); |
| 41 | + |
| 42 | + return new ObjectType($classReflection->getName(), classReflection: $classReflection); |
| 43 | + } |
| 44 | + |
| 45 | +} |
0 commit comments