How to write a PHP Mess Detector rule that has an extended project context, and not just at the class level?

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

It does this because public functions are usually called from other classes, and the scope of the Rule seems to be at the class level. Thus, in each class public functions are not used and therefore are part of the result.

So the question is how to write a rule with a context that is at the project level, and not just at the class level?

+4
source share
2 answers

It is not possible to get all calls to public methods simply by analyzing the source code of the project, as some of the calls can be made using

call_user_func() 

or

 $object->$method() 

I suggest you fully study the project using unit tests. When you execute them, you will have code coverage statistics. It can be presented in a readable form. You will see which methods are called and which are not used.

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

See php module testing and code coverage .

+2
source

I had the same problem and ended up analyzing dynamic code. Basically I ran my site for a certain period of time and had xdebug usage files. To parse all this, I created a small PHPAnalyzer tool that moves these files and displays statistics on all functions (or not used) that are used. Among these statistics is the number of times it was called. The tool is not really polished, and any input is appreciated.

+2
source

Source: https://habr.com/ru/post/1487452/


All Articles