|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\FuncCall; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\Node\Expr\New_; |
| 8 | +use PhpParser\Node\Expr\StaticCall; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use PhpParser\Node\Name; |
| 11 | +use PHPStan\Analyser\DependencyTracker; |
| 12 | +use PHPStan\Analyser\Scope; |
| 13 | +use PHPStan\DependencyInjection\AutowiredExtensions; |
| 14 | +use PHPStan\DependencyInjection\AutowiredService; |
| 15 | +use PHPStan\DependencyInjection\ExtensionsCollection; |
| 16 | +use PHPStan\Reflection\FunctionReflection; |
| 17 | +use PHPStan\Reflection\MethodReflection; |
| 18 | +use PHPStan\Reflection\ParameterReflection; |
| 19 | +use PHPStan\Type\FunctionParameterClosureTypeExtension; |
| 20 | +use PHPStan\Type\MethodParameterClosureTypeExtension; |
| 21 | +use PHPStan\Type\StaticMethodParameterClosureTypeExtension; |
| 22 | +use PHPStan\Type\Type; |
| 23 | + |
| 24 | +/** |
| 25 | + * Asks the parameter closure type extensions for the type of a closure passed to a parameter, |
| 26 | + * the same way ArgumentsHandler does when it analyses the closure body. |
| 27 | + */ |
| 28 | +#[AutowiredService] |
| 29 | +final class ParameterClosureTypeHelper |
| 30 | +{ |
| 31 | + |
| 32 | + /** |
| 33 | + * @param ExtensionsCollection<FunctionParameterClosureTypeExtension> $functionParameterClosureTypeExtensions |
| 34 | + * @param ExtensionsCollection<MethodParameterClosureTypeExtension> $methodParameterClosureTypeExtensions |
| 35 | + * @param ExtensionsCollection<StaticMethodParameterClosureTypeExtension> $staticMethodParameterClosureTypeExtensions |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + #[AutowiredExtensions(of: FunctionParameterClosureTypeExtension::class)] |
| 39 | + private ExtensionsCollection $functionParameterClosureTypeExtensions, |
| 40 | + #[AutowiredExtensions(of: MethodParameterClosureTypeExtension::class)] |
| 41 | + private ExtensionsCollection $methodParameterClosureTypeExtensions, |
| 42 | + #[AutowiredExtensions(of: StaticMethodParameterClosureTypeExtension::class)] |
| 43 | + private ExtensionsCollection $staticMethodParameterClosureTypeExtensions, |
| 44 | + ) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + public function getParameterType( |
| 49 | + FuncCall|MethodCall|StaticCall|New_ $callLike, |
| 50 | + FunctionReflection|MethodReflection $calleeReflection, |
| 51 | + ParameterReflection $parameter, |
| 52 | + Scope&DependencyTracker $scope, |
| 53 | + ): ?Type |
| 54 | + { |
| 55 | + if ($callLike instanceof FuncCall && $calleeReflection instanceof FunctionReflection) { |
| 56 | + foreach ($this->functionParameterClosureTypeExtensions->getAll() as $extension) { |
| 57 | + if ($extension->isFunctionSupported($calleeReflection, $parameter)) { |
| 58 | + return $extension->getTypeFromFunctionCall($calleeReflection, $callLike, $parameter, $scope); |
| 59 | + } |
| 60 | + } |
| 61 | + } elseif ($calleeReflection instanceof MethodReflection) { |
| 62 | + if ($callLike instanceof StaticCall) { |
| 63 | + foreach ($this->staticMethodParameterClosureTypeExtensions->getAll() as $extension) { |
| 64 | + if ($extension->isStaticMethodSupported($calleeReflection, $parameter)) { |
| 65 | + return $extension->getTypeFromStaticMethodCall($calleeReflection, $callLike, $parameter, $scope); |
| 66 | + } |
| 67 | + } |
| 68 | + } elseif ($callLike instanceof New_ && $callLike->class instanceof Name) { |
| 69 | + $staticCall = new StaticCall( |
| 70 | + $callLike->class, |
| 71 | + new Identifier('__construct'), |
| 72 | + $callLike->getArgs(), |
| 73 | + ); |
| 74 | + foreach ($this->staticMethodParameterClosureTypeExtensions->getAll() as $extension) { |
| 75 | + if ($extension->isStaticMethodSupported($calleeReflection, $parameter)) { |
| 76 | + return $extension->getTypeFromStaticMethodCall($calleeReflection, $staticCall, $parameter, $scope); |
| 77 | + } |
| 78 | + } |
| 79 | + } elseif ($callLike instanceof MethodCall) { |
| 80 | + foreach ($this->methodParameterClosureTypeExtensions->getAll() as $extension) { |
| 81 | + if ($extension->isMethodSupported($calleeReflection, $parameter)) { |
| 82 | + return $extension->getTypeFromMethodCall($calleeReflection, $callLike, $parameter, $scope); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments