|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Nette; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\MethodReflection; |
| 8 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 9 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 10 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\Type; |
| 12 | +use PHPStan\Type\TypeCombinator; |
| 13 | + |
| 14 | +final class ComponentGetPresenterDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 15 | +{ |
| 16 | + |
| 17 | + public function getClass(): string |
| 18 | + { |
| 19 | + return 'Nette\Application\UI\Component'; |
| 20 | + } |
| 21 | + |
| 22 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 23 | + { |
| 24 | + return $methodReflection->getName() === 'getPresenter'; |
| 25 | + } |
| 26 | + |
| 27 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 28 | + { |
| 29 | + $defaultReturnType = ParametersAcceptorSelector::selectSingle( |
| 30 | + $methodReflection->getVariants() |
| 31 | + )->getReturnType(); |
| 32 | + if (count($methodCall->args) < 1) { |
| 33 | + return $defaultReturnType; |
| 34 | + } |
| 35 | + |
| 36 | + $paramNeedExpr = $methodCall->args[0]->value; |
| 37 | + $paramNeedType = $scope->getType($paramNeedExpr); |
| 38 | + |
| 39 | + if ($paramNeedType instanceof ConstantBooleanType) { |
| 40 | + if ($paramNeedType->getValue()) { |
| 41 | + return TypeCombinator::removeNull($defaultReturnType); |
| 42 | + } |
| 43 | + |
| 44 | + return TypeCombinator::addNull($defaultReturnType); |
| 45 | + } |
| 46 | + |
| 47 | + return $defaultReturnType; |
| 48 | + } |
| 49 | + |
| 50 | +} |
0 commit comments