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() |
|
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) |
|
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() |
|
147 | |||
148 | /** |
||
149 | * Clears only all symbols. |
||
150 | */ |
||
151 | 39 | public function clearSymbols() |
|
156 | |||
157 | /** |
||
158 | * Returns a variable if it exists. |
||
159 | * |
||
160 | * @param $name |
||
161 | * @return Variable|null |
||
162 | */ |
||
163 | 79 | public function getSymbol($name) |
|
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 | * @return bool |
||
176 | */ |
||
177 | 41 | public function notice($type, $message, \PhpParser\NodeAbstract $expr, $status = Check::CHECK_SAFE) |
|
|
|||
178 | { |
||
179 | 41 | return $this->addIssue($type, $message, $expr->getLine()); |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * Creates a syntax error message. |
||
184 | * |
||
185 | * @param \PhpParser\Error $exception |
||
186 | * @param string $filepath |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function syntaxError(\PhpParser\Error $exception, $filepath) |
||
190 | { |
||
191 | return $this->addIssue('syntax-error', $exception->getMessage(), $exception->getStartLine(), $filepath); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param $type |
||
196 | * @param $message |
||
197 | * @param $line |
||
198 | * @param string|null $filepath |
||
199 | * @return bool |
||
200 | */ |
||
201 | 41 | protected function addIssue($type, $message, $line, $filepath = null) |
|
202 | { |
||
203 | 41 | $filepath = $filepath ?: $this->filepath; |
|
204 | 41 | $issue = new Issue($type, $message, new IssueLocation($filepath, $line)); |
|
205 | |||
206 | 41 | if ($this->application->getConfiguration()->getValue('blame')) { |
|
207 | exec("git blame --show-email -L {$line},{$line} " . $filepath, $result); |
||
208 | if ($result && isset($result[0])) { |
||
209 | $issue->setBlame(trim($result[0])); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | 41 | $issueCollector = $this->application->getIssuesCollector(); |
|
214 | 41 | $issueCollector->addIssue($issue); |
|
215 | |||
216 | 41 | return true; |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns an array of all variables. |
||
221 | * |
||
222 | * @return Variable[] |
||
223 | */ |
||
224 | 39 | public function getSymbols() |
|
225 | { |
||
226 | 39 | return $this->symbols; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param AbstractDefinition $scope |
||
231 | */ |
||
232 | 868 | public function setScope(AbstractDefinition $scope = null) |
|
233 | { |
||
234 | 868 | $this->scope = $scope; |
|
235 | 868 | } |
|
236 | |||
237 | /** |
||
238 | * Creates a debug message. |
||
239 | */ |
||
240 | 41 | public function debug($message, \PhpParser\Node $expr = null) |
|
241 | { |
||
242 | 41 | if ($this->output->isDebug()) { |
|
243 | $this->output->writeln('[DEBUG] ' . $message); |
||
244 | $this->output->write($this->filepath); |
||
245 | |||
246 | if ($expr) { |
||
247 | $this->output->write(':' . $expr->getLine()); |
||
248 | } |
||
249 | |||
250 | $this->output->writeln(''); |
||
251 | } |
||
252 | 41 | } |
|
253 | |||
254 | /** |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getFilepath() |
||
258 | { |
||
259 | return $this->filepath; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param string $filepath |
||
264 | */ |
||
265 | 41 | public function setFilepath($filepath) |
|
266 | { |
||
267 | 41 | $this->filepath = $filepath; |
|
268 | 41 | } |
|
269 | |||
270 | /** |
||
271 | * @return integer |
||
272 | */ |
||
273 | 36 | public function getCurrentBranch() |
|
274 | { |
||
275 | 36 | return $this->currentBranch; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param int|string $currentBranch |
||
280 | */ |
||
281 | 9 | public function setCurrentBranch($currentBranch) |
|
282 | { |
||
283 | 9 | $this->currentBranch = $currentBranch; |
|
284 | 9 | } |
|
285 | |||
286 | /** |
||
287 | * Updates all references on the given variable. |
||
288 | * |
||
289 | * @param Variable $variable |
||
290 | * @param $type |
||
291 | * @param $value |
||
292 | */ |
||
293 | 5 | public function modifyReferencedVariables(Variable $variable, $type, $value) |
|
294 | { |
||
295 | 5 | foreach ($this->symbols as $symbol) { |
|
296 | 5 | $referencedTo = $symbol->getReferencedTo(); |
|
297 | 5 | if ($referencedTo) { |
|
298 | 2 | if ($referencedTo === $variable) { |
|
299 | 2 | $symbol->modify($type, $value); |
|
300 | 2 | } |
|
301 | 2 | } |
|
302 | 5 | } |
|
303 | 5 | } |
|
304 | |||
305 | /** |
||
306 | * @return EventManager |
||
307 | */ |
||
308 | 54 | public function getEventManager() |
|
312 | } |
||
313 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.