r/PHPhelp • • 27d ago

Patching code full of old syntax/functions

Hello,

I am patching some php code of some kind of DB web integration and the code is quite old. After giving it into phpstorm to check for problems, it showed dozens of syntax errors (mostly curly braces for accessing array elements, no big deal in changing them to squared ones) and whole functions that arent supported anymore. Mostly things as MYSQL_BOTH, MYSQL_NUM, etc. Is there a nice way to fix those issues, maybe even some kind of software, or do I need to manually rewrite those passages w/o the functions?

7 Upvotes

12 comments sorted by

View all comments

6

u/allen_jb 27d ago

I would recommend Rector over AI for this.

Rector is based on running sets of fixed, known, tested rules to transform code in a reliable way.

Note that if the code uses the old mysql_* functions (ext/mysql), I don't think Rector currently handles upgrading these. You might want to try searching for mysql shim on Packagist

ext/mysql usage should ideally be upgraded to use PDO MySQL or mysqli. Note: While the mysqli procedural functions may look similar, there are significant differences that make switching non-trivial through simple search and replace.

You may also want to look at the PHPCompatibility ruleset for CodeSniffer

If you need further help I would suggest including the PHP version the code is currently running on.

3

u/obstreperous_troll 27d ago

Rector used to have a ruleset for converting mysql_* functions to mysqli but I can't find it in recent versions anymore. No idea where it went — I imagine /u/tomas_votruba knows?

4

u/allen_jb 27d ago

According to comments on issues in the Rector repo it was breaking more than it was helping, so was removed.

0

u/FreeLogicGate 25d ago

The MySQL shim is in abandoned state for a long time now, and the author specifically marked it as abandoned to advise people against continuing to use it.

1

u/allen_jb 25d ago

As far as I'm aware there's no specific issues with dshafik's library other than that the author doesn't wish to maintain it.

There are several alternatives, which is one reason I linked to the packagist search instead of direct to a specific library.

And yes, as I stated in my reply, ideally these usages should be upgraded to use mysqli or PDO, but developers don't always live in an ideal world. I've performed upgrades of old codebases and there can be a lot to deal with all at once, so using a shim library temporarily can allow developers to quickly solve one problem (temporarily, hopefully) while they solve others.

For example, use a shim library to allow updating the rest of the code with Rector, then the shim can be replaced with modern PDO / mysqli code, taking full advantage of things like the recent addition of ->execute_query in mysqli.

As a quick addition to my original reply, I'll also add that DB calls should ideally be upgraded to use prepared queries where possible to avoid potential bugs in incorrect escaping as well as avoiding SQL injection.

0

u/FreeLogicGate 24d ago

Just from a quick scan of the packagist listing you posted, the other versions are just forks of Davey Shafik's lib.

It was a library created to help people get through a brief period of time when 7.0 was released 11 years ago, but even when it was released, it was meant to buy time for people using 7.x.. The last 7.4 release was end of life 4 years ago.

Your advice was solid, up to the point you linked to an obsolete shim for an obsolete database api on an obsolete version of PHP.