1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Classifier; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This file was copied from {@link https://github.com/spiral/tokenizer}. |
9
|
|
|
* |
10
|
|
|
* @internal |
11
|
|
|
* |
12
|
|
|
* @psalm-type TPosition = list{int, int} |
13
|
|
|
*/ |
14
|
|
|
final class ReflectionFile |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Namespace separator. |
18
|
|
|
*/ |
19
|
|
|
public const NS_SEPARATOR = '\\'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Opening and closing token ids. |
23
|
|
|
*/ |
24
|
|
|
public const O_TOKEN = 0; |
25
|
|
|
public const C_TOKEN = 1; |
26
|
|
|
|
27
|
|
|
public const T_OPEN_CURLY_BRACES = 123; |
28
|
|
|
public const T_CLOSE_CURLY_BRACES = 125; |
29
|
|
|
public const T_SEMICOLON = 59; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set of tokens required to detect classes, traits, interfaces declarations. We |
33
|
|
|
* don't need any other token for that. |
34
|
|
|
*/ |
35
|
|
|
private const TOKENS = [ |
36
|
|
|
self::T_OPEN_CURLY_BRACES, |
37
|
|
|
self::T_CLOSE_CURLY_BRACES, |
38
|
|
|
self::T_SEMICOLON, |
39
|
|
|
T_PAAMAYIM_NEKUDOTAYIM, |
40
|
|
|
T_NAMESPACE, |
41
|
|
|
T_STRING, |
42
|
|
|
T_CLASS, |
43
|
|
|
T_INTERFACE, |
44
|
|
|
T_TRAIT, |
45
|
|
|
T_ENUM, |
|
|
|
|
46
|
|
|
T_NS_SEPARATOR, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Parsed tokens array. |
51
|
|
|
* |
52
|
|
|
* @var array<int, \PhpToken> |
53
|
|
|
*/ |
54
|
|
|
private array $tokens; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Total tokens count. |
58
|
|
|
*/ |
59
|
|
|
private int $countTokens; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Namespaces used in file and their token positions. |
63
|
|
|
* |
64
|
|
|
* @psalm-var array<string, TPosition> |
65
|
|
|
*/ |
66
|
|
|
private array $namespaces = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Declarations of classes, interfaces and traits. |
70
|
|
|
* |
71
|
|
|
* @psalm-var array<class-string|trait-string, TPosition> |
72
|
|
|
*/ |
73
|
|
|
private array $declarations = []; |
74
|
|
|
|
75
|
25 |
|
public function __construct( |
76
|
|
|
private string $filename |
77
|
|
|
) { |
78
|
25 |
|
$this->tokens = \PhpToken::tokenize(file_get_contents($this->filename)); |
79
|
25 |
|
$this->countTokens = \count($this->tokens); |
80
|
|
|
|
81
|
|
|
//Looking for declarations |
82
|
25 |
|
$this->locateDeclarations(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* List of declarations names |
87
|
|
|
* |
88
|
|
|
* @return array<class-string|trait-string> |
|
|
|
|
89
|
|
|
*/ |
90
|
25 |
|
public function getDeclarations(): array |
91
|
|
|
{ |
92
|
25 |
|
return \array_keys($this->declarations); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Locate every class, interface, trait or enum definition. |
97
|
|
|
*/ |
98
|
25 |
|
private function locateDeclarations(): void |
99
|
|
|
{ |
100
|
25 |
|
foreach ($this->tokens as $tokenIndex => $token) { |
101
|
25 |
|
if (!\in_array($token->id, self::TOKENS, true)) { |
102
|
25 |
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
25 |
|
switch ($token->id) { |
106
|
25 |
|
case T_NAMESPACE: |
107
|
24 |
|
$this->registerNamespace($tokenIndex); |
108
|
24 |
|
break; |
109
|
|
|
|
110
|
25 |
|
case T_CLASS: |
111
|
25 |
|
case T_TRAIT: |
112
|
25 |
|
case T_INTERFACE: |
113
|
25 |
|
case T_ENUM: |
|
|
|
|
114
|
25 |
|
if ($this->isClassNameConst($tokenIndex)) { |
115
|
|
|
// PHP5.5 ClassName::class constant |
116
|
13 |
|
continue 2; |
117
|
|
|
} |
118
|
|
|
|
119
|
25 |
|
if ($this->isAnonymousClass($tokenIndex)) { |
120
|
|
|
// PHP7.0 Anonymous classes new class ('foo', 'bar') |
121
|
14 |
|
continue 2; |
122
|
|
|
} |
123
|
|
|
|
124
|
25 |
|
if (!$this->isCorrectDeclaration($tokenIndex)) { |
125
|
|
|
// PHP8.0 Named parameters ->foo(class: 'bar') |
126
|
14 |
|
continue 2; |
127
|
|
|
} |
128
|
|
|
|
129
|
25 |
|
$this->registerDeclaration($tokenIndex); |
130
|
25 |
|
break; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//Dropping empty namespace |
135
|
25 |
|
if (isset($this->namespaces[''])) { |
136
|
14 |
|
$this->namespaces['\\'] = $this->namespaces['']; |
137
|
14 |
|
unset($this->namespaces['']); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Handle namespace declaration. |
143
|
|
|
*/ |
144
|
24 |
|
private function registerNamespace(int $tokenIndex): void |
145
|
|
|
{ |
146
|
24 |
|
$namespace = ''; |
147
|
24 |
|
$localIndex = $tokenIndex + 1; |
148
|
|
|
|
149
|
|
|
do { |
150
|
24 |
|
$token = $this->tokens[$localIndex++]; |
151
|
24 |
|
$namespace .= $token->text; |
152
|
|
|
} while ( |
153
|
24 |
|
isset($this->tokens[$localIndex]) |
154
|
24 |
|
&& $this->tokens[$localIndex]->text !== '{' |
155
|
24 |
|
&& $this->tokens[$localIndex]->text !== ';' |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
//Whitespaces |
159
|
24 |
|
$namespace = \trim($namespace); |
160
|
|
|
|
161
|
24 |
|
if ($this->tokens[$localIndex]->text === ';') { |
162
|
23 |
|
$endingIndex = \count($this->tokens) - 1; |
163
|
|
|
} else { |
164
|
14 |
|
$endingIndex = $this->endingToken($tokenIndex); |
165
|
|
|
} |
166
|
|
|
|
167
|
24 |
|
$this->namespaces[$namespace] = [ |
168
|
24 |
|
self::O_TOKEN => $tokenIndex, |
169
|
24 |
|
self::C_TOKEN => $endingIndex, |
170
|
24 |
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Handle declaration of class, trait of interface. Declaration will be stored under it's token |
175
|
|
|
* type in declarations array. |
176
|
|
|
*/ |
177
|
25 |
|
private function registerDeclaration(int $tokenIndex): void |
178
|
|
|
{ |
179
|
25 |
|
$localIndex = $tokenIndex + 1; |
180
|
25 |
|
while ($this->tokens[$localIndex]->id !== T_STRING) { |
181
|
25 |
|
++$localIndex; |
182
|
|
|
} |
183
|
|
|
|
184
|
25 |
|
$name = $this->tokens[$localIndex]->text; |
185
|
25 |
|
if (!empty($namespace = $this->activeNamespace($tokenIndex))) { |
186
|
24 |
|
$name = $namespace . self::NS_SEPARATOR . $name; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** @var class-string|trait-string $name */ |
190
|
25 |
|
$this->declarations[$name] = [ |
191
|
25 |
|
self::O_TOKEN => $tokenIndex, |
192
|
25 |
|
self::C_TOKEN => $this->endingToken($tokenIndex), |
193
|
25 |
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Check if token ID represents `ClassName::class` constant statement. |
198
|
|
|
*/ |
199
|
25 |
|
private function isClassNameConst(int $tokenIndex): bool |
200
|
|
|
{ |
201
|
25 |
|
return $this->tokens[$tokenIndex]->id === T_CLASS |
202
|
25 |
|
&& isset($this->tokens[$tokenIndex - 1]) |
203
|
25 |
|
&& $this->tokens[$tokenIndex - 1]->id === T_PAAMAYIM_NEKUDOTAYIM; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Check if token ID represents anonymous class creation, e.g. `new class ('foo', 'bar')`. |
208
|
|
|
*/ |
209
|
25 |
|
private function isAnonymousClass(int $tokenIndex): bool |
210
|
|
|
{ |
211
|
25 |
|
return $this->tokens[$tokenIndex]->id === T_CLASS |
212
|
25 |
|
&& isset($this->tokens[$tokenIndex - 2]) |
213
|
25 |
|
&& $this->tokens[$tokenIndex - 2]->id === T_NEW; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Check if token ID represents named parameter with name `class`, e.g. `foo(class: SomeClass::name)`. |
218
|
|
|
*/ |
219
|
25 |
|
private function isCorrectDeclaration(int $tokenIndex): bool |
220
|
|
|
{ |
221
|
25 |
|
return \in_array($this->tokens[$tokenIndex]->id, [T_CLASS, T_TRAIT, T_INTERFACE, T_ENUM], true) |
|
|
|
|
222
|
25 |
|
&& isset($this->tokens[$tokenIndex + 2]) |
223
|
25 |
|
&& $this->tokens[$tokenIndex + 1]->id === T_WHITESPACE |
224
|
25 |
|
&& $this->tokens[$tokenIndex + 2]->id === T_STRING; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get namespace name active at specified token position. |
229
|
|
|
* |
230
|
|
|
* @return array-key |
|
|
|
|
231
|
|
|
*/ |
232
|
25 |
|
private function activeNamespace(int $tokenIndex): string |
233
|
|
|
{ |
234
|
25 |
|
foreach ($this->namespaces as $namespace => $position) { |
235
|
24 |
|
if ($tokenIndex >= $position[self::O_TOKEN] && $tokenIndex <= $position[self::C_TOKEN]) { |
236
|
24 |
|
return $namespace; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
//Seems like no namespace declaration |
241
|
14 |
|
$this->namespaces[''] = [ |
242
|
14 |
|
self::O_TOKEN => 0, |
243
|
14 |
|
self::C_TOKEN => \count($this->tokens), |
244
|
14 |
|
]; |
245
|
|
|
|
246
|
14 |
|
return ''; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Find token index of ending brace. |
251
|
|
|
*/ |
252
|
25 |
|
private function endingToken(int $tokenIndex): int |
253
|
|
|
{ |
254
|
25 |
|
$level = 0; |
255
|
25 |
|
$hasOpen = false; |
256
|
25 |
|
for ($localIndex = $tokenIndex; $localIndex < $this->countTokens; ++$localIndex) { |
257
|
25 |
|
$token = $this->tokens[$localIndex]; |
258
|
25 |
|
if ($token->text === '{') { |
259
|
25 |
|
++$level; |
260
|
25 |
|
$hasOpen = true; |
261
|
25 |
|
continue; |
262
|
|
|
} |
263
|
|
|
|
264
|
25 |
|
if ($token->text === '}') { |
265
|
25 |
|
--$level; |
266
|
|
|
} |
267
|
|
|
|
268
|
25 |
|
if ($hasOpen && $level === 0) { |
269
|
25 |
|
break; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
25 |
|
return $localIndex; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|