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