As mentioned in the thoughts area, I quickly wrote a script to find unused variables declared with the global keyword in your PHP scripts.
Just copy this into any PHP file, drop it at the root of your website install, and have fun! It's not 100% tested, but it seems to be working fine, so I thought I'd share it, if only because SMF needs to do something about their 1600+ unused globals. I know they don't take much processing power or anything, but it just sounds lazy to me to leave globals that are unused.
Feel free to improve it as you like!
I'm releasing this under the WTF Public License, for what it's worth. It's not long enough to even bother with a MIT license, although I dream of using that one in the future... :P
Code: [Select]
To do the opposite operation (i.e. finding globals that weren't declared in a global keyword) isn't going to work with a short script -- unless you only look for the 'usual culprits' ($context, $txt, etc), in which case, yes, a short script is doable, I guess. But for that, I'm using HHVM. It's working fine, and is very smart about it. Only problem, it only works on Linux, so I had to install a VM to run that VM. Amusing. And install tons of dependencies. And go through many more steps of the program not wanting to run (the joys of permissions), then not wanting to start operating (the joys of parameters), then not wanting to complete (the joys of JSON bugs in Ubuntu.)
It took me several hours to go through it, but I found over 400 documented 'bugs' in Wedge, some of which I believe are in SMF/Elk as well, not that many though, but I'll try to document those I find important for them to fix.
:edit:
- Fixed most single-line function declarations.
- Added a 'duplicate global definitions' part. If you have a line that says global $txt, $context, $txt, the script will now tell you about it.
Just copy this into any PHP file, drop it at the root of your website install, and have fun! It's not 100% tested, but it seems to be working fine, so I thought I'd share it, if only because SMF needs to do something about their 1600+ unused globals. I know they don't take much processing power or anything, but it just sounds lazy to me to leave globals that are unused.
Feel free to improve it as you like!
I'm releasing this under the WTF Public License, for what it's worth. It's not long enough to even bother with a MIT license, although I dream of using that one in the future... :P
<?php
find_unused_globals(dirname(__FILE__));
function find_unused_globals($dir)
{
$i = 0;
$files = scandir($dir);
foreach ($files as $file)
{
if ($file == '.' || $file == '..' || $file == '.git' || $file == 'other')
continue;
if (is_dir($dir . '/' . $file))
{
find_unused_globals($dir . '/' . $file);
continue;
}
if (substr($file, -4) != '.php')
continue;
$php = file_get_contents($dir . '/' . $file);
preg_match_all('~\n(\t*)function ([\w]+)[^}\n]+\n\1(.*?)\n\1}~s', $php, $matches);
foreach ($matches[3] as $key => $val)
{
preg_match_all('~global (\$[^;]+);~', $val, $globs);
$globs[1] = isset($globs[1]) ? $globs[1] : array();
foreach ($globs[1] as $find_dupes)
{
$dupes = array_map('trim', explode(',', $find_dupes));
if (count($dupes) > count(array_flip(array_flip($dupes))))
echo 'Found duplicate globals in ', $file, ':', $matches[2][$key], ' -- ', $find_dupes, "\n";
}
preg_match_all('~\$[a-zA-Z_]+~', implode(', ', $globs[1]), $there_we_are);
$val = str_replace($globs[0], '', $val);
if (isset($there_we_are[0]))
foreach ($there_we_are[0] as $test_me)
if (strpos($val, $test_me) === false)
echo 'Unused global in ', $file, ':', $matches[2][$key], ' -- ', $test_me, "\n";
}
}
}
To do the opposite operation (i.e. finding globals that weren't declared in a global keyword) isn't going to work with a short script -- unless you only look for the 'usual culprits' ($context, $txt, etc), in which case, yes, a short script is doable, I guess. But for that, I'm using HHVM. It's working fine, and is very smart about it. Only problem, it only works on Linux, so I had to install a VM to run that VM. Amusing. And install tons of dependencies. And go through many more steps of the program not wanting to run (the joys of permissions), then not wanting to start operating (the joys of parameters), then not wanting to complete (the joys of JSON bugs in Ubuntu.)
It took me several hours to go through it, but I found over 400 documented 'bugs' in Wedge, some of which I believe are in SMF/Elk as well, not that many though, but I'll try to document those I find important for them to fix.
:edit:
- Fixed most single-line function declarations.
- Added a 'duplicate global definitions' part. If you have a line that says global $txt, $context, $txt, the script will now tell you about it.