Search code examples
phpphpmd

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


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?


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.