Hacker News new | ask | show | jobs
by dkarlovi 2062 days ago
Roughly in the order you should do it. Hopefully it's at least PHP 5.3 code:

1. Definitely get a PHP IDE: https://www.jetbrains.com/phpstorm/

2. Get a step debugger and hook it in: https://xdebug.org/

3. Fix the code style: https://github.com/FriendsOfPHP/PHP-CS-Fixer

4. Run the code through some static analysis tools https://phpstan.org/ https://psalm.dev/

5. Upgrade the code with an AST fixer (might help you update to newer PHP version): https://github.com/rectorphp/rector

6. Add tests to it: https://phpunit.de/

7. Run mutation tests: https://infection.github.io/

The "0 budget" is rough, though.

3 comments

In addition:

- Make sure you have version control. So easy to do these days, so often forgotten.

- don't think that the steps above must be done in order, right now.

- dkarlovi mentions tests. Start with 1.) smoke tests like simple selenium tests or something like that. You'll find lots of vate towards it if you look and I admit it has issues but for simple projects like this that doesn't change much it should be fine. Of course of you find something better use that.

- one of the most important things about a good IDE is being able to refactor confidently so you can rename variables to something reasonable as you figure out what they really are.

- the book "refactoring legacy code" might be the best programming book I've read.

There's also "Modernizing Legacy Applications in PHP": https://leanpub.com/mlaphp (but leanpub seems to be down right now lol)
I tried searching for "refactoring legacy code" but I couldn't find it. Who's the author?
Sorry, I misremembered:

"Working Effectively with Legacy Code" is the name. The author is Michael C. Feathers I think.

There seems to be a new version coming out in the next couple of months (ISBN 9780136657125) but I can't tell if it's a new edition or just a redesign.
If you do #1, you'll get #3 and #4 for free, and full integration of #2 and #6 :-) Best 80 bucks you can ever spend if you write PHP.

My basic procedure when inheriting legacy code is:

- Load the project in phpstorm

- Perform autoformatting so it becomes readable

- Run code inspection to find any obvious issues the original author missed

- Write tests in phpstorm, if not already exists

- Run the code with xdebug, step through it

All good advice for the average PHP developer, but I think it is overkill for someone that is starting with PHP just to keep alive a very old app.

Adding tests for an app with no documentation requires a lot of effort.

Of course, that's why the list is sorted, he might never make it pass 1 :)
Yes. Upgrading really old php application (v4? really?) to v8 is such a big step it will need much dedication, so many things changed between 4 and 5 its not easy. And even upgrading 5 to 7 may break so many things. I would not like to switch with him.
> Adding tests for an app with no documentation requires a lot of effort.

code tests yes. Assuming it's a web app, probably not too hard. Get something that records the browser interactions, and do the common tasks people do (log in, click links, make a report, etc). Having general things like that automated to ensure you can run them repeatedly to make sure basic stuff didn't break unexpectedly will help provide some confidence when making changes.