-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathFacadeMethodExtension.php
More file actions
130 lines (112 loc) · 4.24 KB
/
FacadeMethodExtension.php
File metadata and controls
130 lines (112 loc) · 4.24 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
<?php declare(strict_types = 1);
namespace Weebly\PHPStan\Laravel;
use Illuminate\Auth\AuthManager;
use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Support\Facades\Facade;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\MethodReflection;
use Weebly\PHPStan\Laravel\Utils\AnnotationsHelper;
use PHPStan\Broker\ClassNotFoundException;
final class FacadeMethodExtension implements MethodsClassReflectionExtension, BrokerAwareExtension
{
/**
* @var \PHPStan\Broker\Broker
*/
private $broker;
/**
* @var string[]
*/
private $extensions = [
AuthManager::class => 'guard',
BroadcastManager::class => 'driver',
];
/**
* @var \PHPStan\Reflection\MethodReflection[]
*/
private $methods = [];
/**
* @var \Weebly\PHPStan\Laravel\MethodReflectionFactory
*/
private $methodReflectionFactory;
/**
* @var AnnotationsHelper
*/
private $annotationsHelper;
/**
* FacadeMethodExtension constructor.
*
* @param \Weebly\PHPStan\Laravel\MethodReflectionFactory $methodReflectionFactory
* @param AnnotationsHelper $annotationsHelper
*/
public function __construct(MethodReflectionFactory $methodReflectionFactory, AnnotationsHelper $annotationsHelper)
{
$this->methodReflectionFactory = $methodReflectionFactory;
$this->annotationsHelper = $annotationsHelper;
}
/**
* @inheritdoc
*/
public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}
/**
* @inheritdoc
*/
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
if ($classReflection->isSubclassOf(Facade::class)) {
if (!isset($this->methods[$classReflection->getName()])) {
/** @var \Illuminate\Support\Facades\Facade $class */
$class = $classReflection->getName();
$instance = $class::getFacadeRoot();
$instanceReflection = $this->broker->getClass(get_class($instance));
$this->methods[$classReflection->getName()] = $this->createMethods($classReflection, $instanceReflection);
foreach ($this->annotationsHelper->getMixins($instanceReflection) as $mixin) {
try {
$mixinInstanceReflection = $this->broker->getClass($mixin);
} catch (ClassNotFoundException $e) {
continue;
}
$this->methods[$classReflection->getName()] += $this->createMethods($classReflection, $mixinInstanceReflection);
}
if (isset($this->extensions[$instanceReflection->getName()])) {
$extensionMethod = $this->extensions[$instanceReflection->getName()];
$extensionReflection = $this->broker->getClass(get_class($instance->$extensionMethod()));
$this->methods[$classReflection->getName()] += $this->createMethods($classReflection, $extensionReflection);
}
}
}
return isset($this->methods[$classReflection->getName()][$methodName]);
}
/**
* @inheritdoc
*/
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
return $this->methods[$classReflection->getName()][$methodName];
}
/**
* @param \PHPStan\Reflection\ClassReflection $classReflection
* @param \PHPStan\Reflection\ClassReflection $instance
*
* @return \PHPStan\Reflection\MethodReflection[]
*
* @throws \PHPStan\ShouldNotHappenException
*/
private function createMethods(ClassReflection $classReflection, ClassReflection $instance): array
{
$methods = [];
foreach ($instance->getNativeReflection()->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$methods[$method->getName()] = $this->methodReflectionFactory->create(
$classReflection,
$method,
ReflectionMethodAlwaysStatic::class
);
}
return $methods;
}
}