How to fix associative array keys that lack single

2019-08-30 11:41发布

问题:

I've been assigned a project that have associative arrays without single quotes all over the place.

Example:

$foo[bar]

Which should be:

$foo['bar']

This generates a PHP notice and it's very bad practice.

Quoting from PHP.net:

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

And it's not good for performance because it has to search for a constant, before using 'key'

So I was wondering if someone knows a script that can achieve this without having to do it manually in a one by one basis for every file.

UPDATE:

I use Vim as an editor, and I know how to use its search and replace command in a single file, however I see two problems with this:

  1. I can't search and replace something like this because I don't think it supports regular expressions.

  2. It only works in the current opened file (as far as I know).

回答1:

Does your text editor not support global search and replace using regular expressions?

I might search for something like:

(\$[a-zA-Z0-9]+\[)([^\]]+)\]

and replace with

\1'\2']

Regular expression search+replace is really useful for broad changes to lots of source files. Although make sure you take a snapshot of your code before you make the global change and verify afterwards that everything you wanted changed was changed with no unintended side-effects.

UPDATE:

Based on OP's update that s/he is using vim, see the accepted answer of this post: Apply regular expression substitution globally to many files with a script which describes using regular expression search/replace on multiple files in vim.

The sed-like syntax suggests to me that the command you are looking for (untested) is:

:args **/*.php | argdo %s/(\$[a-zA-Z0-9]+\[)([^\]]+)\]/\1'\2'/ge | update

You might need to fiddle with that a bit, so make a backup before you go modifying all your files. I recommend trying to find a more user-friendly IDE as well.



回答2:

Does you IDE do a search and replace? If so use that. It might take some time but you need to be careful with just changing all of them in one go.



回答3:

This would be another possible solution with vim:

:%s/\[\(\h*\)\]/\['\1'\]/gc

This would change all instances of:

[foo]

To:

['foo']

And will prompt for confirmation.

The following examples would remain unchanged (as they should be):

[$foo]
["foo"]
['foo']
[123]


回答4:

Use an IDE to do a find and replace on the project. Most of them allow you to put a regex in the search string, so whenever it matches the pattern replace [ with [' and ] with '].