thomasbachem / php-short-array-syntax-converter

Command-line script to convert PHP's array() syntax to PHP 5.4's short array syntax []

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reverse process

c3c opened this issue · comments

commented

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:

  1. if token is "[" string, assume it is an array
  2. if previous token is a possible array reference, break
  3. add replacement from token to "array("
  4. 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.