1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA; |
7
|
|
|
|
8
|
|
|
use PHPSA\Compiler\Expression; |
9
|
|
|
use PHPSA\Compiler\GlobalVariable; |
10
|
|
|
use PHPSA\Definition\AbstractDefinition; |
11
|
|
|
use PHPSA\Definition\ParentDefinition; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Webiny\Component\EventManager\EventManager; |
14
|
|
|
|
15
|
|
|
class Context |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* For FunctionDefinition it's null, use scopePointer |
19
|
|
|
* |
20
|
|
|
* @var ParentDefinition|null |
21
|
|
|
*/ |
22
|
|
|
public $scope; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AliasManager |
26
|
|
|
*/ |
27
|
|
|
public $aliasManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Application |
31
|
|
|
*/ |
32
|
|
|
public $application; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string|integer |
36
|
|
|
*/ |
37
|
|
|
public $currentBranch; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var OutputInterface |
41
|
|
|
*/ |
42
|
|
|
public $output; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Variable[] |
46
|
|
|
*/ |
47
|
|
|
protected $symbols = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ScopePointer|null |
51
|
|
|
*/ |
52
|
|
|
public $scopePointer; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $filepath; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var EventManager |
61
|
|
|
*/ |
62
|
|
|
protected $eventManager; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Construct our Context with all needed information |
66
|
|
|
* |
67
|
|
|
* @param OutputInterface $output |
68
|
|
|
* @param Application $application |
69
|
|
|
* @param EventManager $eventManager |
70
|
|
|
*/ |
71
|
872 |
|
public function __construct(OutputInterface $output, Application $application, EventManager $eventManager) |
72
|
|
|
{ |
73
|
872 |
|
$this->output = $output; |
74
|
872 |
|
$this->application = $application; |
75
|
|
|
|
76
|
872 |
|
$this->initGlobals(); |
77
|
872 |
|
$this->eventManager = $eventManager; |
78
|
872 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Expression |
82
|
|
|
*/ |
83
|
870 |
|
public function getExpressionCompiler() |
84
|
|
|
{ |
85
|
870 |
|
return new Expression($this, $this->eventManager); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds all global variables to the context. |
90
|
|
|
*/ |
91
|
872 |
|
public function initGlobals() |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* http://php.net/manual/language.variables.superglobals.php |
95
|
|
|
*/ |
96
|
872 |
|
$this->addVariable(new GlobalVariable('GLOBALS', [], CompiledExpression::ARR)); |
97
|
872 |
|
$this->addVariable(new GlobalVariable('_SERVER', [], CompiledExpression::ARR)); |
98
|
872 |
|
$this->addVariable(new GlobalVariable('_GET', [], CompiledExpression::ARR)); |
99
|
872 |
|
$this->addVariable(new GlobalVariable('_POST', [], CompiledExpression::ARR)); |
100
|
872 |
|
$this->addVariable(new GlobalVariable('_FILES', [], CompiledExpression::ARR)); |
101
|
872 |
|
$this->addVariable(new GlobalVariable('_COOKIE', [], CompiledExpression::ARR)); |
102
|
872 |
|
$this->addVariable(new GlobalVariable('_SESSION', [], CompiledExpression::ARR)); |
103
|
872 |
|
$this->addVariable(new GlobalVariable('_REQUEST', [], CompiledExpression::ARR)); |
104
|
872 |
|
$this->addVariable(new GlobalVariable('_ENV', [], CompiledExpression::ARR)); |
105
|
872 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates a variable from a symbol and adds it to the context. |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
* @return Variable |
112
|
|
|
*/ |
113
|
|
|
public function addSymbol($name) |
114
|
|
|
{ |
115
|
|
|
$variable = new Variable($name); |
116
|
|
|
$this->symbols[$name] = $variable; |
117
|
|
|
|
118
|
|
|
return $variable; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Adds a variable to the context. |
123
|
|
|
* |
124
|
|
|
* @param Variable $variable |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
872 |
|
public function addVariable(Variable $variable) |
128
|
|
|
{ |
129
|
872 |
|
$this->symbols[$variable->getName()] = $variable; |
130
|
|
|
|
131
|
872 |
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Resets context to beginning stage. |
136
|
|
|
*/ |
137
|
46 |
|
public function clear() |
138
|
|
|
{ |
139
|
46 |
|
$this->symbols = []; |
140
|
46 |
|
$this->scope = null; |
141
|
46 |
|
$this->scopePointer = null; |
142
|
46 |
|
$this->currentBranch = null; |
143
|
46 |
|
$this->aliasManager = null; |
144
|
|
|
|
145
|
46 |
|
$this->initGlobals(); |
146
|
46 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Clears only all symbols. |
150
|
|
|
*/ |
151
|
43 |
|
public function clearSymbols() |
152
|
|
|
{ |
153
|
43 |
|
unset($this->symbols); |
154
|
43 |
|
$this->symbols = []; |
155
|
43 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns a variable if it exists. |
159
|
|
|
* |
160
|
|
|
* @param $name |
161
|
|
|
* @return Variable|null |
162
|
|
|
*/ |
163
|
87 |
|
public function getSymbol($name) |
164
|
|
|
{ |
165
|
87 |
|
return isset($this->symbols[$name]) ? $this->symbols[$name] : null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Creates a warning message. |
170
|
|
|
* |
171
|
|
|
* @param string $type |
172
|
|
|
* @param string $message |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
13 |
|
public function warning($type, $message) |
176
|
|
|
{ |
177
|
13 |
|
$this->output |
178
|
13 |
|
->writeln('<comment>Warning: ' . $message . ' in ' . $this->filepath . ' [' . $type . ']</comment>'); |
179
|
|
|
|
180
|
13 |
|
$this->output->writeln(''); |
181
|
|
|
|
182
|
13 |
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Creates a notice message. |
187
|
|
|
* |
188
|
|
|
* @param string $type |
189
|
|
|
* @param string $message |
190
|
|
|
* @param \PhpParser\NodeAbstract $expr |
191
|
|
|
* @param int $status |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
46 |
|
public function notice($type, $message, \PhpParser\NodeAbstract $expr, $status = Check::CHECK_SAFE) |
|
|
|
|
195
|
|
|
{ |
196
|
46 |
|
$filepath = $this->filepath; |
197
|
46 |
|
$code = file($filepath); |
198
|
|
|
|
199
|
46 |
|
$this->output->writeln('<comment>Notice: ' . $message . " in {$filepath} on {$expr->getLine()} [{$type}]</comment>"); |
200
|
46 |
|
$this->output->writeln(''); |
201
|
|
|
|
202
|
46 |
|
if ($this->application->getConfiguration()->valueIsTrue('blame')) { |
203
|
|
|
exec("git blame --show-email -L {$expr->getLine()},{$expr->getLine()} " . $filepath, $result); |
204
|
|
|
if ($result && isset($result[0])) { |
205
|
|
|
$result[0] = trim($result[0]); |
206
|
|
|
|
207
|
|
|
$this->output->writeln("<comment>\t {$result[0]}</comment>"); |
208
|
|
|
} |
209
|
|
|
} else { |
210
|
46 |
|
$code = trim($code[$expr->getLine() - 1]); |
211
|
46 |
|
$this->output->writeln("<comment>\t {$code} </comment>"); |
212
|
|
|
} |
213
|
|
|
|
214
|
46 |
|
$this->output->writeln(''); |
215
|
|
|
|
216
|
46 |
|
$issueCollector = $this->application->getIssuesCollector(); |
217
|
46 |
|
$issueCollector->addIssue( |
218
|
46 |
|
new Issue( |
219
|
46 |
|
$type, |
220
|
46 |
|
$message, |
221
|
46 |
|
new IssueLocation( |
222
|
46 |
|
$this->filepath, |
223
|
46 |
|
$expr->getLine() - 1 |
224
|
46 |
|
) |
225
|
46 |
|
) |
226
|
46 |
|
); |
227
|
|
|
|
228
|
46 |
|
return true; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Creates a syntax error message. |
233
|
|
|
* |
234
|
|
|
* @param \PhpParser\Error $exception |
235
|
|
|
* @param string $filepath |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public function syntaxError(\PhpParser\Error $exception, $filepath) |
239
|
|
|
{ |
240
|
|
|
$code = file($filepath); |
241
|
|
|
|
242
|
|
|
$this->output->writeln('<error>Syntax error: ' . $exception->getMessage() . " in {$filepath} </error>"); |
243
|
|
|
$this->output->writeln(''); |
244
|
|
|
|
245
|
|
|
$issueCollector = $this->application->getIssuesCollector(); |
246
|
|
|
$issueCollector->addIssue( |
247
|
|
|
new Issue( |
248
|
|
|
'syntax-error', |
249
|
|
|
'syntax-error', |
250
|
|
|
new IssueLocation( |
251
|
|
|
$filepath, |
252
|
|
|
$exception->getStartLine() - 2 |
253
|
|
|
) |
254
|
|
|
) |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$code = trim($code[$exception->getStartLine()-2]); |
258
|
|
|
$this->output->writeln("<comment>\t {$code} </comment>"); |
259
|
|
|
|
260
|
|
|
return true; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Returns an array of all variables. |
265
|
|
|
* |
266
|
|
|
* @return Variable[] |
267
|
|
|
*/ |
268
|
43 |
|
public function getSymbols() |
269
|
|
|
{ |
270
|
43 |
|
return $this->symbols; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param AbstractDefinition $scope |
275
|
|
|
*/ |
276
|
872 |
|
public function setScope(AbstractDefinition $scope = null) |
277
|
|
|
{ |
278
|
872 |
|
$this->scope = $scope; |
|
|
|
|
279
|
872 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Creates a debug message. |
283
|
|
|
*/ |
284
|
48 |
|
public function debug($message, \PhpParser\Node $expr = null) |
285
|
|
|
{ |
286
|
48 |
|
if ($this->output->isDebug()) { |
287
|
|
|
$this->output->writeln('[DEBUG] ' . $message); |
288
|
|
|
$this->output->write($this->filepath); |
289
|
|
|
|
290
|
|
|
if ($expr) { |
291
|
|
|
$this->output->write(':' . $expr->getLine()); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$this->output->writeln(''); |
295
|
|
|
} |
296
|
48 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
public function getFilepath() |
302
|
|
|
{ |
303
|
|
|
return $this->filepath; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param string $filepath |
308
|
|
|
*/ |
309
|
46 |
|
public function setFilepath($filepath) |
310
|
|
|
{ |
311
|
46 |
|
$this->filepath = $filepath; |
312
|
46 |
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return integer |
316
|
|
|
*/ |
317
|
40 |
|
public function getCurrentBranch() |
318
|
|
|
{ |
319
|
40 |
|
return $this->currentBranch; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param int|string $currentBranch |
324
|
|
|
*/ |
325
|
10 |
|
public function setCurrentBranch($currentBranch) |
326
|
|
|
{ |
327
|
10 |
|
$this->currentBranch = $currentBranch; |
328
|
10 |
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Updates all references on the given variable. |
332
|
|
|
* |
333
|
|
|
* @param Variable $variable |
334
|
|
|
* @param $type |
335
|
|
|
* @param $value |
336
|
|
|
*/ |
337
|
6 |
|
public function modifyReferencedVariables(Variable $variable, $type, $value) |
338
|
|
|
{ |
339
|
6 |
|
foreach ($this->symbols as $symbol) { |
340
|
6 |
|
$referencedTo = $symbol->getReferencedTo(); |
|
|
|
|
341
|
6 |
|
if ($referencedTo) { |
342
|
2 |
|
if ($referencedTo === $variable) { |
343
|
2 |
|
$symbol->modify($type, $value); |
344
|
2 |
|
} |
345
|
2 |
|
} |
346
|
6 |
|
} |
347
|
6 |
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return EventManager |
351
|
|
|
*/ |
352
|
59 |
|
public function getEventManager() |
353
|
|
|
{ |
354
|
59 |
|
return $this->eventManager; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.