Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace Carbon\PHPStan; |
| 4 | |
| 5 | use PHPStan\Reflection\ClassReflection; |
| 6 | use PHPStan\Reflection\MethodReflection; |
| 7 | use PHPStan\Reflection\MethodsClassReflectionExtension; |
| 8 | use PHPStan\Reflection\Php\PhpMethodReflectionFactory; |
| 9 | use PHPStan\Type\TypehintHelper; |
| 10 | |
| 11 | /** |
| 12 | * Class MacroExtension. |
| 13 | * |
| 14 | * @codeCoverageIgnore Pure PHPStan wrapper. |
| 15 | */ |
| 16 | final class MacroExtension implements MethodsClassReflectionExtension |
| 17 | { |
| 18 | /** |
| 19 | * @var PhpMethodReflectionFactory |
| 20 | */ |
| 21 | protected $methodReflectionFactory; |
| 22 | |
| 23 | /** |
| 24 | * @var MacroScanner |
| 25 | */ |
| 26 | protected $scanner; |
| 27 | |
| 28 | /** |
| 29 | * Extension constructor. |
| 30 | * |
| 31 | * @param PhpMethodReflectionFactory $methodReflectionFactory |
| 32 | */ |
| 33 | public function __construct(PhpMethodReflectionFactory $methodReflectionFactory) |
| 34 | { |
| 35 | $this->scanner = new MacroScanner(); |
| 36 | $this->methodReflectionFactory = $methodReflectionFactory; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 43 | { |
| 44 | return $this->scanner->hasMethod($classReflection->getName(), $methodName); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * {@inheritdoc} |
| 49 | */ |
| 50 | public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 51 | { |
| 52 | $builtinMacro = $this->scanner->getMethod($classReflection->getName(), $methodName); |
| 53 | |
| 54 | return $this->methodReflectionFactory->create( |
| 55 | $classReflection, |
| 56 | null, |
| 57 | $builtinMacro, |
| 58 | $classReflection->getActiveTemplateTypeMap(), |
| 59 | [], |
| 60 | TypehintHelper::decideTypeFromReflection($builtinMacro->getReturnType()), |
| 61 | null, |
| 62 | null, |
| 63 | $builtinMacro->isDeprecated()->yes(), |
| 64 | $builtinMacro->isInternal(), |
| 65 | $builtinMacro->isFinal(), |
| 66 | $builtinMacro->getDocComment() |
| 67 | ); |
| 68 | } |
| 69 | } |