|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Arachne\Codeception\PHPStan; |
| 6 | + |
| 7 | +use Arachne\Codeception\Module\NetteDIModule; |
| 8 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Name; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | + |
| 17 | +class NetteDIModuleType implements DynamicMethodReturnTypeExtension |
| 18 | +{ |
| 19 | + public function getClass(): string |
| 20 | + { |
| 21 | + return NetteDIModule::class; |
| 22 | + } |
| 23 | + |
| 24 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 25 | + { |
| 26 | + return $methodReflection->getName() === 'grabService'; |
| 27 | + } |
| 28 | + |
| 29 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 30 | + { |
| 31 | + if (!$methodCall->args) { |
| 32 | + return $methodReflection->getReturnType(); |
| 33 | + } |
| 34 | + |
| 35 | + $arg = $methodCall->args[0]->value; |
| 36 | + |
| 37 | + if (!$arg instanceof ClassConstFetch) { |
| 38 | + return $methodReflection->getReturnType(); |
| 39 | + } |
| 40 | + |
| 41 | + $class = $arg->class; |
| 42 | + |
| 43 | + if (!$class instanceof Name) { |
| 44 | + return $methodReflection->getReturnType(); |
| 45 | + } |
| 46 | + |
| 47 | + $class = (string) $class; |
| 48 | + |
| 49 | + if ($class === 'static') { |
| 50 | + return $methodReflection->getReturnType(); |
| 51 | + } |
| 52 | + |
| 53 | + if ($class === 'self') { |
| 54 | + $class = $scope->getClassReflection()->getName(); |
| 55 | + } |
| 56 | + |
| 57 | + return new ObjectType($class); |
| 58 | + } |
| 59 | +} |
0 commit comments