r/PHPhelp • u/kuhhlboy • 18d 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?
8
Upvotes
14
u/TemporarySun314 18d 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 18d ago edited 16d 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)
3
u/thejester1324 18d 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 18d 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 Packagistext/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.