Wedge

Public area => The Pub => Off-topic => Topic started by: Nao on November 16th, 2013, 01:03 AM

Title: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 16th, 2013, 01:03 AM
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]
<?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.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 16th, 2013, 09:08 AM
I fixed a small bug in which unused globals were reported in wrong situations.

Current results:
Elk - 159 declared, but unused globals. Good job. I suspect this is the work of TE using NetBeans :P
Wedge - 323 declared, but unused globals. I'll get started on this! (I removed the /other/ folder from results, because it's a large offender, and will never make it into the Wedge package.)
SMF 2.1 - 1231 declared, but unused globals. Baaaaad!
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: colby67 on November 16th, 2013, 11:57 AM
Very interesting! I have been lazy with this..well, some time ago I started being more observant in my own functions, but I haven't touched any of the SMF native in this manner.

The results I got..(differs from yours? could be my changed script..?
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!


(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:
Code: [Select]
<?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++;
}
}
}
}
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: colby67 on November 16th, 2013, 12:02 PM
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.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 16th, 2013, 12:27 PM
Quote from Bloc on November 16th, 2013, 11:57 AM
Very interesting! I have been lazy with this..
No problem being lazy; you just need to fix when you spot it. :P
And with this script, then everyone can afford to be lazy again, in the future. They can just whip it up at some point, and fix everything they forgot to fix in one go.
Quote
The results I got..(differs from yours? could be my changed script..?
Lulz Protendo results... ;)
It's no problem, though.
In fact, I don't think adding tons of unused globals will have any performance impact. It's just, after all, an 'alias' to something that can be accessed through $GLOBALS&#91;'var'] just the same.
I do think, however, that it's best to save on filesize as much as possible. Every byte always counts, in the long run. Your mileage may vary.

My working copy had fewer than 542, because I'd already started removing a few ones manually, but not that much.
Also, I've fixed my script to output duplicate globals. It's a nice new feature.
I edited my post a few minutes ago, so you missed it, sorry.

Still missing: some support for create_function stuff and nested functions... (Yes, they're not taken into account.)
Quote
(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 ).
No, I don't think we changed anything to that tag. You're welcome to share suggestions, though.
Quote
Having unused globals takes memory, right? Thats the main point in removing them?
I don't know. Really, I don't think there's much of a point in worrying about that generally, but I'd recommend removing them from files that are loaded on every page load by SMF/Elk/Wedge/Pro/etc, because less is better.
Quote
oh, and I added some to the script to read the results better(for me) so it now just reads:
Oh, I didn't style anything, because on that script, I'll simply right-click, view source, and Opera 19 gives me line numbers with it, allowing me to know how many changes I have yet to make, plus a monospace font, which is more readable for code, of course.
I'll leave it up to you, though!
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 16th, 2013, 12:30 PM
Quote from Bloc on November 16th, 2013, 12:02 PM
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.
Objects take a bit more memory, more processing power, and are generally not the way to go if you're looking into performance.
They are, however, very much welcome for complex code. And when it comes to micro-optimization, objects are not really worth it. I made many tests on the system object, for instance. Replacing $user_info['something'] (+ global) with we::$user['something'] (no global) had a tiny, tiny performance impact, but not enough to justify not using it.
In case it doesn't show... I hate globals :lol:
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: colby67 on November 16th, 2013, 12:49 PM
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. :)

http://protendo.net/index.php?topic=9.0

Title: Re: A short script to remove unused, useless globals in PHP.
Post by: TE on November 16th, 2013, 06:52 PM
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:
Posted: November 16th, 2013, 06:32 PM

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..
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 16th, 2013, 11:11 PM
Quote from Bloc on November 16th, 2013, 12:49 PM
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.
Yes, it's always good to have a clean codebase!
I can't believe SMF's has always been so 'dirty', in comparison... But I did spend many months fixing up all the code.
Quote from Bloc on November 16th, 2013, 12:49 PM
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 :)
OOP can't live without at least a bit of procedural code here and there I'll say, because otherwise, might as well start using a framework like Laravel or something even newer and fancier, and then start rewriting everything on top of it, etc... (Like FluxBB is doing, for instance. Well, that version 2.0 has been in development hell for months now, so... Maybe not such a good idea.)
Quote from Bloc on November 16th, 2013, 12:49 PM
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. :)
Very, very nice.
I could see you turning into a more familar tag family, though:
[table][th]Hello|World|!!![/th][tr]Catch|me if you|can[/tr][/table]
I'm sure we could even tweak the internal table decoding functions to support this alternative format, and then push for its use.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 16th, 2013, 11:16 PM
Quote from TE on November 16th, 2013, 06:52 PM
yay. Thank you for that little helper.
You're welcome.
Quote
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.
Impressive.
Quote
Make a github repo and push it, It's a nice litte tool :cool:
I don't know, do you think it'll be that helpful..? What can be added to it? Apart from trying to fix false positives (always a question mark on whether it's possible to do much) and adding support for nested functions (yes, this one's doable), I don't know what else to do... I tried changing the code to allow for the removal of strings, but considering there are several ways to start a string in PHP, it started getting complicated, and then I gave up on that one...

Anyway, I'm not adverse to the idea of making a repo out of it; it's just that I'm not into the habit of pushing a repo that's just one file, and a few dozen lines... ;)
Quote
checked with your updated version, 2 remaining and these are from an external library ::) Won't fix them but Elk is completely clean.
Well, I'm sure you already did 90% of the work through NetBeans, didn't you? ;)
I decided against using NetBeans for that generally, because of the many false positives, such as "undeclared variable" when doing preg_match_all($regex, $string, $my_new_variable)... Yeah, NetBeans, I'm not going to initialize a variable just because you can't fathom that PHP would actually initialize the variable this way, hmm...
Quote
Edit: I left those in install.php and upgrade.php, too..
I'm sure Elk isn't 'upgradeable' via upgrade.php just like Wedge isn't, so yeah, you can probably leave it out... ;)
As for install.php, it doesn't have many issues with global, IIRC.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 18th, 2013, 11:15 PM
Bump to try and determine if it's really worth making a repo out of this snippet. ;)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: emanuele on November 19th, 2013, 12:01 AM
You can use gist too if you want: not a "real" repo, just a sort of pastebin, but with everything a repo has in terms of versioning and forking (I'm not sure you can clone gists).

https://gist.github.com/
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: MultiformeIngegno on November 19th, 2013, 12:12 AM
It should be worth putting it on Github.. :)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: nolsilang on November 19th, 2013, 04:05 AM
I vote for putting it on GitHub :)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: TE on November 19th, 2013, 05:53 AM
Quote
Bump to try and determine if it's really worth making a repo out of this snippet. ;)
yep, make a repo named "tools" or "helpers" or whatever you like and put it there.  If you don't want your own repo I can "host" it for you :lol: :lol: https://github.com/eurich/php-tools
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 19th, 2013, 09:33 AM
Oh my, I forgot you liked complex interfaces for small operations... Cf. your analyzer :lol:
So, should I expect you to come by, fork my code and turn it into a giga-script? Then you can do that right now, since it's a WTF file... You don't even have to credit me for the original code, although it'd be nice if you did.
If not, then I'll create my repo all right, but I just don't expect to modify the code any more.

Well, maybe I should also move the 'attic', or even the entire 'other' folder, to another repo. But I've never done that, and don't know if it's easy to do. (Again-- filter-branch. Ouch...)

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..?
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: TE on November 19th, 2013, 04:36 PM
Quote from Nao on November 19th, 2013, 09:33 AM
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..?
yep, can have a look but moving folders from one place to another is not a problem for the importer. Modifying the db structure or the content (e.g. BBCodes) is important to know.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 19th, 2013, 05:19 PM
I made some improvements to the script (it will now find more unused vars than before), so I guess it makes sense to use git for that; I'll be moving to a github repo so that I can commit if I have other things to update.

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..?

PS: let's just say it's going to be... well, my very own github tutorial.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 20th, 2013, 10:54 AM
It's all turned into a really, really nice little script by now. It does all the changes for me, too, if I ask nicely. I'm currently in the process of checking all files manually, it's all good. Only one single false-positive so far, and that's because the global keyword was in comments, so I added an extra check for that (it won't work in multi-line comments, though.)

And I really should commit it! But I have yet to receive an answer from github, re: my account/e-mail problems. I don't want to have to re-create my repo later, so... Waiting for now.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 20th, 2013, 05:29 PM
(Yay, first Google result on selected keywords from the post title. SEO isn't as bad as I thought :D)

Current script size is 120 lines, lol.
The next update will be MIT licensed, then. I'm still waiting for github to answer my e-mail, or anyone really.

SMF people will love it[1], because of the auto-fixer, and it had 100% accuracy on the Wedge codebase, I didn't have to do any last-minute fixes apart from one in ManagePlugins which helped strengthen the script even further.
Heck, I could even run the thing myself on SMF 2.1, and push that...
 1. Well, everyone but Arantor, of course.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: live627 on November 20th, 2013, 09:59 PM
Quote
Heck, I could even run the thing myself on SMF 2.1, and push that...
There will be false positives.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 20th, 2013, 10:55 PM
Right now, the only false positives that can be found are:

Code: [Select]
function foobar() {
/* This is a comment.
global $something;
*/
return;
}

As well as, hmm...

Code: [Select]
function foobar() {
echo '
global $something;';
}

There may be other situations where it'll find false positives, but they'll probably be variations on the ones above.
Basically, I could try and remove comments, I'm doing it on Wess after all, but I'll have to restore them before fixing the files, and I can't bother for now. As for quotes, since there are single quotes and double quotes and heredoc and nowdoc, and... I don't know if I'll ever bother about that, unless I can find a script that removes these easily for me. (Keeping in mind they must also be restored.)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: live627 on November 20th, 2013, 11:41 PM
$smcFunc and packman are the two major offenders that I know of.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 20th, 2013, 11:44 PM
I don't know about packman. As for $smcFunc, I don't really see why it should give out false positives..?
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: live627 on November 20th, 2013, 11:51 PM
I'm just going on what Arantor said. I'm also not looking at the code, either.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 21st, 2013, 12:01 AM
But what he "said" was based on the very first version of the script, wasn't it..? The one that was buggy and fixed less than an hour later...
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 23rd, 2013, 12:09 AM
Quote from Nao on November 19th, 2013, 05:19 PM
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..?
I have yet to receive an answer from github, which is surprising, given that my earlier support request didn't involve a bug report and was answered within a few hours.

This means that I'm still unable to launch the repo. Although I could just give up on the idea of using the website's merging feature at all. They have a method to do the merges locally, so I could possibly fetch pull requests, then either rebase or merge them, whatever, and then push them to the official repo. That would, effectively, make my local repo the 'authoritative' one, with the github repo being just a 'backup' of sorts, I guess. And it also means I'm the only merger around forever, at least until I'm ready for 'more'.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: emanuele on November 23rd, 2013, 02:32 PM
Where can you see the email at github?

Sign-off?
If so, you have to change the config of your local git (global config or local config depending on what you want to obtain).
And the old commits do not change unless you rewrite the history resigning all of them (once I found a trick to do that, but last time I tried to find it again I wasn't able to... :().
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 23rd, 2013, 03:19 PM
No. Shows up when doing site operations like merging pull requests or creating a repo.
I'm aware of how settings work don't worry ;)
O just want the website to follow my own personal settings but they won't. They only pass valid emails to git. Or you have to use their no spam email, but as I said, it's buggy, and github is not giving feedback.

Re history rewrite, it's done through git filter-branch. There's a script for email rewriting in the official online git book (git-scm.com iirc), and stackoverflow of course.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 24th, 2013, 11:26 AM
So, I received an answer from GH shortly after my last post here. :)
- They fixed my username,
- They're working on a fix for everyone who changes their username,
- They're looking into whether it's possible to choose your commit name when doing web merges.

Good!

In the meantime, I made some tests on a GH repo, and have come to these conclusions:
- (This, I already knew but it's important to point out) You can set your commit e-mail per-repo ('local') or for all repos ('global'), so if you want to change e-mails between repos, it's easy as pie, and if they're all in your GH account, then GH will always show your commits as yours.
- Antispam e-mails used when web merging at GH are really, really noreply e-mails. That is, e-mails to these aren't redirected to your actual registered e-mail, so... Hmm, I don't know, it can be a good thing, or a bad thing.
- TortoiseGit statistics only care about the user name. If my user name is 'Nao', whatever the commit e-mail is, the stats page will merge everything together into the Nao entry. Good.
- My commit e-mail at GH is indeed fixed.
- You can 'force' GH to commit using your user name by simply removing your name. I've chosen to put it into my 'Work' box, it works very well, as it shows my name in small characters right below my user name, and right above my location. That's, really, the best place I could have put it into, eh. Of course I can't say 'Wedge' in my work box, but... Whatever. People who come to my account know what I'm working on, really. After all, Wedge will soon be on my repo list, so, whatever... :P

All in all, good news. I'll get started on this unused-globals.php repo, using my usual commit e-mail (nao@wedge --> and yes, you can add the obvious TLD to the domain, and it'll redirect to my e-mail address. For now, at least.)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 24th, 2013, 12:12 PM
And here we are... I made a changelog, so I might as well post it.
Note: I just made a push -f to overwrite my initial commit with a UTF-8 version of the file. If you downloaded the repo previously, please re-clone it from scratch. Future commits will avoid forced pushes, I just thought -- who cares, it's an initial commit.

https://github.com/Wedge/tools/ (<-- hmm, should I adopt 'wedge' or 'Wedge' in the URL..?)
Released under the MIT license.

85 insertions(+), 8 deletions(-)

Auto-fixing, cosmetic updates and improvements.

+ The script will now attempt to fix the files for you, at least if they seem safe to work on. Add the ?fixme parameter, but don't forget to do a dry-run before.

@ Known potential false positives that aren't detected yet: globals declared inside a string (sometimes it'll work, in create_function for instance), and inside multi-line comments (i.e. commented out code.) If you use this script, don't fixme blindly, and check all changes manually before committing your work.

@ Duplicate globals aren't auto-fixed. You'll have to do these manually, at least for now.

+ Proper HTML styling. You no longer have to view-source in order to make it usable.

+ Support for all line-ending types.

- Removed false positive: use of 'global $something' inside a line (e.g. a comment), with text preceding it that isn't a bracket.

+ Adding a warning (and not auto-fixing) in case an unused global was found in a function, but also the following: $$ (perhaps a variable variable has your global name in it?), ${$ (another syntax for variable variables), include(), require() and their _once() variations. The script will indicate which of these it found, and keep going its way.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: MultiformeIngegno on November 24th, 2013, 02:46 PM
It seems better to have 'wedge' instead of 'Wedge' :)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on November 24th, 2013, 06:09 PM
Well, I would usually say that, but considering that the default URLs given out by GH are '/Wedge', and that to force it to use '/wedge' I have to change the project's name, and I don't want its name to be lowercased, I guess I'll have to settle on /Wedge, because I don't see myself changing that every single time I post a link to the repo... :lol:
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on December 10th, 2013, 11:38 PM
---- A pretty fine update for this script! For whoever's interested, it's now finding 180 matches in Wedge (it used to be at zero, excluding unprocessed false positives.)

[master a98b243]
 1 file changed, 55 insertions(+), 10 deletions(-), 2.98 KiB
 rename unused-globals.php => fix-globals.php (52%)

+ New feature: searches for undeclared globals. Doesn't attempt to fix them yet, but will detect false positives inside comments. You have to provide a list of common globals, I used a selection of Wedge globals, do as you like with your version of the script.

+ Added support for class methods.

+ More color codes, and a subtle gradient background for differentiating between error types.

* Renamed script to 'fix-globals.php', considering that it's been some time since it was limited to just finding unused globals.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on December 15th, 2013, 10:19 PM
[master 76c4ba9]
 1 file changed, 163 insertions(+), 37 deletions(-)

+ Lowered rate of false positives by writing a nice little script that will remove comments (// and /*), as well as strings (single quotes only, and will skip double quotes), which would be worth an extra script file just by itself (I'll probably split it eventually.) In order to ensure it's done properly, the script has to spend a long time on this, making it up to 5 or 10 times slower than before. But, hey, less false positives is better!

@ Note that due to this change, the fixme option is likely to be totally broken. I tried to ensure it wasn't, but I can't say for sure, and will need to test.

+ Added an ignorefp option, to prevent the script from showing potential false positives. If you're fixing files manually, this could be a blessing.

+ Added more false positive detections for undeclared globals: variables declared in function params, in foreach() loops, in list() calls, and declared as an implied array by assignment. Removed 'found in a comment' false positive, since that is no longer an issue.

* Slight cosmetic improvements. I still need to add some congratulations if your code shows no signs of a global problem.

+ Adding a time limit, if PHP allows it.

+ Adding links to the couple of non-threatening options, noclean and ignorefp.

+ Adding $modSettings to the list of globals to look for. This is a SMF global, not used in Wedge, but whatever.

! Cache folder shouldn't be checked.
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on December 30th, 2013, 12:37 PM
[master a112bbe]
 1 file changed, 41 insertions(+), 23 deletions(-)

* Prevent recursion errors in XDebug.
* Minor parsing optimization.
* Added a message in case no errors are found.
* Adding $theme global for SMF mainline/forks. (fix-globals has no external contributions, but I'm the only one to use it. Hint.)
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on January 29th, 2014, 04:10 PM
I realized earlier this morning that I'd left this script to rot for a few days (i.e. after Wedge went public)... I've put a lot of work into the current version, and I didn't even notice I hadn't pushed it yet! (I made several commits over the weeks.)

Anyway, so I'll try to finish a version that supports lambda functions today. Believe me, it's horribly complicated and required not only a large rewrite of the script, but also forced me to remove the ability to fix stuff automatically... Which is okay, I suppose, because it only fixed unneeded globals, and the script started doing much more than that these last few weeks, and if you want to automatically remove unused globals, you can STILL use an older version of the script, I guess.

I also renamed it to Globye, which I find more amusing than some basic, lousy name. :ph34r:
Title: Re: A short script to remove unused, useless globals in PHP.
Post by: Nao on January 29th, 2014, 06:52 PM
Well, I failed horribly at fixing my function, so I reverted my code and simply pushed the existing work-in-progress.
It's ugly.

Sad face.

I'll try again tomorrow, or something. I don't have time left to work on something else than Wedge right now.