r/PHPhelp • • 16d 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

11 comments sorted by

14

u/TemporarySun314 16d ago

Seems like a task for which an AI agent is very useful, and should do good work.

I think rector should also have some rules for that kind of migration, but that might be more work than using AI...

3

u/Tomas_Votruba 16d ago edited 15d ago

Rector + agents are able to handle this in ~90 % cases. Give them a go.

If you struggle, we can help: https://getrector.com/hire-team

(also great way to support open source development of ongoing Rector ecosystem)

2

u/Takeoded 16d ago

Reddit Rich Text Editor, in its infinite wisdom, added a real nbsp to the visible part of your url, and an url-encoded nbsp (%C2%A0) to the actual underlying hyperlink, so your link goes to https://getrector.com/hire-team%C2%A0 ... which returns HTTP 404 Not Found.

Consider trying the Markdown Editor :)

1

u/Tomas_Votruba 15d ago

I just used plain text like here, but space + ( got wrong somehow. Thanks for spotting :)

Fixed

3

u/thejester1324 16d ago

Two separate problems with different fixes.

The $row{0} offset syntax was removed in PHP 8.0 (it's in the migration guide), so that part is a search and replace to $row[0]. Don't do a blind replace on every {, though. Interpolation like {$row['name']} inside double quotes is still valid, so only the $var{...} form needs changing.

The mysql_* functions have been gone since PHP 7.0. I wouldn't count on Rector for those, so that part is hand work. The thing to watch is argument order: mysql_query($sql, $link) becomes mysqli_query($link, $sql), with the connection first. MYSQL_BOTH and MYSQL_NUM become MYSQLI_BOTH and MYSQLI_NUM as the mode argument to mysqli_fetch_array.

Grep for mysql_ and convert one call at a time so you don't swap arguments by accident.

6

u/allen_jb 16d 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 16d 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 16d ago

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

0

u/FreeLogicGate 15d 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 15d 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 14d 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.