Mercurial > hg > isophonics-drupal-site
comparison vendor/psy/psysh/src/CodeCleaner.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | c2387f117808 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
136 if ($file === null) { | 136 if ($file === null) { |
137 return; | 137 return; |
138 } | 138 } |
139 | 139 |
140 try { | 140 try { |
141 $code = @file_get_contents($file); | 141 $code = @\file_get_contents($file); |
142 if (!$code) { | 142 if (!$code) { |
143 return; | 143 return; |
144 } | 144 } |
145 | 145 |
146 $stmts = $this->parse($code, true); | 146 $stmts = $this->parse($code, true); |
167 * | 167 * |
168 * @return string|null | 168 * @return string|null |
169 */ | 169 */ |
170 private static function getDebugFile() | 170 private static function getDebugFile() |
171 { | 171 { |
172 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); | 172 $trace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
173 | 173 |
174 foreach (array_reverse($trace) as $stackFrame) { | 174 foreach (\array_reverse($trace) as $stackFrame) { |
175 if (!self::isDebugCall($stackFrame)) { | 175 if (!self::isDebugCall($stackFrame)) { |
176 continue; | 176 continue; |
177 } | 177 } |
178 | 178 |
179 if (preg_match('/eval\(/', $stackFrame['file'])) { | 179 if (\preg_match('/eval\(/', $stackFrame['file'])) { |
180 preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches); | 180 \preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches); |
181 | 181 |
182 return $matches[1][0]; | 182 return $matches[1][0]; |
183 } | 183 } |
184 | 184 |
185 return $stackFrame['file']; | 185 return $stackFrame['file']; |
212 * | 212 * |
213 * @return string|false Cleaned PHP code, False if the input is incomplete | 213 * @return string|false Cleaned PHP code, False if the input is incomplete |
214 */ | 214 */ |
215 public function clean(array $codeLines, $requireSemicolons = false) | 215 public function clean(array $codeLines, $requireSemicolons = false) |
216 { | 216 { |
217 $stmts = $this->parse('<?php ' . implode(PHP_EOL, $codeLines) . PHP_EOL, $requireSemicolons); | 217 $stmts = $this->parse('<?php ' . \implode(PHP_EOL, $codeLines) . PHP_EOL, $requireSemicolons); |
218 if ($stmts === false) { | 218 if ($stmts === false) { |
219 return false; | 219 return false; |
220 } | 220 } |
221 | 221 |
222 // Catch fatal errors before they happen | 222 // Catch fatal errors before they happen |
223 $stmts = $this->traverser->traverse($stmts); | 223 $stmts = $this->traverser->traverse($stmts); |
224 | 224 |
225 // Work around https://github.com/nikic/PHP-Parser/issues/399 | 225 // Work around https://github.com/nikic/PHP-Parser/issues/399 |
226 $oldLocale = setlocale(LC_NUMERIC, 0); | 226 $oldLocale = \setlocale(LC_NUMERIC, 0); |
227 setlocale(LC_NUMERIC, 'C'); | 227 \setlocale(LC_NUMERIC, 'C'); |
228 | 228 |
229 $code = $this->printer->prettyPrint($stmts); | 229 $code = $this->printer->prettyPrint($stmts); |
230 | 230 |
231 // Now put the locale back | 231 // Now put the locale back |
232 setlocale(LC_NUMERIC, $oldLocale); | 232 \setlocale(LC_NUMERIC, $oldLocale); |
233 | 233 |
234 return $code; | 234 return $code; |
235 } | 235 } |
236 | 236 |
237 /** | 237 /** |
305 | 305 |
306 private function parseErrorIsEOF(\PhpParser\Error $e) | 306 private function parseErrorIsEOF(\PhpParser\Error $e) |
307 { | 307 { |
308 $msg = $e->getRawMessage(); | 308 $msg = $e->getRawMessage(); |
309 | 309 |
310 return ($msg === 'Unexpected token EOF') || (strpos($msg, 'Syntax error, unexpected EOF') !== false); | 310 return ($msg === 'Unexpected token EOF') || (\strpos($msg, 'Syntax error, unexpected EOF') !== false); |
311 } | 311 } |
312 | 312 |
313 /** | 313 /** |
314 * A special test for unclosed single-quoted strings. | 314 * A special test for unclosed single-quoted strings. |
315 * | 315 * |
342 return $e->getRawMessage() === 'Unterminated comment'; | 342 return $e->getRawMessage() === 'Unterminated comment'; |
343 } | 343 } |
344 | 344 |
345 private function parseErrorIsTrailingComma(\PhpParser\Error $e, $code) | 345 private function parseErrorIsTrailingComma(\PhpParser\Error $e, $code) |
346 { | 346 { |
347 return ($e->getRawMessage() === 'A trailing comma is not allowed here') && (substr(rtrim($code), -1) === ','); | 347 return ($e->getRawMessage() === 'A trailing comma is not allowed here') && (\substr(\rtrim($code), -1) === ','); |
348 } | 348 } |
349 } | 349 } |