Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 3 | /** |
| 4 | * This file is part of the Carbon package. |
| 5 | * |
| 6 | * (c) Brian Nesbitt <brian@nesbot.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 12 | namespace Carbon\PHPStan; |
| 13 | |
| 14 | use PHPStan\Reflection\ClassReflection; |
| 15 | use PHPStan\Reflection\MethodReflection; |
| 16 | use PHPStan\Reflection\MethodsClassReflectionExtension; |
| 17 | use PHPStan\Reflection\Php\PhpMethodReflectionFactory; |
| 18 | use PHPStan\Type\TypehintHelper; |
| 19 | |
| 20 | /** |
| 21 | * Class MacroExtension. |
| 22 | * |
| 23 | * @codeCoverageIgnore Pure PHPStan wrapper. |
| 24 | */ |
| 25 | final class MacroExtension implements MethodsClassReflectionExtension |
| 26 | { |
| 27 | /** |
| 28 | * @var PhpMethodReflectionFactory |
| 29 | */ |
| 30 | protected $methodReflectionFactory; |
| 31 | |
| 32 | /** |
| 33 | * @var MacroScanner |
| 34 | */ |
| 35 | protected $scanner; |
| 36 | |
| 37 | /** |
| 38 | * Extension constructor. |
| 39 | * |
| 40 | * @param PhpMethodReflectionFactory $methodReflectionFactory |
| 41 | */ |
| 42 | public function __construct(PhpMethodReflectionFactory $methodReflectionFactory) |
| 43 | { |
| 44 | $this->scanner = new MacroScanner(); |
| 45 | $this->methodReflectionFactory = $methodReflectionFactory; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * {@inheritdoc} |
| 50 | */ |
| 51 | public function hasMethod(ClassReflection $classReflection, string $methodName): bool |
| 52 | { |
| 53 | return $this->scanner->hasMethod($classReflection->getName(), $methodName); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * {@inheritdoc} |
| 58 | */ |
| 59 | public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection |
| 60 | { |
| 61 | $builtinMacro = $this->scanner->getMethod($classReflection->getName(), $methodName); |
| 62 | |
| 63 | return $this->methodReflectionFactory->create( |
| 64 | $classReflection, |
| 65 | null, |
| 66 | $builtinMacro, |
| 67 | $classReflection->getActiveTemplateTypeMap(), |
| 68 | [], |
| 69 | TypehintHelper::decideTypeFromReflection($builtinMacro->getReturnType()), |
| 70 | null, |
| 71 | null, |
| 72 | $builtinMacro->isDeprecated()->yes(), |
| 73 | $builtinMacro->isInternal(), |
| 74 | $builtinMacro->isFinal(), |
| 75 | $builtinMacro->getDocComment() |
| 76 | ); |
| 77 | } |
| 78 | } |