-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBuilderMethodExtension.php
More file actions
130 lines (112 loc) · 4.2 KB
/
BuilderMethodExtension.php
File metadata and controls
130 lines (112 loc) · 4.2 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\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
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;
final class BuilderMethodExtension implements MethodsClassReflectionExtension, BrokerAwareExtension
{
/**
* @var \PHPStan\Broker\Broker
*/
private $broker;
/**
* @var \PHPStan\Reflection\MethodReflection[]
*/
private $methods = [];
/**
* @var \Weebly\PHPStan\Laravel\MethodReflectionFactory
*/
private $methodReflectionFactory;
/**
* @var AnnotationsHelper
*/
private $annotationsHelper;
/**
* BuilderMethodExtension 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 (!isset($this->methods[$classReflection->getName()]) && (
$classReflection->isSubclassOf(Model::class)
|| in_array(Builder::class, $this->annotationsHelper->getMixins($classReflection))
)) {
$builder = $this->broker->getClass(Builder::class);
$this->methods[$classReflection->getName()] = $this->createWrappedMethods($classReflection, $builder);
$queryBuilder = $this->broker->getClass(QueryBuilder::class);
$this->methods[$classReflection->getName()] += $this->createMethods($classReflection, $queryBuilder);
}
if ($classReflection->getName() === Builder::class && !isset($this->methods[Builder::class])) {
$queryBuilder = $this->broker->getClass(QueryBuilder::class);
$this->methods[Builder::class] = $this->createMethods($classReflection, $queryBuilder);
}
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 $queryBuilder
*
* @return \PHPStan\Reflection\MethodReflection[]
*/
private function createMethods(ClassReflection $classReflection, ClassReflection $queryBuilder): array
{
$methods = [];
foreach ($queryBuilder->getNativeReflection()->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->isStatic()) {
continue;
}
$methods[$method->getName()] = $this->methodReflectionFactory->create($classReflection, $method);
}
return $methods;
}
/**
* @param ClassReflection $classReflection
* @param \PHPStan\Reflection\ClassReflection $builder
*
* @return \PHPStan\Reflection\MethodReflection[]
*
* @throws \PHPStan\ShouldNotHappenException
*/
private function createWrappedMethods(ClassReflection $classReflection, ClassReflection $builder): array
{
$methods = [];
foreach ($builder->getNativeReflection()->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$methods[$method->getName()] = $this->methodReflectionFactory->create(
$classReflection,
$method,
ReflectionMethodAlwaysStatic::class
);
}
return $methods;
}
}