Merge "Special:Newpages feed now shows first revision instead of latest revision"
[lhc/web/wiklou.git] / maintenance / CodeCleanerGlobalsPass.inc
1 <?php
2 /**
3 * Psy CodeCleaner to allow PHP super globals.
4 *
5 * https://github.com/bobthecow/psysh/issues/353
6 *
7 * Copyright © 2017 Justin Hileman <justin@justinhileman.info>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup Maintenance
26 *
27 * @author Justin Hileman <justin@justinhileman.info>
28 */
29
30 /**
31 * Prefix the real command with a bunch of 'global $VAR;' commands, one for each global.
32 * This will make the shell behave as if it was running in the global scope (almost;
33 * variables created in the shell won't become global if no global variable by that name
34 * existed before).
35 */
36 class CodeCleanerGlobalsPass extends \Psy\CodeCleaner\CodeCleanerPass {
37 private static $superglobals = [
38 'GLOBALS', '_SERVER', '_ENV', '_FILES', '_COOKIE', '_POST', '_GET', '_SESSION'
39 ];
40
41 public function beforeTraverse( array $nodes ) {
42 $names = [];
43 foreach ( array_diff( array_keys( $GLOBALS ), self::$superglobals ) as $name ) {
44 array_push( $names, new \PhpParser\Node\Expr\Variable( $name ) );
45 }
46
47 array_unshift( $nodes, new \PhpParser\Node\Stmt\Global_( $names ) );
48
49 return $nodes;
50 }
51 }