Lately I have been adding new and new features to existing application and now came time when I have to play with some seriously old code. I mean - really old, forgotten and never refactored code. It’s just there, it’s working and nobody dares touching it. So before I started I need to prepare myself for the battle.
if you are in need of refactoring some critical part of the system the big chance is that this critical part doesn’t came with any tests (life, so to say). There is also a big chance that the code is basically impossible to test with unit testing - write some integration tests instead. As they say - any kind of test are better that none tests. You can use anything that works for you (for example BDD tool like behat).
install PHP_CodeSniffer, PHP Mess Detector, PHP Copy/Paste Detector and PHP_Depend (phpmd requires it so probably at this point you’ll have it installed anyway) - these tools will be very useful - trust me
you can integrate your tests/code checks with phpUnderControl that is extending CruiseControl - prepare your build file according to manual (you may want to exclude some directories for each tool, also if you code base is old I would stick with PSR-1 for the start as the code standard because PSR-2 check will likely to kill you on sight)
Bonus: some interesting results of phpmd ran just for two files :) (methods/class names blanked).
...
The class some_class has 41 methods. Consider refactoring user_art to keep number of methods under 10.
The method someMethod() has an NPath complexity of 15211861552761400491758084218783915899329999864762835394560. The configured NPath complexity threshold is 200.
The method someMethod2() has an NPath complexity of 900000. The configured NPath complexity threshold is 200.
The method someMethod3() has an NPath complexity of 119430531454390553552. The configured NPath complexity threshold is 200.
...
The class some_class2 has 3442 lines of code. Current threshold is 1000. Avoid really long classes.
The method someMethod() has an NPath complexity of 151619238808128. The configured NPath complexity threshold is 200.
...
Happy refactoring!