PHP 7.2 upgraded undefined constant errors from a notice to a warning, with advice that in future they will return a full-on error instead.
I am trying to identify a way to fix these via scripting, ideally via a regex that I can run to parse each PHP file on a site, find all offending bits of code, and fix them.
I've found multiple examples of how to fix one variant, but none for another, and it's that one that I'm looking for help with.
Here's an example file:
<?php $array[foo] = "bar"; // this should become // $array['foo'] = "bar" echo "hello, my name is $array[foo] and it's nice to meet you"; // would need to become // echo "hello, my name is " . $array['foo'] . " and it's nice to meet you"; ?>
I've seen a lot of options to identify and change the first type, but none for the second, where the undefined constant is within a string. In that instance the parser would need to:
- Find the entire variable, end quotes beforehand, put a . either side, and then reopen quotes afterwards
Edit: ideally one regexp would deal with both examples in the sample code in one pass - i.e. add the ticks, and also add the quotes/dots if it identifies it’s within a string.