MediaWiki REL1_40
CodeCleanerGlobalsPass.php
Go to the documentation of this file.
1<?php
37class CodeCleanerGlobalsPass extends \Psy\CodeCleaner\CodeCleanerPass {
38 private static $superglobals = [
39 'GLOBALS', '_SERVER', '_ENV', '_FILES', '_COOKIE', '_POST', '_GET', '_SESSION'
40 ];
41
42 public function beforeTraverse( array $nodes ) {
43 $globalVars = array_diff( array_keys( $GLOBALS ), self::$superglobals );
44 $validGlobalVars = array_filter( $globalVars, static function ( string $name ) {
45 // https://www.php.net/manual/en/language.variables.basics.php
46 return preg_match( '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $name );
47 } );
48
49 if ( $validGlobalVars ) {
50 $globalCommand = new \PhpParser\Node\Stmt\Global_( array_map( static function ( string $name ) {
51 return new \PhpParser\Node\Expr\Variable( $name );
52 }, $validGlobalVars ) );
53 array_unshift( $nodes, $globalCommand );
54 }
55
56 return $nodes;
57 }
58}
Prefix the real command with a 'global $VAR, $VAR2, ...;' command, where $VAR etc.