|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ARiddlestone\PHPStanCakePHP2; |
| 6 | + |
| 7 | +use Component; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Scalar\String_; |
| 10 | +use PHPStan\Analyser\Scope; |
| 11 | +use PHPStan\Reflection\MethodReflection; |
| 12 | +use PHPStan\Reflection\ReflectionProvider; |
| 13 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | + |
| 17 | +class LoadComponentOnFlyMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 18 | +{ |
| 19 | + private ReflectionProvider $reflectionProvider; |
| 20 | + |
| 21 | + public function __construct(ReflectionProvider $reflectionProvider) |
| 22 | + { |
| 23 | + $this->reflectionProvider = $reflectionProvider; |
| 24 | + } |
| 25 | + |
| 26 | + public function getClass(): string |
| 27 | + { |
| 28 | + return \ComponentCollection::class; |
| 29 | + } |
| 30 | + |
| 31 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 32 | + { |
| 33 | + return $methodReflection->getName() === 'load'; |
| 34 | + } |
| 35 | + |
| 36 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type |
| 37 | + { |
| 38 | + $arg = $methodCall->getArgs()[0]->value; |
| 39 | + |
| 40 | + if (!$arg instanceof String_) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + $componentName = $arg->value . 'Component'; |
| 45 | + |
| 46 | + if (!$this->reflectionProvider->hasClass($componentName)) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if (!$this->reflectionProvider->getClass($componentName)->is(Component::class)) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return new ObjectType($componentName); |
| 55 | + } |
| 56 | +} |
0 commit comments