-
-
Save cordoval/9fed45851c9c4a0c3c35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Symfony\CS\FixerInterface; | |
use Symfony\CS\Tokens; | |
class ShortArraySyntaxFixer implements FixerInterface | |
{ | |
public function fix(\SplFileInfo $file, $content) | |
{ | |
$tokens = Tokens::fromCode($content); | |
for ($index = 0, $c = $tokens->count(); $index < $c; $index++) { | |
$token = $tokens[$index]; | |
if (Tokens::isKeyword($token) && T_ARRAY === $token[0] && '(' === $tokens->getNextNonWhitespace($index)) { | |
$this->fixArray($tokens, $index); | |
continue; | |
} | |
} | |
return $tokens->generateCode(); | |
} | |
private function fixArray(Tokens $tokens, &$index) | |
{ | |
$bracesLevel = 0; | |
unset($tokens[$index]); | |
$index++; | |
for ($c = $tokens->count(); $index < $c; $index++) { | |
$token = $tokens[$index]; | |
if ('(' === $token) { | |
if (0 === $bracesLevel) { | |
$tokens[$index] = '['; | |
} | |
++$bracesLevel; | |
continue; | |
} | |
if (Tokens::isKeyword($token) && T_ARRAY === $token[0] && '(' === $tokens->getNextNonWhitespace($index)) { | |
$this->fixArray($tokens, $index); | |
continue; | |
} | |
if (')' === $token) { | |
--$bracesLevel; | |
if (0 === $bracesLevel) { | |
$tokens[$index] = ']'; | |
break; | |
} | |
} | |
} | |
} | |
public function getLevel() | |
{ | |
return FixerInterface::ALL_LEVEL; | |
} | |
public function getPriority() | |
{ | |
return 0; | |
} | |
public function supports(\SplFileInfo $file) | |
{ | |
return 'php' === pathinfo($file->getFilename(), PATHINFO_EXTENSION); | |
} | |
public function getName() | |
{ | |
return 'short_array_syntax'; | |
} | |
public function getDescription() | |
{ | |
return 'PHP array\'s should use the PHP 5.4 short-syntax'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ShortArraySyntaxFixerTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @dataProvider provideExamples | |
*/ | |
public function testFix($expected, $input) | |
{ | |
$fixer = new Fixer(); | |
$file = $this->getTestFile(); | |
$this->assertEquals($expected, $fixer->fix($file, $input)); | |
} | |
public function provideExamples() | |
{ | |
return array( | |
array('<?php $x = [];', '<?php $x = array();'), | |
array('<?php $x = []; $y = [];', '<?php $x = array(); $y = array();'), | |
array('<?php $x = [ ];', '<?php $x = array( );'), | |
array('<?php $x = [\'foo\'];', '<?php $x = array(\'foo\');'), | |
array('<?php $x = [ \'foo\' ];', '<?php $x = array( \'foo\' );'), | |
array('<?php $x = [($y ? true : false)];', '<?php $x = array(($y ? true : false));'), | |
array('<?php $x = [($y ? [true] : [false])];', '<?php $x = array(($y ? array(true) : array(false)));'),// | |
array('<?php $x = [($y ? [true] : [ false ])];', '<?php $x = array(($y ? array(true) : array( false )));'), | |
array('<?php $x = [($y ? ["t" => true] : ["f" => false])];', '<?php $x = array(($y ? array("t" => true) : array("f" => false)));'), | |
array('<?php print_r([($y ? true : false)]);', '<?php print_r(array(($y ? true : false)));'), | |
array('<?php $x = [[[]]];', '<?php $x = array(array(array()));'), | |
array('<?php $x = [[[]]]; $y = [[[]]];', '<?php $x = array(array(array())); $y = array(array(array()));'), | |
); | |
} | |
private function getTestFile($filename = __FILE__) | |
{ | |
static $files = array(); | |
if (!isset($files[$filename])) { | |
$files[$filename] = new \SplFileInfo($filename); | |
} | |
return $files[$filename]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment