|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\Node\Identifier; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\ReflectionProvider; |
| 10 | +use PHPStan\Symfony\MessageMap; |
| 11 | +use PHPStan\Symfony\MessageMapFactory; |
| 12 | +use PHPStan\Type\ExpressionTypeResolverExtension; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\TypeCombinator; |
| 15 | +use function count; |
| 16 | +use function in_array; |
| 17 | +use function is_null; |
| 18 | + |
| 19 | +/** |
| 20 | + * Configurable extension for resolving return types of methods that internally use HandleTrait. |
| 21 | + * |
| 22 | + * Configured via PHPStan parameters under symfony.messenger.handleTraitWrappers with |
| 23 | + * "class::method" patterns, e.g.: |
| 24 | + * - App\Bus\QueryBus::dispatch |
| 25 | + * - App\Bus\QueryBus::query |
| 26 | + * - App\Bus\CommandBus::execute |
| 27 | + * - App\Bus\CommandBus::handle |
| 28 | + */ |
| 29 | +final class MessengerHandleTraitWrapperReturnTypeExtension implements ExpressionTypeResolverExtension |
| 30 | +{ |
| 31 | + |
| 32 | + private MessageMapFactory $messageMapFactory; |
| 33 | + |
| 34 | + private ?MessageMap $messageMap = null; |
| 35 | + |
| 36 | + /** @var array<string> */ |
| 37 | + private array $wrappers; |
| 38 | + |
| 39 | + private ReflectionProvider $reflectionProvider; |
| 40 | + |
| 41 | + /** @param array{handleTraitWrappers: array<string>}|null $messenger */ |
| 42 | + public function __construct(MessageMapFactory $messageMapFactory, ?array $messenger, ReflectionProvider $reflectionProvider) |
| 43 | + { |
| 44 | + $this->messageMapFactory = $messageMapFactory; |
| 45 | + $this->wrappers = $messenger['handleTraitWrappers'] ?? []; |
| 46 | + $this->reflectionProvider = $reflectionProvider; |
| 47 | + } |
| 48 | + |
| 49 | + public function getType(Expr $expr, Scope $scope): ?Type |
| 50 | + { |
| 51 | + if (!$this->isSupported($expr, $scope)) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + $args = $expr->getArgs(); |
| 56 | + if (count($args) !== 1) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + $arg = $args[0]->value; |
| 61 | + $argClassNames = $scope->getType($arg)->getObjectClassNames(); |
| 62 | + |
| 63 | + if (count($argClassNames) === 0) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + $returnTypes = []; |
| 68 | + foreach ($argClassNames as $argClassName) { |
| 69 | + $messageMap = $this->getMessageMap(); |
| 70 | + $returnType = $messageMap->getTypeForClass($argClassName); |
| 71 | + |
| 72 | + if (is_null($returnType)) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + $returnTypes[] = $returnType; |
| 77 | + } |
| 78 | + |
| 79 | + return TypeCombinator::union(...$returnTypes); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @phpstan-assert-if-true =MethodCall $expr |
| 84 | + */ |
| 85 | + private function isSupported(Expr $expr, Scope $scope): bool |
| 86 | + { |
| 87 | + if ($this->wrappers === []) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + if (!($expr instanceof MethodCall) || !($expr->name instanceof Identifier)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + $methodName = $expr->name->name; |
| 96 | + $varType = $scope->getType($expr->var); |
| 97 | + $classNames = $varType->getObjectClassNames(); |
| 98 | + |
| 99 | + if (count($classNames) === 0) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + foreach ($classNames as $className) { |
| 104 | + if (!$this->isClassMethodSupported($className, $methodName)) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + private function isClassMethodSupported(string $className, string $methodName): bool |
| 113 | + { |
| 114 | + $classMethodCombination = $className . '::' . $methodName; |
| 115 | + |
| 116 | + // Check if this exact class::method combination is configured |
| 117 | + if (in_array($classMethodCombination, $this->wrappers, true)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + // Check if any interface implemented by this class::method is configured |
| 122 | + if ($this->reflectionProvider->hasClass($className)) { |
| 123 | + $classReflection = $this->reflectionProvider->getClass($className); |
| 124 | + foreach ($classReflection->getInterfaces() as $interface) { |
| 125 | + $interfaceMethodCombination = $interface->getName() . '::' . $methodName; |
| 126 | + if (in_array($interfaceMethodCombination, $this->wrappers, true)) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + private function getMessageMap(): MessageMap |
| 136 | + { |
| 137 | + if ($this->messageMap === null) { |
| 138 | + $this->messageMap = $this->messageMapFactory->create(); |
| 139 | + } |
| 140 | + |
| 141 | + return $this->messageMap; |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments