4

I have a very big PHP project with i think a lot of useless stuffs .

Do you have some tips or maybe some tools to detect useless part of code or useless files ?

4
  • 3
    Drink a lot of coffee. Commented Jan 5, 2017 at 9:34
  • 2
    phpdcd (PHP Dead Code Detector) isn't perfect, but can go some way to assessing dead code Commented Jan 5, 2017 at 9:44
  • Depending on how your code is written, it may be easy to find dead code using phpcd... or it may be impossible to automate and you'll just have to do it manually with lots of hard work. I've been through them both. If you're doing it manually, make sure that (1) you use a good version control system so you can roll-back if you break things and (2) take it step-by-step: find small bits to delete rather than trying to do too much at once. Once you've tidied up the small stuff, the bigger fixes will become more obvious, but the small stuff gets in the way and makes it hard to see the big picture Commented Jan 5, 2017 at 10:29
  • Also worth noting that if you're using a good quality IDE such as PHPStorm or NetBeans, then the IDE will point out code that it can be certain will never run. This is similar to the results you'll get from phpdcd. However my experience with old badly written and bloated PHP code is that quite often the way the code is written doesn't allow static analysis tools like this to actually give useful results, so you will probably have to do it the hard way and just work through it manually. Good luck! Commented Jan 5, 2017 at 11:58

2 Answers 2

3

PHP Mess Detector (PHPMD):

  • Possible bugs;
  • Suboptimal code;
  • Overcomplicated expressions;
  • Unused parameters, methods, properties.

PHPMD will show to you all mess created in your code. It's also show the cyclomatic complexity of your codem which will let you do a few code optimizations.

PHP Depend

PHPMD is a spin-off of PHP Depend. PHP Depend will show to you better metrics and graphics of your software than PHPMD. A really powerfull tool, better than PHPMD for optimization, but with a different purpose.

Mark Baker also talked about PHP Dead Code Detector (PHPDCD). IDK the project, but seems very similar to PHPMD.

Sign up to request clarification or add additional context in comments.

2 Comments

pdpdcd is (ironically) a dead project and you are correct that phpmd's capabilities of detecting unused methods and parameters do cover most of phpdcd's functionality, so you can use phpmd instead. But also, most good IDEs can do this kind of static code analysis without even needing to use a commandline tool, so phpmd may not be needed at all if the OP is using a decent IDE. Also for old crufty projects like this, I've found that phpmd tends to throw so many warnings that it's difficult to see the wood from the trees (ie because basically all the code is sub-optimal!).
@Simba that's right. PHPMD also has the capability to use just a single rule set (which prevent error from old projects). I have Zend IDE also (thanks to my zend certification) and I still use the PHPMD, there's a lot of thing what I can do combining both.
1

Nowadays, your best option for unused PHP code is probably PHPStan extension called Dead Code Detector:

It can detect dead cycles, supports popular PHP libraries (like Symfony, Doctrine, PHPUnit, etc.) and can even remove the dead code by itself. You can customize it to your needs and use it in your CI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.