2017 © Pedro Peláez
 

library php-short-array-syntax-converter

Command-line script to convert or revert PHP's array() syntax to PHP 5.4's short array syntax [] using PHP's built-in tokenizer.

image

phpbsb/php-short-array-syntax-converter

Command-line script to convert or revert PHP's array() syntax to PHP 5.4's short array syntax [] using PHP's built-in tokenizer.

  • Wednesday, April 6, 2016
  • by fagundes
  • Repository
  • 2 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 34 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP 5.4 Short Array Syntax Converter

Command-line script to convert and revert PHP's array() syntax to PHP 5.4's short array syntax[] using PHP's built-in tokenizer., (*1)

By relying on the PHP tokenizer, nothing but the array syntax itself will be altered. The script was successfully tested against code bases with more than 5.000 PHP files., (*2)

Usage

Usage: php convert.php [-w] <file>

Run the script with the path of the PHP file you wish to convert as argument. This will print the converted source code to STDOUT., (*3)

You can add the -w switch if you want to override the original file with the converted code., (*4)

If you want the script to process PHP files with short open tags (<?) as well, you need to make sure that the short_open_tag setting is enabled in your php.ini file., (*5)

In case of any error, an error message is written to STDERR and the script exits with a return code of 1., (*6)

Use find to convert a whole directory recursively (on Linux/Mac):, (*7)

find <directory> -name "*.php" -exec php "convert.php" -w "{}" \;

Or on Windows (thanks to John Jablonski for suggesting):, (*8)

FOR /f "tokens=*" %a in ('dir *.php /S/B') DO php convert.php %a -w

In case you don't trust the script yet, you can even perform a syntax check after conversion:, (*9)

find <directory> -name "*.php" -exec php -l "{}" \; | grep "error:"

Revert

Usage: php revert.php [-w] <file>

Reverting has not yet been thoroughly tested, so use with extreme percaution!, (*10)

Since there is no specific token for the short array syntax, it assumes every "[" is an aray and relies on checking the previous token for a variable, object property, function return ")", nested array "]" and variable reference "}"., (*11)

Thanks to

Thanks to Lebenslauf.com (German CV editor) for sponsoring the development., (*12)

The Versions

06/04 2016

dev-master

9999999-dev https://github.com/phpbsb/php-short-array-syntax-converter

Command-line script to convert or revert PHP's array() syntax to PHP 5.4's short array syntax [] using PHP's built-in tokenizer.

  Sources   Download

The Requires

  • php >=5.3

 

php array converter short