How to write a PHP Mess Detector rule that has a project wide context instead of only on a class level? [duplicate]

StackOverflow https://stackoverflow.com/questions/17233629

  •  01-06-2022
  •  | 
  •  

Question

I would like to write a rule to find all the public unused functions in a project. I have copied and amended the original UnusedPrivateMethod to work. But alas it works too good and finds ALL the public functions in the project.

It does so because public functions are usually called from other classes and the scope of the Rule seems to be at class level. So within each class the public functions are not used and so is part of the result.

Thus the question of how can I write a rule with a context that is on a project level instead of just at the class level?

Was it helpful?

Solution

It is not possible to get all public methods calls just by parsing your project source code as some of the calls could be made with

call_user_func()

or

$object->$method()

I suggest you cover the project with unit tests as fully as possible. When you execute them you will have code coverage statistics. It can be presented in nice easy to read form. You will see which methods are called and which are not used.

Yes, you will have to spend some time writing those unit tests. But it's totally worth it.

Take a look at php unit testing and code coverage.

OTHER TIPS

I had the same problem a while back and ended up doing dynamic code analysis for this. Basically I ran my site for a period of time and had xdebug output usage files. To parse all these I created a small tool, PHPAnalyzer which traverses these files and output statistics of all functions used (or not used). Among this statistic is the number of times it was called. The tool is not really polished, and any contribution is welcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top