blob: 5a3d6941864ed8aa87bdec144325b5c51090c419 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace Carbon\PHPStan;
4
5use PHPStan\Reflection\ClassReflection;
6use PHPStan\Reflection\MethodReflection;
7use PHPStan\Reflection\MethodsClassReflectionExtension;
8use PHPStan\Reflection\Php\PhpMethodReflectionFactory;
9use PHPStan\Type\TypehintHelper;
10
11/**
12 * Class MacroExtension.
13 *
14 * @codeCoverageIgnore Pure PHPStan wrapper.
15 */
16final 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}