Find and fix places where globals were being used without declaring them.
All the places were found using a script, reproduced below, in case
anyone wants to reuse it.
But beware: The script produces a lot of false positives, because it
doesn't fully parse PHP and is fooled e.g. by javascript functions
inside of <<<HERE documents.
#!/usr/bin/perl -0777 -n
#
# find functions where variables starting with $wg are used without declaring them as globals
# should be given a list of files on the command line
#
# first remove all comments
# Warning: this may remove too much, e.g. if /* is inside a string
s,/\*.*?\*/,,sg; # remove multiline comments
s,#.*,,g; # remove single line comments starting with #
s,//.*,,g; # and the ones starting with //
s,(?<!\\)'.*?(?<!\\)',,g; # remove 'single quoted single-line strings'; may again remove too much
# now process each function one by one
# does not deal correctly with javascript functions embedded in <<HERE documents, unfortunately
#
while (/(^\s*)(?:(?:private|public|protected|static)\s+)*function\s+(\w+)\s*\((.*?)\)\s*\{(.*?)\1\}/msg) {
$fname = $2; $farg = $3; $fbody = $4;
%globals = ();
while ($farg =~ /(\$\w+)/g) { # treat arguments to functions as globals here
$globals{$1} = 1;
}
while ($fbody =~ /^\s*global\s+([^;]+?)\s*;/msg) { # find all global vars
for (split /\s*,\s*/, $1) {
$globals{$_} = 1;
}
}
while ($fbody =~ /(?<!\\)(\$wg\w+)\b/g) { # search for all variables starting with $wg and see if they are declared as globals
if (not $globals{$1}) {
print "Global $1 not declared in function $fname, file $ARGV\n";
$globals{$1} = 1; # warn only once
}
}
}