blob: 8e2524c099179274ef7251fc2d6a13424fdf059f [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01003/**
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 Benkard7b2a3a12021-08-16 10:57:25 +020012namespace Carbon\PHPStan;
13
14use PHPStan\Reflection\ClassReflection;
15use PHPStan\Reflection\MethodReflection;
16use PHPStan\Reflection\MethodsClassReflectionExtension;
17use PHPStan\Reflection\Php\PhpMethodReflectionFactory;
18use PHPStan\Type\TypehintHelper;
19
20/**
21 * Class MacroExtension.
22 *
23 * @codeCoverageIgnore Pure PHPStan wrapper.
24 */
25final 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}