1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Reflection\ClassUseStatements; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class UsesBlockParser |
7
|
|
|
* @package Reflection |
8
|
|
|
*/ |
9
|
|
|
class UsesBlockParser { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @const string |
13
|
|
|
*/ |
14
|
|
|
const CLASS_STATEMENT_TYPE = 'class'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @const string |
18
|
|
|
*/ |
19
|
|
|
const ALIAS_STATEMENT_TYPE = 'alias'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $usesBlock; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var boolean |
28
|
|
|
*/ |
29
|
|
|
private $isUseStatementBuilding = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $currentClassStatement = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $currentAliasStatement = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $currentStatementType = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* UsesBlockParser constructor. |
48
|
|
|
* @param string $usesBlock |
49
|
|
|
*/ |
50
|
1 |
|
public function __construct(string $usesBlock) { |
51
|
1 |
|
$this->setUsesBlock($usesBlock); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
1 |
|
public function getUsesBlock(): string { |
58
|
1 |
|
return $this->usesBlock; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $usesBlock |
63
|
|
|
* @return UsesBlockParser |
64
|
|
|
*/ |
65
|
1 |
|
public function setUsesBlock(string $usesBlock): UsesBlockParser { |
66
|
1 |
|
$this->usesBlock = $usesBlock; |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
1 |
|
protected function isUseStatementBuilding(): bool { |
75
|
1 |
|
return $this->isUseStatementBuilding; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return UsesBlockParser |
80
|
|
|
*/ |
81
|
1 |
|
protected function setUseStatementIsBuilding(): UsesBlockParser { |
82
|
1 |
|
$this->isUseStatementBuilding = true; |
83
|
|
|
|
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return UsesBlockParser |
89
|
|
|
*/ |
90
|
1 |
|
protected function setUseStatementIsNotBuilding(): UsesBlockParser { |
91
|
1 |
|
$this->isUseStatementBuilding = false; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
protected function getCurrentClassStatement(): string { |
100
|
1 |
|
return $this->currentClassStatement; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $classStatement |
105
|
|
|
* @return UsesBlockParser |
106
|
|
|
*/ |
107
|
1 |
|
protected function setCurrentClassStatement(string $classStatement): UsesBlockParser { |
108
|
1 |
|
$this->currentClassStatement .= $classStatement; |
109
|
|
|
|
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return UsesBlockParser |
115
|
|
|
*/ |
116
|
1 |
|
protected function clearCurrentClassStatement(): UsesBlockParser { |
117
|
1 |
|
$this->currentClassStatement = ''; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
1 |
|
protected function getCurrentAliasStatement(): string { |
126
|
1 |
|
return $this->currentAliasStatement; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $aliasStatement |
131
|
|
|
* @return UsesBlockParser |
132
|
|
|
*/ |
133
|
1 |
|
protected function setCurrentAliasStatement(string $aliasStatement): UsesBlockParser { |
134
|
1 |
|
$this->currentAliasStatement = $aliasStatement; |
135
|
|
|
|
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
1 |
|
protected function getCurrentStatementType(): string { |
143
|
1 |
|
return $this->currentStatementType; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $currentStatementType |
148
|
|
|
* @return UsesBlockParser |
149
|
|
|
*/ |
150
|
1 |
|
protected function setCurrentStatementType(string $currentStatementType): UsesBlockParser { |
151
|
1 |
|
$this->currentStatementType = $currentStatementType; |
152
|
|
|
|
153
|
1 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return UseStatements |
158
|
|
|
*/ |
159
|
1 |
|
public function getUseStatements(): UseStatements { |
160
|
1 |
|
$useStatements = new UseStatements(); |
161
|
|
|
|
162
|
1 |
|
foreach ($this->getTokens() as $token) { |
163
|
1 |
|
if (is_array($token)) { |
164
|
1 |
|
if ($token[0] === T_USE) { |
165
|
1 |
|
$this->setUseStatementIsBuilding() |
166
|
1 |
|
->setCurrentStatementType(self::CLASS_STATEMENT_TYPE); |
167
|
|
|
|
168
|
1 |
|
continue; |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
if ($token[0] === T_AS) { |
172
|
1 |
|
$this->setCurrentStatementType(self::ALIAS_STATEMENT_TYPE); |
173
|
|
|
|
174
|
1 |
|
continue; |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
if ($this->isUseStatementBuilding() && $this->getCurrentStatementType()) { |
178
|
1 |
|
switch ($token[0]) { |
179
|
1 |
|
case T_NS_SEPARATOR: |
180
|
1 |
|
case T_STRING: |
181
|
1 |
|
$this->setCurrentStatement($token[1]); |
182
|
|
|
|
183
|
1 |
|
break; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} else { |
187
|
1 |
|
if (($token === ';' || $token === ',') && $this->isUseStatementBuilding()) { |
188
|
1 |
|
$useStatements->add(new UseStatement( |
189
|
1 |
|
$this->getCurrentClassStatement(), |
190
|
1 |
|
$this->getCurrentAliasStatement() |
191
|
|
|
)); |
192
|
|
|
|
193
|
1 |
|
$this->clearCurrentStatements(); |
194
|
|
|
|
195
|
1 |
|
if ($token === ';') { |
196
|
1 |
|
$this->setUseStatementIsNotBuilding(); |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
if ($token === ',') { |
200
|
1 |
|
$this->setCurrentStatementType(self::CLASS_STATEMENT_TYPE); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
return $useStatements; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
1 |
|
private function getTokens(): array { |
213
|
1 |
|
return token_get_all($this->getUsesBlock()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $value |
218
|
|
|
* @return UsesBlockParser |
219
|
|
|
*/ |
220
|
1 |
|
private function setCurrentStatement(string $value): UsesBlockParser { |
221
|
1 |
|
$type = $this->getCurrentStatementType(); |
222
|
|
|
|
223
|
1 |
|
$setter = 'setCurrent' . ucfirst($type) . 'Statement'; |
224
|
|
|
|
225
|
1 |
|
return $this->$setter($value); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return UsesBlockParser |
230
|
|
|
*/ |
231
|
1 |
|
protected function clearCurrentStatements(): UsesBlockParser { |
232
|
1 |
|
return $this->clearCurrentClassStatement() |
233
|
1 |
|
->setCurrentAliasStatement(''); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |