From 8b16eceb00af3705eac34194d1246648e6b3dbd6 Mon Sep 17 00:00:00 2001 From: Andrey Vasyliev Date: Fri, 31 Oct 2014 12:47:16 +0200 Subject: [PATCH 1/8] Added git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file From 0beb67507f843e113e10f921da2ed08af189a8cc Mon Sep 17 00:00:00 2001 From: Andrey Vasyliev Date: Fri, 31 Oct 2014 12:47:44 +0200 Subject: [PATCH 2/8] Applied PSR standard formatting --- README.md | 9 ++- array-converter.php | 147 ++++++++++++++++++++++++++++++++++++++++++++ convert.php | 144 ------------------------------------------- 3 files changed, 153 insertions(+), 147 deletions(-) create mode 100755 array-converter.php delete mode 100644 convert.php diff --git a/README.md b/README.md index fb6b73c..6de196b 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,14 @@ Command-line script to convert PHP's `array()` syntax to PHP 5.4's short array s 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. +Installation via Composer +=========== + Usage ================================ - Usage: php convert.php [-w] + Usage: php array-converter.php [-w] 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. @@ -19,7 +22,7 @@ In case of any error, an error message is written to STDERR and the script exits Use `find` to convert a whole directory recursively: - find -name "*.php" -exec php "convert.php" -w "{}" \; + find -name "*.php" -exec php "array-converter.php" -w "{}" \; In case you don't trust the script yet, you can even perform a syntax check after conversion: @@ -28,4 +31,4 @@ In case you don't trust the script yet, you can even perform a syntax check afte Thanks to ================================ -Thanks to [Lebenslauf.com](https://lebenslauf.com) (German CV editor) for sponsoring the development. +Thanks to [Lebenslauf.com](https://lebenslauf.com) (German CV editor) for sponsoring the development. \ No newline at end of file diff --git a/array-converter.php b/array-converter.php new file mode 100755 index 0000000..7882c84 --- /dev/null +++ b/array-converter.php @@ -0,0 +1,147 @@ + + */ + + +// - - - - - HANDLE COMMAND LINE ARGUMENTS - - - - - + +$filePath = null; +$saveFile = false; + +if ($argc > 3) { + file_put_contents('php://stderr', 'Usage: php convert.php [-w] ' . "\n"); + exit(1); +} +for ($i = 1; $i < $argc; ++$i) { + if ($argv[$i] && $argv[$i][0] == '-') { + $saveFile = ($argv[$i] == '-w'); + } else { + $filePath = $argv[$i]; + } +} + +if (!$filePath) { + file_put_contents('php://stderr', 'Usage: php array-converter.php [-w] ' . "\n"); + exit(1); +} elseif (!file_exists($filePath)) { + file_put_contents('php://stderr', 'File "' . $filePath . '" not found.' . "\n"); + exit(1); +} + + +// - - - - - READ ORIGINAL CODE - - - - - + +$code = file_get_contents($filePath); +$tokens = token_get_all($code); + + +// - - - - - PARSE CODE - - - - - + +$replacements = array(); +$offset = 0; +for ($i = 0; $i < count($tokens); ++$i) { + // Keep track of the current byte offset in the source code + $offset += strlen(is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]); + + // T_ARRAY could either mean the "array(...)" syntax we're looking for + // or a type hinting statement ("function(array $foo) { ... }") + if (is_array($tokens[$i]) && $tokens[$i][0] === T_ARRAY) { + // Look for a subsequent opening bracket ("(") to be sure we're actually + // looking at an "array(...)" statement + $isArraySyntax = false; + $subOffset = $offset; + for ($j = $i + 1; $j < count($tokens); ++$j) { + $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); + + if (is_string($tokens[$j]) && $tokens[$j] == '(') { + $isArraySyntax = true; + break; + } elseif (!is_array($tokens[$j]) || $tokens[$j][0] !== T_WHITESPACE) { + $isArraySyntax = false; + break; + } + } + + if ($isArraySyntax) { + // Replace "array" and the opening bracket (including preceeding whitespace) with "[" + $replacements[] = array( + 'start' => $offset - strlen($tokens[$i][1]), + 'end' => $subOffset, + 'string' => '[', + ); + + // Look for matching closing bracket (")") + $subOffset = $offset; + $openBracketsCount = 0; + for ($j = $i + 1; $j < count($tokens); ++$j) { + $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); + + if (is_string($tokens[$j]) && $tokens[$j] == '(') { + ++$openBracketsCount; + } elseif (is_string($tokens[$j]) && $tokens[$j] == ')') { + --$openBracketsCount; + + if ($openBracketsCount == 0) { + // Replace ")" with "]" + $replacements[] = array( + 'start' => $subOffset - 1, + 'end' => $subOffset, + 'string' => ']', + ); + break; + } + } + } + } + } +} + + +// - - - - - UPDATE CODE - - - - - + +// Apply the replacements to the source code +$offsetChange = 0; +foreach ($replacements as $replacement) { + $code = substr_replace( + $code, + $replacement['string'], + $replacement['start'] + $offsetChange, + $replacement['end'] - $replacement['start'] + ); + $offsetChange += strlen($replacement['string']) - ($replacement['end'] - $replacement['start']); +} + + +// - - - - - OUTPUT/WRITE NEW CODE - - - - - + +if ($saveFile) { + file_put_contents($filePath, $code); +} else { + print $code; +} \ No newline at end of file diff --git a/convert.php b/convert.php deleted file mode 100644 index be299d5..0000000 --- a/convert.php +++ /dev/null @@ -1,144 +0,0 @@ - - */ - - -// - - - - - HANDLE COMMAND LINE ARGUMENTS - - - - - - -$filePath = null; -$saveFile = false; - -if($argc > 3) { - file_put_contents('php://stderr', 'Usage: php convert.php [-w] ' . "\n"); - exit(1); -} -for($i = 1; $i < $argc; ++$i) { - if($argv[$i] && $argv[$i][0] == '-') { - $saveFile = ($argv[$i] == '-w'); - } else { - $filePath = $argv[$i]; - } -} - -if(!$filePath) { - file_put_contents('php://stderr', 'Usage: php convert.php [-w] ' . "\n"); - exit(1); -} elseif(!file_exists($filePath)) { - file_put_contents('php://stderr', 'File "' . $filePath . '" not found.' . "\n"); - exit(1); -} - - -// - - - - - READ ORIGINAL CODE - - - - - - -$code = file_get_contents($filePath); -$tokens = token_get_all($code); - - -// - - - - - PARSE CODE - - - - - - -$replacements = array(); -$offset = 0; -for($i = 0; $i < count($tokens); ++$i) { - // Keep track of the current byte offset in the source code - $offset += strlen(is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]); - - // T_ARRAY could either mean the "array(...)" syntax we're looking for - // or a type hinting statement ("function(array $foo) { ... }") - if(is_array($tokens[$i]) && $tokens[$i][0] === T_ARRAY) { - // Look for a subsequent opening bracket ("(") to be sure we're actually - // looking at an "array(...)" statement - $isArraySyntax = false; - $subOffset = $offset; - for($j = $i + 1; $j < count($tokens); ++$j) { - $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); - - if(is_string($tokens[$j]) && $tokens[$j] == '(') { - $isArraySyntax = true; - break; - } elseif(!is_array($tokens[$j]) || $tokens[$j][0] !== T_WHITESPACE) { - $isArraySyntax = false; - break; - } - } - - if($isArraySyntax) { - // Replace "array" and the opening bracket (including preceeding whitespace) with "[" - $replacements[] = array( - 'start' => $offset - strlen($tokens[$i][1]), - 'end' => $subOffset, - 'string' => '[', - ); - - // Look for matching closing bracket (")") - $subOffset = $offset; - $openBracketsCount = 0; - for($j = $i + 1; $j < count($tokens); ++$j) { - $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); - - if(is_string($tokens[$j]) && $tokens[$j] == '(') { - ++$openBracketsCount; - } elseif(is_string($tokens[$j]) && $tokens[$j] == ')') { - --$openBracketsCount; - - if($openBracketsCount == 0) { - // Replace ")" with "]" - $replacements[] = array( - 'start' => $subOffset - 1, - 'end' => $subOffset, - 'string' => ']', - ); - break; - } - } - } - } - } -} - - -// - - - - - UPDATE CODE - - - - - - -// Apply the replacements to the source code -$offsetChange = 0; -foreach($replacements as $replacement) { - $code = substr_replace($code, $replacement['string'], $replacement['start'] + $offsetChange, $replacement['end'] - $replacement['start']); - $offsetChange += strlen($replacement['string']) - ($replacement['end'] - $replacement['start']); -} - - -// - - - - - OUTPUT/WRITE NEW CODE - - - - - - -if($saveFile) { - file_put_contents($filePath, $code); -} else { - print $code; -} - -?> \ No newline at end of file From 4c93b88352e3e03c2326d8e285fa2dcb729348ea Mon Sep 17 00:00:00 2001 From: Andrey Vasyliev Date: Fri, 31 Oct 2014 12:49:37 +0200 Subject: [PATCH 3/8] Bin script added, permissions changed --- array-converter.php | 0 bin/array-converter.php | 4 ++++ 2 files changed, 4 insertions(+) mode change 100755 => 100644 array-converter.php create mode 100755 bin/array-converter.php diff --git a/array-converter.php b/array-converter.php old mode 100755 new mode 100644 diff --git a/bin/array-converter.php b/bin/array-converter.php new file mode 100755 index 0000000..d6aec70 --- /dev/null +++ b/bin/array-converter.php @@ -0,0 +1,4 @@ +#!/usr/bin/env php + Date: Fri, 31 Oct 2014 12:53:21 +0200 Subject: [PATCH 4/8] Composer.json added --- bin/{array-converter.php => array-converter} | 0 composer.json | 31 ++++++++++++++++++++ 2 files changed, 31 insertions(+) rename bin/{array-converter.php => array-converter} (100%) create mode 100644 composer.json diff --git a/bin/array-converter.php b/bin/array-converter similarity index 100% rename from bin/array-converter.php rename to bin/array-converter diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1ebcd1c --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "thomasbachem/php-short-array-syntax-converter", + "description": "Command-line script to convert PHP's array() syntax to PHP 5.4's short array syntax [] using PHP's built-in tokenizer.", + "type": "library", + "homepage": "https://github.com/a-vasyliev/php-short-array-syntax-converter", + "keywords": [ + "short", + "array", + "converter", + "php" + ], + "authors": [ + { + "name": "Thomas Bachem", + "email": "mail@thomasbachem.com", + "homepage": "http://thomasbachem.com" + }, + { + "name": "Andrii Vasyliev", + "email": "contact@andrey-vasiliev.com", + "homepage": "http://andrey-vasiliev.com" + } + ], + "minimum-stability": "dev", + "require": { + "php": ">=5.4" + }, + "bin": [ + "bin/array-converter" + ] +} From 885350b6244b7e72a5c69f106fe0e6566c5c6aaf Mon Sep 17 00:00:00 2001 From: Andrey Vasyliev Date: Fri, 31 Oct 2014 12:59:31 +0200 Subject: [PATCH 5/8] Readme updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6de196b..843393a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ By relying on the PHP tokenizer, nothing but the array syntax itself will be alt Installation via Composer =========== - +Just add "thomasbachem/php-short-array-syntax-converter": "dev-master" to your require(-dev) section and run update command on Composer. Usage ================================ From 67ceddb584d7de630ca69f1467d81457a37a05bf Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Fri, 31 Oct 2014 13:03:24 +0200 Subject: [PATCH 6/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 843393a..993af63 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ By relying on the PHP tokenizer, nothing but the array syntax itself will be alt Installation via Composer =========== -Just add "thomasbachem/php-short-array-syntax-converter": "dev-master" to your require(-dev) section and run update command on Composer. +Just add `"thomasbachem/php-short-array-syntax-converter": "dev-master"` to your require(-dev) section and run update command on Composer. Usage ================================ @@ -31,4 +31,4 @@ In case you don't trust the script yet, you can even perform a syntax check afte Thanks to ================================ -Thanks to [Lebenslauf.com](https://lebenslauf.com) (German CV editor) for sponsoring the development. \ No newline at end of file +Thanks to [Lebenslauf.com](https://lebenslauf.com) (German CV editor) for sponsoring the development. From 34698269b82c10d2d817d5788cd0c3800dad6af3 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Fri, 31 Oct 2014 15:56:36 +0200 Subject: [PATCH 7/8] Update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 723ef36..485dee6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.idea \ No newline at end of file +.idea From 59fde6c9a45543a39f2ce4787c5c1d83c9f50899 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Fri, 31 Oct 2014 15:57:10 +0200 Subject: [PATCH 8/8] Update array-converter --- bin/array-converter | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/array-converter b/bin/array-converter index d6aec70..4d66a68 100755 --- a/bin/array-converter +++ b/bin/array-converter @@ -1,4 +1,4 @@ #!/usr/bin/env php