├── .gitignore ├── composer.json ├── convert ├── convert.php └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dg/php54-arrays", 3 | "type": "project", 4 | "description": "Command-line script to convert between array() and PHP 5.4's short syntax []", 5 | "license": ["BSD-3-Clause"], 6 | "authors": [ 7 | { 8 | "name": "David Grudl", 9 | "homepage": "https://davidgrudl.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0" 14 | }, 15 | "bin": ["convert"] 16 | } 17 | -------------------------------------------------------------------------------- /convert: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | isFile() && in_array($file->getExtension(), ['php', 'phpt', 'phtml'], true); 21 | }); 22 | } else { 23 | echo "Path $path not found.\n"; 24 | die(1); 25 | } 26 | 27 | foreach ($iterator as $file) { 28 | echo $file; 29 | $orig = file_get_contents($file); 30 | $res = $convert($orig); 31 | if ($orig !== $res) { 32 | echo " (changed)"; 33 | file_put_contents($file, $res); 34 | } 35 | echo "\n"; 36 | } 37 | 38 | } elseif (defined('STDIN') && (fstat(STDIN)['size'])) { 39 | $orig = file_get_contents('php://stdin'); 40 | echo $convert($orig); 41 | 42 | } else { 43 | echo " 44 | Convertor for PHP 5.4 arrays 45 | ---------------------------- 46 | Usage: {$args[0]} [-r|--reverse] [ | ] (or STDIN is used) 47 | "; 48 | die(1); 49 | } 50 | 51 | 52 | /** 53 | * Converts array() to [] 54 | * @param string 55 | * @return string 56 | */ 57 | function convertArraysToSquareBrackets($code) 58 | { 59 | $out = ''; 60 | $brackets = []; 61 | $tokens = token_get_all($code); 62 | 63 | for ($i = 0; $i < count($tokens); $i++) { 64 | $token = $tokens[$i]; 65 | if ($token === '(') { 66 | $brackets[] = false; 67 | 68 | } elseif ($token === ')') { 69 | $token = array_pop($brackets) ? ']' : ')'; 70 | 71 | } elseif (is_array($token) && $token[0] === T_ARRAY) { 72 | $a = $i + 1; 73 | if (isset($tokens[$a]) && $tokens[$a][0] === T_WHITESPACE) { 74 | $a++; 75 | } 76 | if (isset($tokens[$a]) && $tokens[$a] === '(') { 77 | $i = $a; 78 | $brackets[] = true; 79 | $token = '['; 80 | } 81 | } 82 | $out .= is_array($token) ? $token[1] : $token; 83 | } 84 | return $out; 85 | } 86 | 87 | /** 88 | * Converts [] to array() 89 | * @param string 90 | * @return string 91 | * @author Honza Novák (http://honzanovak.com) 92 | */ 93 | function convertSquareBracketsToArrays($code) 94 | { 95 | $out = ''; 96 | $brackets = []; 97 | $ignoreBracket = false; 98 | foreach (token_get_all($code) as $token) { 99 | if ($token === '[') { 100 | $brackets[] = !$ignoreBracket; 101 | $token = $ignoreBracket ? '[' : 'array('; 102 | } elseif ($token == ']'){ 103 | $token = array_pop($brackets) ? ')' : ']'; 104 | } 105 | if (!is_array($token) || $token[0] !== T_WHITESPACE) { 106 | $ignoreBracket = (in_array($token, [')', ']', '}']) 107 | || (is_array($token) && in_array($token[0], [T_VARIABLE, T_STRING, T_STRING_VARNAME]))); 108 | } 109 | $out .= is_array($token) ? $token[1] : $token; 110 | } 111 | return $out; 112 | } 113 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | PHP 5.4 Short Arrays Converter [] 2 | ================================= 3 | 4 | Command-line script to convert between `array()` and PHP 5.4's short syntax `[]` (and vice versa). 5 | 6 | It uses native PHP tokenizer, so conversion is safe. 7 | The script was successfully tested against thousands of PHP files. 8 | 9 | Usage 10 | ----- 11 | 12 | To convert all `*.php` and `*.phpt` files in whole directory recursively or to convert a single file use: 13 | 14 | ``` 15 | php convert.php 16 | ``` 17 | 18 | To convert source code from STDIN and print the output to STDOUT use: 19 | 20 | ``` 21 | php convert.php < input.php > output.php 22 | ``` 23 | 24 | To convert short syntax `[]` to older long syntax `array()` use option `--reverse`: 25 | 26 | ``` 27 | php convert.php --reverse 28 | ``` 29 | --------------------------------------------------------------------------------