A short script to remove unused, useless globals in PHP.

Nao

  • Dadman with a boy
  • Posts: 16,079
Re: A short script to remove unused, useless globals in PHP.
« Reply #30, 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.

MultiformeIngegno

  • Posts: 1,337

Nao

  • Dadman with a boy
  • Posts: 16,079
Re: A short script to remove unused, useless globals in PHP.
« Reply #32, 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:
Re: A short script to remove unused, useless globals in PHP.
« Reply #33, 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.
Re: A short script to remove unused, useless globals in PHP.
« Reply #34, 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.
Re: A short script to remove unused, useless globals in PHP.
« Reply #35, 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.)
Re: A short script to remove unused, useless globals in PHP.
« Reply #36, 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:
Re: A short script to remove unused, useless globals in PHP.
« Reply #37, 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.