Using array keys without quotes, how to fix it in a big project?

I started a project using many unquoted arrays.

And now I have problems with this method, I didn't know it was bad when I started my project. I finally wanted to display E_NOTICES errors for reasons, but it crashes because the log is overloaded with millions of type notifications PHP Notice: Use of undefined constant message - assumed 'key'

.

So to fix this, I could add quotes to my keys throughout my project, but there are so many! Is there a way to achieve this with an algorithm or something to fix my code? I want to replace any undefined constant with the quoted string, EG: $my_array[key]

with $my_array['key']

.

Thank.

EDIT: I was able to capture all the declarations with rejex, for example:

\[([^0-9\$\'\"\]])([^\'\"\]]*)\]

to \[\'\1\2\'\]

But that's not enough, there are many situations where no quotes are used without parentheses, EG:

array_key_exists(unquotedKey,$array)

$array['key'] = array( unquotedKey => array(96,56) );

etc...

I could fix all situations with regex, but I think I will have a lot of problems to deal with this well and sometimes the keys from my arrays are really constant and it shouldn't be specified! If anyone has a better solution this will help me a lot.

The ideal solution would be to be able to get my code after replacing PHP undefined with constants with quoted strings , is this possible? It does this every time I compile, it is possibly stored temporarily.

+3


source to share


1 answer


I am using Notepad ++ which has search and replace functionality in files (Ctrl + Shift + F). In regex mode, you can use

Search:

\$my_array\[([^\'\"]+)\]

      

Replace



\$my_array\[\'$1\'\]

      

The search searches for anything in the square brackets of the array key where there is no " or, which indicates that the declaration is already valid.

Select your project directory and then click "Replace in files". Make sure the entire project is copied first if something goes wrong.

+1


source







All Articles