Reverse process
c3c opened this issue · comments
Is there a way to do the reverse process?
Converting the shorthand syntax automatically to the old array()-style?
Kind regards,
Cedric
That's a bit more difficult, as the PHP tokenizer doens't expose the bracket syntax as T_ARRAY tokens, just as strings. So you have to watch out that it's really an array.
Possible solution is to assume that "[" character is array by default and check preceding tokens if it isn't. I'm just having trouble finding a complete reference of "[" usage in array-index context (to do the "not-array" token check). Can anyone please help me with that?
Some pseudo:
- if token is "[" string, assume it is an array
- if previous token is a possible array reference, break
- add replacement from token to "array("
- find matching "]" bracket and add replacement to ")"
As far as I know, "[" can be used after these tokens:
- variable
- function return value (since 5.4)
- object property
- ...??? (help 😄)
I will post a patch for the reverse as soon as I'm done with it.