Coder Social home page Coder Social logo

andrecmatos / php-short-array-syntax-converter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thomasbachem/php-short-array-syntax-converter

0.0 0.0 0.0 17 KB

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

PHP 100.00%

php-short-array-syntax-converter's Introduction

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.

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.

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.

You can add the -w switch if you want to override the original file with the converted code.

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.

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

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

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

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

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:

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!

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 "}".

Thanks to

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

php-short-array-syntax-converter's People

Contributors

dizyart avatar thomasbachem avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.