4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
David 'the bald ginger'
  • 1,296
  • 3
  • 20
  • 38
  • I would say this is impossible. things like $class->$variable() are legal. and _get() there would be very hard to work with. I would say its a manual job rather than an automated one – exussum Jul 22 '13 at 06:24
  • Thank you for the comment. I see from the answers as well that it does not seem possible. :( – David 'the bald ginger' Jul 23 '13 at 05:39
  • Regarding the dupe: you are asking "how to dig a hole? Can I use a hammer?" and people tell you "You can't. Hammer ain't no good for that. But here is a list of tools than can dig a hole" to which you reply: "but I want to use the hammer". – Gordon Jul 23 '13 at 06:44
  • Hi Gordon, I am sorry if all questions seem as simple to you that the one can answer the other one. I wanted to know specifically about a solution concerning PHP Mess detector; even if similar solutions exists using other tools. PS: The motivation for the question is finding Public functions BUT the question was about writing a rule with a project wide context. – David 'the bald ginger' Jul 23 '13 at 06:53
  • I don't see any value in reopening the question when the specific answer is "you can't. It's the wrong tool". I'll leave it to another mod to handle your flag, so you get a second opinion. But to me [it's a classic XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Gordon Jul 23 '13 at 07:13
  • That is the beauty of Stackoverflow - we can live to disagree. :) Could you please award the bounty of 50 reputation points to Kulikov Alexei? – David 'the bald ginger' Jul 23 '13 at 07:21
  • No, I can't. I had to remove the bounty (you'll get the rep back) in order to close the question. Should your question get reopened, you'll have to award it again manually. On a side note, code coverage tells you which methods got called in a UnitTest, which is not necessarily the same as dead code. You can very much have code coverage that covers dead code. So the answer you accepted and want to award the bounty to isn't even answering the X nor the Y. – Gordon Jul 23 '13 at 07:30
  • Thank you Gordon for your feedback. I will take it into consideration for future questions&answers. – David 'the bald ginger' Jul 23 '13 at 07:36

2 Answers2

2

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.

2

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.

Alexander Olsson
  • 1,908
  • 1
  • 15
  • 24