-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathMacroMethodsClassReflectionExtension.php
More file actions
147 lines (120 loc) · 5.65 KB
/
Copy pathMacroMethodsClassReflectionExtension.php
File metadata and controls
147 lines (120 loc) · 5.65 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace Saloon\PHPStan;
use Closure;
use function explode;
use function in_array;
use function is_array;
use function get_class;
use function is_string;
use function array_keys;
use ReflectionException;
use function is_callable;
use function str_contains;
use Saloon\Traits\Macroable;
use function array_key_exists;
use PHPStan\Type\ClosureTypeFactory;
use PHPStan\ShouldNotHappenException;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\MissingMethodFromReflectionException;
/**
* Many thanks to Larastan for building this excellent extension.
*
* @see https://github.com/larastan/larastan/blob/3.x/src/Methods/MacroMethodsClassReflectionExtension.php
*/
class MacroMethodsClassReflectionExtension implements MethodsClassReflectionExtension
{
/** @var array<string, MethodReflection> */
private array $methods = [];
/** @var array<string, array<string, bool>> */
private array $traitCache = [];
public function __construct(private ReflectionProvider $reflectionProvider, private ClosureTypeFactory $closureTypeFactory)
{
}
/**
* @throws ReflectionException
* @throws ShouldNotHappenException
* @throws MissingMethodFromReflectionException
*/
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
/** @var class-string[] $classNames */
$classNames = [];
$found = false;
$macroTraitProperty = null;
if ($this->hasIndirectTraitUse($classReflection, Macroable::class)) {
$classNames = [$classReflection->getName()];
$macroTraitProperty = 'macros';
}
if ($classNames !== [] && $macroTraitProperty) {
foreach ($classNames as $className) {
$macroClassReflection = $this->reflectionProvider->getClass($className);
if (! $macroClassReflection->getNativeReflection()->hasProperty($macroTraitProperty)) {
continue;
}
$refProperty = $macroClassReflection->getNativeReflection()->getProperty($macroTraitProperty);
$found = array_key_exists($methodName, $refProperty->getValue());
if (! $found) {
continue;
}
$macroDefinition = $refProperty->getValue()[$methodName];
if (is_string($macroDefinition)) {
if (str_contains($macroDefinition, '::')) {
$macroDefinition = explode('::', $macroDefinition, 2);
$macroClassName = $macroDefinition[0];
if (! $this->reflectionProvider->hasClass($macroClassName) || ! $this->reflectionProvider->getClass($macroClassName)->hasNativeMethod($macroDefinition[1])) {
throw new ShouldNotHappenException('Class ' . $macroClassName . ' does not exist');
}
$methodReflection = $this->reflectionProvider->getClass($macroClassName)->getNativeMethod($macroDefinition[1]);
} elseif (is_callable($macroDefinition)) {
$methodReflection = new Macro(
$macroClassReflection,
$methodName,
$this->closureTypeFactory->fromClosureObject(Closure::fromCallable($macroDefinition)),
);
} else {
throw new ShouldNotHappenException('Function ' . $macroDefinition . ' does not exist');
}
} elseif (is_array($macroDefinition)) {
if (is_string($macroDefinition[0])) {
$macroClassName = $macroDefinition[0];
} else {
$macroClassName = get_class($macroDefinition[0]);
}
if ($macroClassName === false || ! $this->reflectionProvider->hasClass($macroClassName) || ! $this->reflectionProvider->getClass($macroClassName)->hasNativeMethod($macroDefinition[1])) {
throw new ShouldNotHappenException('Class ' . $macroClassName . ' does not exist');
}
$methodReflection = $this->reflectionProvider->getClass($macroClassName)->getNativeMethod($macroDefinition[1]);
} else {
$methodReflection = new Macro(
$macroClassReflection,
$methodName,
$this->closureTypeFactory->fromClosureObject($macroDefinition),
);
$methodReflection->setIsStatic(true);
}
$this->methods[$classReflection->getName() . '-' . $methodName] = $methodReflection;
break;
}
}
return $found;
}
public function getMethod(
ClassReflection $classReflection,
string $methodName,
): MethodReflection {
return $this->methods[$classReflection->getName() . '-' . $methodName];
}
private function hasIndirectTraitUse(ClassReflection $class, string $traitName): bool
{
$className = $class->getName();
if (array_key_exists($className, $this->traitCache) && array_key_exists($traitName, $this->traitCache[$className])) {
return $this->traitCache[$className][$traitName];
}
$this->traitCache[$className][$traitName] = in_array($traitName, array_keys($class->getTraits(true)), true);
return $this->traitCache[$className][$traitName];
}
}