1 | <?php |
||
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 | 868 | public function __construct(OutputInterface $output, Application $application, EventManager $eventManager) |
|
72 | { |
||
73 | 868 | $this->output = $output; |
|
74 | 868 | $this->application = $application; |
|
75 | |||
76 | 868 | $this->initGlobals(); |
|
77 | 868 | $this->eventManager = $eventManager; |
|
78 | 868 | } |
|
79 | |||
80 | /** |
||
81 | * @return Expression |
||
82 | */ |
||
83 | 859 | public function getExpressionCompiler() |
|
84 | { |
||
85 | 859 | return new Expression($this, $this->eventManager); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Adds all global variables to the context. |
||
90 | */ |
||
91 | 868 | public function initGlobals() |
|
92 | { |
||
93 | /** |
||
94 | * http://php.net/manual/language.variables.superglobals.php |
||
95 | */ |
||
96 | 868 | $this->addVariable(new GlobalVariable('GLOBALS', [], CompiledExpression::ARR)); |
|
97 | 868 | $this->addVariable(new GlobalVariable('_SERVER', [], CompiledExpression::ARR)); |
|
98 | 868 | $this->addVariable(new GlobalVariable('_GET', [], CompiledExpression::ARR)); |
|
99 | 868 | $this->addVariable(new GlobalVariable('_POST', [], CompiledExpression::ARR)); |
|
100 | 868 | $this->addVariable(new GlobalVariable('_FILES', [], CompiledExpression::ARR)); |
|
101 | 868 | $this->addVariable(new GlobalVariable('_COOKIE', [], CompiledExpression::ARR)); |
|
102 | 868 | $this->addVariable(new GlobalVariable('_SESSION', [], CompiledExpression::ARR)); |
|
103 | 868 | $this->addVariable(new GlobalVariable('_REQUEST', [], CompiledExpression::ARR)); |
|
104 | 868 | $this->addVariable(new GlobalVariable('_ENV', [], CompiledExpression::ARR)); |
|
105 | 868 | } |
|
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 | 3 | public function addSymbol($name) |
|
114 | { |
||
115 | 3 | $variable = new Variable($name); |
|
116 | 3 | $this->symbols[$name] = $variable; |
|
117 | |||
118 | 3 | return $variable; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Adds a variable to the context. |
||
123 | * |
||
124 | * @param Variable $variable |
||
125 | * @return bool |
||
126 | */ |
||
127 | 868 | public function addVariable(Variable $variable) |
|
128 | { |
||
129 | 868 | $this->symbols[$variable->getName()] = $variable; |
|
130 | |||
131 | 868 | return true; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Resets context to beginning stage. |
||
136 | */ |
||
137 | 41 | public function clear() |
|
138 | { |
||
139 | 41 | $this->symbols = []; |
|
140 | 41 | $this->scope = null; |
|
141 | 41 | $this->scopePointer = null; |
|
142 | 41 | $this->currentBranch = null; |
|
143 | 41 | $this->aliasManager = null; |
|
144 | |||
145 | 41 | $this->initGlobals(); |
|
146 | 41 | } |
|
147 | |||
148 | /** |
||
149 | * Clears only all symbols. |
||
150 | */ |
||
151 | 39 | public function clearSymbols() |
|
152 | { |
||
153 | 39 | unset($this->symbols); |
|
154 | 39 | $this->symbols = []; |
|
155 | 39 | } |
|
156 | |||
157 | /** |
||
158 | * Returns a variable if it exists. |
||
159 | * |
||
160 | * @param $name |
||
161 | * @return Variable|null |
||
162 | */ |
||
163 | 79 | public function getSymbol($name) |
|
164 | { |
||
165 | 79 | return isset($this->symbols[$name]) ? $this->symbols[$name] : null; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Creates a notice message. |
||
170 | * |
||
171 | * @param string $type |
||
172 | * @param string $message |
||
173 | * @param \PhpParser\NodeAbstract $expr |
||
174 | * @param int $status |
||
175 | * @param array $categories |
||
176 | * @return bool |
||
177 | */ |
||
178 | 41 | public function notice($type, $message, \PhpParser\NodeAbstract $expr, $status = Check::CHECK_SAFE, array $categories = [Issue::CATEGORY_BUG_RISK]) |
|
|
|||
179 | { |
||
180 | 41 | $issue = new Issue($type, $message, new IssueLocation($this->filepath, $expr->getLine()), $categories); |
|
181 | |||
182 | 41 | return $this->addIssue($issue); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Creates a syntax error message. |
||
187 | * |
||
188 | * @param \PhpParser\Error $exception |
||
189 | * @param string $filepath |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function syntaxError(\PhpParser\Error $exception, $filepath) |
||
193 | { |
||
194 | $issue = new Issue( |
||
195 | 'syntax-error', |
||
196 | $exception->getMessage(), |
||
197 | new IssueLocation($filepath, $exception->getStartLine()), |
||
198 | [Issue::CATEGORY_BUG_RISK] |
||
199 | ); |
||
200 | |||
201 | return $this->addIssue($issue); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param Issue $issue |
||
206 | * @return bool |
||
207 | */ |
||
208 | 41 | protected function addIssue(Issue $issue) |
|
209 | { |
||
210 | 41 | if ($this->application->getConfiguration()->getValue('blame')) { |
|
211 | $line = $issue->getLocation()->getLineStart(); |
||
212 | |||
213 | exec("git blame --show-email -L {$line},{$line} " . $issue->getLocation()->getFilePath(), $result); |
||
214 | if ($result && isset($result[0])) { |
||
215 | $issue->setBlame(trim($result[0])); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | 41 | $issueCollector = $this->application->getIssuesCollector(); |
|
220 | 41 | $issueCollector->addIssue($issue); |
|
221 | |||
222 | 41 | return true; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * Returns an array of all variables. |
||
227 | * |
||
228 | * @return Variable[] |
||
229 | */ |
||
230 | 39 | public function getSymbols() |
|
234 | |||
235 | /** |
||
236 | * @param AbstractDefinition $scope |
||
237 | */ |
||
238 | 868 | public function setScope(AbstractDefinition $scope = null) |
|
242 | |||
243 | /** |
||
244 | * Creates a debug message. |
||
245 | */ |
||
246 | 41 | public function debug($message, \PhpParser\Node $expr = null) |
|
247 | { |
||
248 | 41 | if ($this->output->isDebug()) { |
|
249 | $this->output->writeln('[DEBUG] ' . $message); |
||
250 | $this->output->write($this->filepath); |
||
251 | |||
252 | if ($expr) { |
||
253 | $this->output->write(':' . $expr->getLine()); |
||
259 | |||
260 | /** |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getFilepath() |
||
267 | |||
268 | /** |
||
269 | * @param string $filepath |
||
270 | */ |
||
271 | 41 | public function setFilepath($filepath) |
|
275 | |||
276 | /** |
||
277 | * @return integer |
||
278 | */ |
||
279 | 36 | public function getCurrentBranch() |
|
283 | |||
284 | /** |
||
285 | * @param int|string $currentBranch |
||
286 | */ |
||
287 | 9 | public function setCurrentBranch($currentBranch) |
|
291 | |||
292 | /** |
||
293 | * Updates all references on the given variable. |
||
294 | * |
||
295 | * @param Variable $variable |
||
296 | * @param $type |
||
297 | * @param $value |
||
298 | */ |
||
299 | 5 | public function modifyReferencedVariables(Variable $variable, $type, $value) |
|
310 | |||
311 | /** |
||
312 | * @return EventManager |
||
313 | */ |
||
314 | 54 | public function getEventManager() |
|
318 | } |
||
319 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.