We bought a big PHP script and changed a lot of its code and functions. Now we are looking to clear some code, starting with removing non used functions/methods.
We are thinking to add a few lines of code using the debug_backtrace()
to each method, to log into a file or database, the name of the class/method + from where it was called.
Since the application is really big, I want know if is there a better way to do that, before I go in and start editing hundred of functions. Any advice?
xdebug's code coverage tools allow you to test which lines of code are actually being executed, without needing to put trace statements in all of the functions/methods.
There are no php.ini settings that control the creation of code coverage statistics. Gathering of the statistics is turned on and off by PHP function at runtime, allowing for a fine-grained levelof control over which pieces of code to create coverage statistics for.
To start gathering code coverage statistics, use xdebug_start_code_coverage()
.
To stop gathering statistics, use xdebug_stop_code_coverage()
.
xdebug
keeps the gathered statistics in memory while the script is executed, so you can turn on and off code coverage within a script as often as you like. To retrieve the collected statistics as an array, use xdebug_get_code_coverage()
.
You have a lot of freedom in how you implement it, but it sounds like you have a single big script, in which case just start it at the top, stop it all exit points, and print out the coverage before it actually exits.
The Zend Developer Zone has a good introduction to doing such things, it begins with Part One: Introducing xdebug.
If you prefer to do Static Analysis, the OWASP boys keep a good list of tools along with pros and cons of each one on their Source Code Analysis Tools page that detail both Open Source or Free as well as Commercial Tools.
I suggest checking out some, like, PHP Mess Detector (PHPMD) it is pretty similar to PDepend, and provides reporting for bugs, unused code and overly complex functionality. It's very useful for identifying spots that could use some refactoring.
PHPMD can be invoked from the command line like so:
phpmd {directory} {report format} {rules}
$ phpmd . text codesize,unusedcode
/src/xxxxxx/api/src/base/Model.php:75 The method __toArray() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10.
/src/xxxxxx/api/src/controller/Users.php:235 Avoid unused parameters such as '$request'.
/src/xxxxxx/api/src/controller/Users.php:246 Avoid unused local variables such as '$x'.
/src/xxxxxx/api/src/controller/Users.php:246 Avoid unused local variables such as '$undeclared'.
/src/xxxxxx/api/src/base/Controller.php - Unexpected token: [, line: 88, col: 56, file: /src/xxxxxx/api/src/base/Controller.php.
PHP CodeSniffer is probably the best tool at the moment; ridiculously easy to set up, and the default standards are easy to extend or replace with your own. Because of this, it's a no-brainer for teams consisting of more than a few people. It also ships with PSR-1 and PSR-2 standards, so maintaining an open-source, PSR-compatible project is just a matter of installing and running it against your project (and, of course, following its advice). It also has the ability to tokenize JavaScript and CSS.
PHPCS is easily invoked from the command line via:
phpcs --standard={comma-separated rulesets} {directory}
With PHPCS' output, it is trivial to maintain style guidelines in any project. Example output from the same project:
$ phpcs --standard=PSR1,PSR2 .
FILE: /src/xxxx/api/src/exception/Xxx.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
15 | ERROR | The closing brace for the class must go on the next line after | | the body
--------------------------------------------------------------------------------
Time: 0 seconds, Memory: 5.25Mb
Google can educate you further on more options than we have room to discuss here. Hopefully this gets you on the correct path for you project.