<?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";
}
}
}| Protendo | 2088 | its built on SMF 2.0.x so to be expected I guess. Still bad though. |
| Wedge | 542 | but I suspect your current working copy have even fewer. |
| Elkarte | 83 | Could be from using more OOP perhaps? |
| SMF2.1 | 1231 | not bad really, but still behind Elkarte/wedge |
| Silvercircle | 1316 | not bad |
| SMF 2.0.6 | 2500 | this I expected hah! |
<?php
$i = 0;
find_unused_globals(dirname(__FILE__));
echo '<hr>total: '.$i.'';
function find_unused_globals($dir)
{
global $i;
$files = scandir($dir);
foreach ($files as $file)
{
if ($file == '.' || $file == '..' || $file == '.git')
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);
$glos = isset($globs[1]) ? implode(',', $globs[1]) : '';
preg_match_all('~\$[a-zA-Z_]+~', $glos, $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 '<div style="margin: 1px 0; border: solid 1px #aaa;background: #eee; padding: 5px 10px;">Unused global in ', $file, ':', $matches[2][$key], ' -- ', $test_me, "</div>";
$i++;
}
}
}
}Very interesting! I have been lazy with this..
The results I got..(differs from yours? could be my changed script..?
(is there some styling in the table tag in wedge btw? I added a few basic color schemes in Protendo,in a new table-like tag with headers and title as well, very useful for me at least :D ).
Having unused globals takes memory, right? Thats the main point in removing them?
oh, and I added some to the script to read the results better(for me) so it now just reads:
Come to think of it - I am illiterate at this really - the use of objects, they also take up memory..so could Elkarte with so few globals in fact be using just as much memory as SMF due to them? Maybe some benchmark/resource meter for PHP will show this, I need to investigate further.
Ah, ok..still, its worthwhile setting the globals correctly, so I'll hunt down some of them. It may take a while before I reach all 2000 hah. It will be more tidy code though, removing those unused, good when hunting down bugs too.
OOP yes, I've been reluctant to adopt to them, but see their use of course. Its that though, beginning to use them makes me think how the whole system may be better using it, and that implies a total rewrite - which is very time-consuming. I simply don't have the time for it, and fear my theme/feature ideas will wither away while doing it... :P :)
About table tags..this is what I put on Protendo, its a simpler code and it certainly need some error checking(its now up to the author to do it correctly, I have an idea of a popup where you just fill in items though, and its inserted with the right tags for you), but my goal was "simplify" foremost. Not everyone understands table/tr/td so well. :)
yay. Thank you for that little helper.
Found some real ones in Elk with it, but some are definetely false reports.. I'm currently down to a total of 21 for Elk :whistle: 8 from upgrade.php, but there's the eval for the parsed SQL code.
Make a github repo and push it, It's a nice litte tool :cool:
checked with your updated version, 2 remaining and these are from an external library ::) Won't fix them but Elk is completely clean.
Edit: I left those in install.php and upgrade.php, too..
Bump to try and determine if it's really worth making a repo out of this snippet. ;)
PS: considering that Wedge will soon modify a few fundamentals of SMF such as folder structure and $theme stuff, are you willing to look into the importer when I do, and make sure it doesn't break..?
| 1. | Well, everyone but Arantor, of course. |
Heck, I could even run the thing myself on SMF 2.1, and push that...
function foobar() {
/* This is a comment.
global $something;
*/
return;
}function foobar() {
echo '
global $something;';
}This will be my first 'real' use of github though, and I'm wondering if it's worth hiding my e-mail address over there. Also, using the antispam feature, my e-mail address starts with "Naowe@" (my old user-name), when it's been renamed for quite a long time to "Nao@"... Dunno why it's still using that old name. Anyone knows how to fix, maybe..?