|
| 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\Symfony\Configuration; |
| 10 | +use PHPStan\Symfony\MessageMap; |
| 11 | +use PHPStan\Symfony\MessageMapFactory; |
| 12 | +use PHPStan\Type\ExpressionTypeResolverExtension; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use function count; |
| 15 | +use function in_array; |
| 16 | +use function is_null; |
| 17 | + |
| 18 | +/** |
| 19 | + * Configurable extension for resolving return types of methods that internally use HandleTrait. |
| 20 | + * |
| 21 | + * Configured via PHPStan parameters under symfony.messenger.handleTraitWrappers with |
| 22 | + * "Class::method" patterns, e.g.: |
| 23 | + * - App\Bus\QueryBus::dispatch |
| 24 | + * - App\Bus\QueryBus::query |
| 25 | + * - App\Bus\CommandBus::execute |
| 26 | + * - App\Bus\CommandBus::handle |
| 27 | + */ |
| 28 | +final class MessengerHandleTraitWrapperReturnTypeExtension implements ExpressionTypeResolverExtension |
| 29 | +{ |
| 30 | + |
| 31 | + /** @var MessageMapFactory */ |
| 32 | + private $messageMapFactory; |
| 33 | + |
| 34 | + /** @var MessageMap|null */ |
| 35 | + private $messageMap; |
| 36 | + |
| 37 | + /** @var array<string> */ |
| 38 | + private $wrappers; |
| 39 | + |
| 40 | + public function __construct(MessageMapFactory $messageMapFactory, Configuration $configuration) |
| 41 | + { |
| 42 | + $this->messageMapFactory = $messageMapFactory; |
| 43 | + $this->wrappers = $configuration->getMessengerHandleTraitWrappers(); |
| 44 | + } |
| 45 | + |
| 46 | + public function getType(Expr $expr, Scope $scope): ?Type |
| 47 | + { |
| 48 | + if (!$this->isSupported($expr, $scope)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + $args = $expr->getArgs(); |
| 53 | + if (count($args) !== 1) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + $arg = $args[0]->value; |
| 58 | + $argClassNames = $scope->getType($arg)->getObjectClassNames(); |
| 59 | + |
| 60 | + if (count($argClassNames) === 1) { |
| 61 | + $messageMap = $this->getMessageMap(); |
| 62 | + $returnType = $messageMap->getTypeForClass($argClassNames[0]); |
| 63 | + |
| 64 | + if (!is_null($returnType)) { |
| 65 | + return $returnType; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @phpstan-assert-if-true =MethodCall $expr |
| 74 | + */ |
| 75 | + private function isSupported(Expr $expr, Scope $scope): bool |
| 76 | + { |
| 77 | + if (!($expr instanceof MethodCall) || !($expr->name instanceof Identifier)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + $methodName = $expr->name->name; |
| 82 | + $varType = $scope->getType($expr->var); |
| 83 | + $classNames = $varType->getObjectClassNames(); |
| 84 | + |
| 85 | + if (count($classNames) !== 1) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + $className = $classNames[0]; |
| 90 | + $classMethodCombination = $className . '::' . $methodName; |
| 91 | + |
| 92 | + // Check if this class::method combination is configured |
| 93 | + return in_array($classMethodCombination, $this->wrappers, true); |
| 94 | + } |
| 95 | + |
| 96 | + private function getMessageMap(): MessageMap |
| 97 | + { |
| 98 | + if ($this->messageMap === null) { |
| 99 | + $this->messageMap = $this->messageMapFactory->create(); |
| 100 | + } |
| 101 | + |
| 102 | + return $this->messageMap; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments