MediaWiki master
CodeCleanerGlobalsPass.php
Go to the documentation of this file.
1<?php
23class CodeCleanerGlobalsPass extends \Psy\CodeCleaner\CodeCleanerPass {
24 private const SUPERGLOBALS = [
25 'GLOBALS', '_SERVER', '_ENV', '_FILES', '_COOKIE', '_POST', '_GET', '_SESSION'
26 ];
27
28 public function beforeTraverse( array $nodes ): array {
29 $globalVars = array_diff( array_keys( $GLOBALS ), self::SUPERGLOBALS );
30 $validGlobalVars = array_filter( $globalVars, static function ( string $name ) {
31 // https://www.php.net/manual/en/language.variables.basics.php
32 return preg_match( '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $name );
33 } );
34
35 if ( $validGlobalVars ) {
36 $globalCommand = new \PhpParser\Node\Stmt\Global_( array_map( static function ( string $name ) {
37 return new \PhpParser\Node\Expr\Variable( $name );
38 }, $validGlobalVars ) );
39 array_unshift( $nodes, $globalCommand );
40 }
41
42 return $nodes;
43 }
44}
Prefix the real command with a 'global $VAR, $VAR2, ...;' command, where $VAR etc.