MacroScanner.php
2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Carbon\PHPStan;
use Carbon\CarbonInterface;
use PHPStan\Reflection\ReflectionProvider;
use ReflectionClass;
use ReflectionException;
final class MacroScanner
{
/**
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
/**
* MacroScanner constructor.
*
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
*/
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
/**
* Return true if the given pair class-method is a Carbon macro.
*
* @param class-string $className
* @param string $methodName
*
* @return bool
*/
public function hasMethod(string $className, string $methodName): bool
{
$classReflection = $this->reflectionProvider->getClass($className);
if (
$classReflection->getName() !== CarbonInterface::class &&
!$classReflection->isSubclassOf(CarbonInterface::class)
) {
return false;
}
return \is_callable([$className, 'hasMacro']) &&
$className::hasMacro($methodName);
}
/**
* Return the Macro for a given pair class-method.
*
* @param class-string $className
* @param string $methodName
*
* @throws ReflectionException
*
* @return Macro
*/
public function getMethod(string $className, string $methodName): Macro
{
$reflectionClass = new ReflectionClass($className);
$property = $reflectionClass->getProperty('globalMacros');
$property->setAccessible(true);
$macro = $property->getValue()[$methodName];
return new Macro(
$className,
$methodName,
$macro
);
}
}