|
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
|
387 |
|
public function __construct(OutputInterface $output, Application $application, EventManager $eventManager) |
|
72
|
|
|
{ |
|
73
|
387 |
|
$this->output = $output; |
|
74
|
387 |
|
$this->application = $application; |
|
75
|
|
|
|
|
76
|
387 |
|
$this->initGlobals(); |
|
77
|
387 |
|
$this->eventManager = $eventManager; |
|
78
|
387 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return Expression |
|
82
|
|
|
*/ |
|
83
|
381 |
|
public function getExpressionCompiler() |
|
84
|
|
|
{ |
|
85
|
381 |
|
return new Expression($this, $this->eventManager); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
387 |
|
public function initGlobals() |
|
89
|
|
|
{ |
|
90
|
|
|
/** |
|
91
|
|
|
* http://php.net/manual/language.variables.superglobals.php |
|
92
|
|
|
*/ |
|
93
|
387 |
|
$this->addVariable(new GlobalVariable('GLOBALS', [], CompiledExpression::ARR)); |
|
94
|
387 |
|
$this->addVariable(new GlobalVariable('_SERVER', [], CompiledExpression::ARR)); |
|
95
|
387 |
|
$this->addVariable(new GlobalVariable('_GET', [], CompiledExpression::ARR)); |
|
96
|
387 |
|
$this->addVariable(new GlobalVariable('_POST', [], CompiledExpression::ARR)); |
|
97
|
387 |
|
$this->addVariable(new GlobalVariable('_FILES', [], CompiledExpression::ARR)); |
|
98
|
387 |
|
$this->addVariable(new GlobalVariable('_COOKIE', [], CompiledExpression::ARR)); |
|
99
|
387 |
|
$this->addVariable(new GlobalVariable('_SESSION', [], CompiledExpression::ARR)); |
|
100
|
387 |
|
$this->addVariable(new GlobalVariable('_REQUEST', [], CompiledExpression::ARR)); |
|
101
|
387 |
|
$this->addVariable(new GlobalVariable('_ENV', [], CompiledExpression::ARR)); |
|
102
|
387 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @return Variable |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function addSymbol($name) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$variable = new Variable($name); |
|
111
|
1 |
|
$this->symbols[$name] = $variable; |
|
112
|
|
|
|
|
113
|
1 |
|
return $variable; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Variable $variable |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
387 |
|
public function addVariable(Variable $variable) |
|
121
|
|
|
{ |
|
122
|
387 |
|
$this->symbols[$variable->getName()] = $variable; |
|
123
|
|
|
|
|
124
|
387 |
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Clear prevent context |
|
129
|
|
|
*/ |
|
130
|
14 |
|
public function clear() |
|
131
|
|
|
{ |
|
132
|
14 |
|
$this->symbols = []; |
|
133
|
14 |
|
$this->scope = null; |
|
134
|
14 |
|
$this->scopePointer = null; |
|
135
|
14 |
|
$this->currentBranch = null; |
|
136
|
14 |
|
$this->aliasManager = null; |
|
137
|
|
|
|
|
138
|
14 |
|
$this->initGlobals(); |
|
139
|
14 |
|
} |
|
140
|
|
|
|
|
141
|
14 |
|
public function clearSymbols() |
|
142
|
|
|
{ |
|
143
|
14 |
|
unset($this->symbols); |
|
144
|
14 |
|
$this->symbols = []; |
|
145
|
14 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param $name |
|
149
|
|
|
* @return Variable|null |
|
150
|
|
|
*/ |
|
151
|
16 |
|
public function getSymbol($name) |
|
152
|
|
|
{ |
|
153
|
16 |
|
return isset($this->symbols[$name]) ? $this->symbols[$name] : null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $type |
|
158
|
|
|
* @param string $message |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function warning($type, $message) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->output |
|
164
|
1 |
|
->writeln('<comment>Warning: ' . $message . ' in ' . $this->filepath . ' [' . $type . ']</comment>'); |
|
165
|
|
|
|
|
166
|
1 |
|
$this->output->writeln(''); |
|
167
|
|
|
|
|
168
|
1 |
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $type |
|
173
|
|
|
* @param string $message |
|
174
|
|
|
* @param \PhpParser\NodeAbstract $expr |
|
175
|
|
|
* @param int $status |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
14 |
|
public function notice($type, $message, \PhpParser\NodeAbstract $expr, $status = Check::CHECK_SAFE) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
14 |
|
$filepath = $this->filepath; |
|
181
|
14 |
|
$code = file($filepath); |
|
182
|
|
|
|
|
183
|
14 |
|
$this->output->writeln('<comment>Notice: ' . $message . " in {$filepath} on {$expr->getLine()} [{$type}]</comment>"); |
|
184
|
14 |
|
$this->output->writeln(''); |
|
185
|
|
|
|
|
186
|
14 |
|
if ($this->application->getConfiguration()->valueIsTrue('blame')) { |
|
187
|
|
|
exec("git blame --show-email -L {$expr->getLine()},{$expr->getLine()} " . $filepath, $result); |
|
188
|
|
|
if ($result && isset($result[0])) { |
|
189
|
|
|
$result[0] = trim($result[0]); |
|
190
|
|
|
|
|
191
|
|
|
$this->output->writeln("<comment>\t {$result[0]}</comment>"); |
|
192
|
|
|
} |
|
193
|
|
|
} else { |
|
194
|
14 |
|
$code = trim($code[$expr->getLine() - 1]); |
|
195
|
14 |
|
$this->output->writeln("<comment>\t {$code} </comment>"); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
14 |
|
$this->output->writeln(''); |
|
199
|
|
|
|
|
200
|
14 |
|
$this->application->getIssuesCollector() |
|
201
|
14 |
|
->addIssue($type, $message, basename($this->filepath), $expr->getLine() - 1); |
|
202
|
|
|
|
|
203
|
14 |
|
return true; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param \PhpParser\Error $exception |
|
208
|
|
|
* @param string $filepath |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
|
|
public function syntaxError(\PhpParser\Error $exception, $filepath) |
|
212
|
|
|
{ |
|
213
|
|
|
$code = file($filepath); |
|
214
|
|
|
|
|
215
|
|
|
$this->output->writeln('<error>Syntax error: ' . $exception->getMessage() . " in {$filepath} </error>"); |
|
216
|
|
|
$this->output->writeln(''); |
|
217
|
|
|
|
|
218
|
|
|
$this->application->getIssuesCollector() |
|
219
|
|
|
->addIssue('syntax-error', 'syntax-error', $filepath, $exception->getStartLine() - 2); |
|
220
|
|
|
|
|
221
|
|
|
$code = trim($code[$exception->getStartLine()-2]); |
|
222
|
|
|
$this->output->writeln("<comment>\t {$code} </comment>"); |
|
223
|
|
|
|
|
224
|
|
|
return true; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return Variable[] |
|
229
|
|
|
*/ |
|
230
|
14 |
|
public function getSymbols() |
|
231
|
|
|
{ |
|
232
|
14 |
|
return $this->symbols; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param AbstractDefinition $scope |
|
237
|
|
|
*/ |
|
238
|
387 |
|
public function setScope(AbstractDefinition $scope = null) |
|
239
|
|
|
{ |
|
240
|
387 |
|
$this->scope = $scope; |
|
|
|
|
|
|
241
|
387 |
|
} |
|
242
|
|
|
|
|
243
|
14 |
|
public function debug($message, \PhpParser\Node $expr = null) |
|
244
|
|
|
{ |
|
245
|
14 |
|
if ($this->output->isDebug()) { |
|
246
|
|
|
$this->output->writeln('[DEBUG] ' . $message); |
|
247
|
|
|
$this->output->write($this->filepath); |
|
248
|
|
|
|
|
249
|
|
|
if ($expr) { |
|
250
|
|
|
$this->output->write(':' . $expr->getLine()); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$this->output->writeln(''); |
|
254
|
|
|
} |
|
255
|
14 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return string |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getFilepath() |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->filepath; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param string $filepath |
|
267
|
|
|
*/ |
|
268
|
14 |
|
public function setFilepath($filepath) |
|
269
|
|
|
{ |
|
270
|
14 |
|
$this->filepath = $filepath; |
|
271
|
14 |
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @return integer |
|
275
|
|
|
*/ |
|
276
|
7 |
|
public function getCurrentBranch() |
|
277
|
|
|
{ |
|
278
|
7 |
|
return $this->currentBranch; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param int|string $currentBranch |
|
283
|
|
|
*/ |
|
284
|
|
|
public function setCurrentBranch($currentBranch) |
|
285
|
|
|
{ |
|
286
|
|
|
$this->currentBranch = $currentBranch; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param Variable $variable |
|
291
|
|
|
* @param $type |
|
292
|
|
|
* @param $value |
|
293
|
|
|
*/ |
|
294
|
3 |
|
public function modifyReferencedVariables(Variable $variable, $type, $value) |
|
295
|
|
|
{ |
|
296
|
3 |
|
foreach ($this->symbols as $symbol) { |
|
297
|
3 |
|
$referencedTo = $symbol->getReferencedTo(); |
|
|
|
|
|
|
298
|
3 |
|
if ($referencedTo) { |
|
299
|
2 |
|
if ($referencedTo === $variable) { |
|
300
|
2 |
|
$symbol->modify($type, $value); |
|
301
|
2 |
|
} |
|
302
|
2 |
|
} |
|
303
|
3 |
|
} |
|
304
|
3 |
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @return EventManager |
|
308
|
|
|
*/ |
|
309
|
14 |
|
public function getEventManager() |
|
310
|
|
|
{ |
|
311
|
14 |
|
return $this->eventManager; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.