/
ClassStatementsGatherer.php
199 lines (175 loc) · 4.8 KB
/
ClassStatementsGatherer.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php declare(strict_types = 1);
namespace PHPStan\Node;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Constant\ClassConstantFetch;
use PHPStan\Node\Property\PropertyRead;
use PHPStan\Node\Property\PropertyWrite;
use PHPStan\Reflection\ClassReflection;
use PHPStan\ShouldNotHappenException;
use function count;
class ClassStatementsGatherer
{
/** @var callable(Node $node, Scope $scope): void */
private $nodeCallback;
/** @var ClassPropertyNode[] */
private array $properties = [];
/** @var ClassMethod[] */
private array $methods = [];
/** @var \PHPStan\Node\Method\MethodCall[] */
private array $methodCalls = [];
/** @var array<int, PropertyWrite|PropertyRead> */
private array $propertyUsages = [];
/** @var Node\Stmt\ClassConst[] */
private array $constants = [];
/** @var ClassConstantFetch[] */
private array $constantFetches = [];
/**
* @param callable(Node $node, Scope $scope): void $nodeCallback
*/
public function __construct(
private ClassReflection $classReflection,
callable $nodeCallback,
)
{
$this->nodeCallback = $nodeCallback;
}
/**
* @return ClassPropertyNode[]
*/
public function getProperties(): array
{
return $this->properties;
}
/**
* @return ClassMethod[]
*/
public function getMethods(): array
{
return $this->methods;
}
/**
* @return Method\MethodCall[]
*/
public function getMethodCalls(): array
{
return $this->methodCalls;
}
/**
* @return array<int, PropertyWrite|PropertyRead>
*/
public function getPropertyUsages(): array
{
return $this->propertyUsages;
}
/**
* @return Node\Stmt\ClassConst[]
*/
public function getConstants(): array
{
return $this->constants;
}
/**
* @return ClassConstantFetch[]
*/
public function getConstantFetches(): array
{
return $this->constantFetches;
}
public function __invoke(Node $node, Scope $scope): void
{
$nodeCallback = $this->nodeCallback;
$nodeCallback($node, $scope);
$this->gatherNodes($node, $scope);
}
private function gatherNodes(Node $node, Scope $scope): void
{
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}
if ($scope->getClassReflection()->getName() !== $this->classReflection->getName()) {
return;
}
if ($node instanceof ClassPropertyNode) {
$this->properties[] = $node;
if ($node->isPromoted()) {
$this->propertyUsages[] = new PropertyWrite(
new PropertyFetch(new Expr\Variable('this'), new Identifier($node->getName())),
$scope,
);
}
return;
}
if ($node instanceof Node\Stmt\ClassMethod) {
$this->methods[] = new ClassMethod($node, $scope->isInTrait());
return;
}
if ($node instanceof Node\Stmt\ClassConst) {
$this->constants[] = $node;
return;
}
if ($node instanceof MethodCall || $node instanceof StaticCall) {
$this->methodCalls[] = new \PHPStan\Node\Method\MethodCall($node, $scope);
return;
}
if ($node instanceof MethodCallableNode || $node instanceof StaticMethodCallableNode) {
$this->methodCalls[] = new \PHPStan\Node\Method\MethodCall($node->getOriginalNode(), $scope);
return;
}
if ($node instanceof Array_ && count($node->items) === 2) {
$this->methodCalls[] = new \PHPStan\Node\Method\MethodCall($node, $scope);
return;
}
if ($node instanceof Expr\ClassConstFetch) {
$this->constantFetches[] = new ClassConstantFetch($node, $scope);
return;
}
if ($node instanceof PropertyAssignNode) {
$this->propertyUsages[] = new PropertyWrite($node->getPropertyFetch(), $scope);
return;
}
if (!$node instanceof Expr) {
return;
}
if ($node instanceof Expr\AssignOp\Coalesce) {
$this->gatherNodes($node->var, $scope);
return;
}
if ($node instanceof Expr\AssignRef) {
if (!$node->expr instanceof PropertyFetch && !$node->expr instanceof StaticPropertyFetch) {
$this->gatherNodes($node->expr, $scope);
return;
}
$this->propertyUsages[] = new PropertyRead($node->expr, $scope);
$this->propertyUsages[] = new PropertyWrite($node->expr, $scope);
return;
}
if ($node instanceof Node\Scalar\EncapsedStringPart) {
return;
}
if ($node instanceof FunctionCallableNode) {
$node = $node->getOriginalNode();
} elseif ($node instanceof InstantiationCallableNode) {
$node = $node->getOriginalNode();
}
$inAssign = $scope->isInExpressionAssign($node);
if ($inAssign) {
return;
}
while ($node instanceof ArrayDimFetch) {
$node = $node->var;
}
if (!$node instanceof PropertyFetch && !$node instanceof StaticPropertyFetch) {
return;
}
$this->propertyUsages[] = new PropertyRead($node, $scope);
}
}