123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\ArrayNotation;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Jared Henderson <jared@netrivet.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer>
- */
- final class TrimArraySpacesFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php $foo = array("foo");',
- '<?php $foo = array( "foo" );',
- ];
- yield [
- '<?php $foo = ["foo"];',
- '<?php $foo = [ "foo" ];',
- ];
- yield [
- '<?php $foo = array();',
- '<?php $foo = array( );',
- ];
- yield [
- '<?php $foo = [];',
- '<?php $foo = [ ];',
- ];
- yield [
- '<?php $foo = array("foo", "bar");',
- '<?php $foo = array( "foo", "bar" );',
- ];
- yield [
- '<?php $foo = array("foo", "bar", );',
- '<?php $foo = array( "foo", "bar", );',
- ];
- yield [
- '<?php $foo = ["foo", "bar", ];',
- '<?php $foo = [ "foo", "bar", ];',
- ];
- yield [
- "<?php \$foo = array('foo', 'bar');",
- "<?php \$foo = array(\t'foo', 'bar'\t);",
- ];
- yield [
- "<?php \$foo = array('foo', 'bar');",
- "<?php \$foo = array( \t 'foo', 'bar'\t );",
- ];
- yield [
- '<?php $foo = array("foo", "bar");',
- '<?php $foo = array( "foo", "bar" );',
- ];
- yield [
- '<?php $foo = ["foo", "bar"];',
- '<?php $foo = [ "foo", "bar" ];',
- ];
- yield [
- '<?php $foo = ["foo", "bar"];',
- '<?php $foo = [ "foo", "bar" ];',
- ];
- yield [
- "<?php \$foo = ['foo', 'bar'];",
- "<?php \$foo = [\t'foo', 'bar'\t];",
- ];
- yield [
- "<?php \$foo = ['foo', 'bar'];",
- "<?php \$foo = [ \t \t 'foo', 'bar'\t \t ];",
- ];
- yield [
- '<?php $foo = array("foo", "bar"); $bar = array("foo", "bar");',
- '<?php $foo = array( "foo", "bar" ); $bar = array( "foo", "bar" );',
- ];
- yield [
- '<?php $foo = ["foo", "bar"]; $bar = ["foo", "bar"];',
- '<?php $foo = [ "foo", "bar" ]; $bar = [ "foo", "bar" ];',
- ];
- yield [
- '<?php $foo = array("foo" => "bar");',
- '<?php $foo = array( "foo" => "bar" );',
- ];
- yield [
- '<?php $foo = ["foo" => "bar"];',
- '<?php $foo = [ "foo" => "bar" ];',
- ];
- yield [
- '<?php $foo = array($y ? true : false);',
- '<?php $foo = array( $y ? true : false );',
- ];
- yield [
- '<?php $foo = [$y ? true : false];',
- '<?php $foo = [ $y ? true : false ];',
- ];
- yield [
- '<?php $foo = array(array("foo"), array("bar"));',
- '<?php $foo = array( array( "foo" ), array( "bar" ) );',
- ];
- yield [
- '<?php $foo = [["foo"], ["bar"]];',
- '<?php $foo = [ [ "foo" ], [ "bar" ] ];',
- ];
- yield [
- '<?php function(array $foo = array("bar")) {};',
- '<?php function(array $foo = array( "bar" )) {};',
- ];
- yield [
- '<?php function(array $foo = ["bar"]) {};',
- '<?php function(array $foo = [ "bar" ]) {};',
- ];
- yield [
- '<?php $foo = array(function() {return "foo";});',
- '<?php $foo = array( function() {return "foo";} );',
- ];
- yield [
- '<?php $foo = [function() {return "foo";}];',
- '<?php $foo = [ function() {return "foo";} ];',
- ];
- yield [
- "<?php \$foo = [function( \$a = \tarray('foo') ) { return 'foo' ;}];",
- "<?php \$foo = [ function( \$a = \tarray( 'foo' ) ) { return 'foo' ;} ];",
- ];
- yield [
- "<?php \$foo = array(function( ) {\treturn 'foo' \t;\t});",
- "<?php \$foo = array( function( ) {\treturn 'foo' \t;\t} );",
- ];
- yield [
- "<?php \$foo = [function()\t{\t \treturn 'foo';\t}];",
- "<?php \$foo = [ function()\t{\t \treturn 'foo';\t} ];",
- ];
- yield [
- "<?php \$foo \t = array(function(\$a,\$b,\$c=array(3, 4))\t{\t \treturn 'foo';\t});",
- "<?php \$foo \t = array( function(\$a,\$b,\$c=array( 3, 4 ))\t{\t \treturn 'foo';\t} );",
- ];
- yield [
- '<?php $foo = array($bar->method(), Foo::doSomething());',
- '<?php $foo = array( $bar->method(), Foo::doSomething() );',
- ];
- yield [
- '<?php $foo = [$bar->method(), Foo::doSomething()];',
- '<?php $foo = [ $bar->method(), Foo::doSomething() ];',
- ];
- yield [
- "<?php \$foo = [\$bar->method( \$a,\$b, \$c,\t\t \$d ), Foo::doSomething()];",
- "<?php \$foo = [ \$bar->method( \$a,\$b, \$c,\t\t \$d ), Foo::doSomething() ];",
- ];
- yield [
- "<?php \$foo =\t array(\$bar->method( \$a,\$b, \$c,\t\t \$d ), \$bar -> doSomething( ['baz']));",
- "<?php \$foo =\t array( \$bar->method( \$a,\$b, \$c,\t\t \$d ), \$bar -> doSomething( [ 'baz']) );",
- ];
- yield [
- '<?php $foo = array(array("foo"), array("bar"));',
- '<?php $foo = array( array("foo"), array("bar") );',
- ];
- yield [
- '<?php $foo = [["foo"], ["bar"]];',
- '<?php $foo = [ ["foo"], ["bar"] ];',
- ];
- yield [
- '<?php $foo = array(array("foo"), array("bar"));',
- '<?php $foo = array(array( "foo" ), array( "bar" ));',
- ];
- yield [
- '<?php $foo = [["foo"], ["bar"]];',
- '<?php $foo = [[ "foo" ], [ "bar" ]];',
- ];
- yield [
- '<?php $foo = array(/* empty array */);',
- '<?php $foo = array( /* empty array */ );',
- ];
- yield [
- '<?php $foo = [/* empty array */];',
- '<?php $foo = [ /* empty array */ ];',
- ];
- yield [
- '<?php someFunc(array(/* empty array */));',
- '<?php someFunc(array( /* empty array */ ));',
- ];
- yield [
- '<?php someFunc([/* empty array */]);',
- '<?php someFunc([ /* empty array */ ]);',
- ];
- yield [
- '<?php
- someFunc(array(
- /* empty array */
- ));',
- ];
- yield [
- '<?php
- someFunc([
- /* empty array */
- ]);',
- ];
- yield [
- '<?php
- someFunc(array(
- /* empty
- array */));',
- '<?php
- someFunc(array(
- /* empty
- array */ ));',
- ];
- yield [
- '<?php
- someFunc([
- /* empty
- array */]);',
- '<?php
- someFunc([
- /* empty
- array */ ]);',
- ];
- yield [
- '<?php
- $a = array( // My array of:
- 1, // - first item
- 2, // - second item
- );',
- ];
- yield [
- '<?php
- $a = [ // My array of:
- 1, // - first item
- 2, // - second item
- ];',
- ];
- yield [
- '<?php
- $a = array(
- // My array of:
- 1, // - first item
- 2, // - second item
- );',
- ];
- yield [
- '<?php
- $a = [
- // My array of:
- 1, // - first item
- 2, // - second item
- ];',
- ];
- yield [
- '<?php
- $foo = array(/* comment */
- 1
- );',
- '<?php
- $foo = array( /* comment */
- 1
- );',
- ];
- yield [
- '<?php
- $foo = [/* comment */
- 1
- ];',
- '<?php
- $foo = [ /* comment */
- 1
- ];',
- ];
- // don't fix array syntax within comments
- yield [
- '<?php someFunc([/* array( "foo", "bar", [ "foo" ] ) */]);',
- '<?php someFunc([ /* array( "foo", "bar", [ "foo" ] ) */ ]);',
- ];
- yield [
- '<?php $foo = array($bar[ 4 ]);',
- '<?php $foo = array( $bar[ 4 ] );',
- ];
- yield [
- '<?php $foo = [$bar[ 4 ]];',
- '<?php $foo = [ $bar[ 4 ] ];',
- ];
- yield [
- '<?php // array( "foo", "bar" );',
- ];
- // multiple single line nested arrays on one line
- yield [
- '<?php $foo = array("foo", "bar", [1, 2, array(3)]); $baz = ["hash", 1, array("test")];',
- '<?php $foo = array( "foo", "bar", [ 1, 2, array( 3 )] ); $baz = [ "hash", 1, array( "test") ];',
- ];
- yield [
- "<?php \$foo = array( \n'bar'\n );",
- ];
- yield [
- "<?php \$foo = [ \n'bar'\n ];",
- ];
- yield [
- "<?php \$foo = array( \n'a', 'b',\n'c');",
- "<?php \$foo = array( \n'a', 'b',\n'c' );",
- ];
- yield [
- "<?php \$foo = [ \n'a', 'b',\n'c'];",
- "<?php \$foo = [ \n'a', 'b',\n'c' ];",
- ];
- yield [
- "<?php \$foo = array('a', 'b',\n'c'\n);",
- "<?php \$foo = array( 'a', 'b',\n'c'\n);",
- ];
- yield [
- "<?php \$foo = ['a', 'b',\n'c'\n];",
- "<?php \$foo = [ 'a', 'b',\n'c'\n];",
- ];
- // don't fix array syntax within string
- yield [
- '<?php $foo = [\'$bar = array( "foo" );\', array(1, 5)];',
- '<?php $foo = [ \'$bar = array( "foo" );\', array(1, 5 ) ];',
- ];
- // crazy nested garbage pile #1
- yield [
- "<?php \$foo = array(/* comment \$bar = array([ ], array( 'foo' ) ), */ function(\$a = array('foo'), \$b = [/* comment [] */]) {}, array('foo' => 'bar', 'baz' => \$x[ 4], 'hash' => array(1,2,3)));",
- "<?php \$foo = array( /* comment \$bar = array([ ], array( 'foo' ) ), */ function(\$a = array( 'foo' ), \$b = [ /* comment [] */ ]) {}, array( 'foo' => 'bar', 'baz' => \$x[ 4], 'hash' => array(1,2,3 )) );",
- ];
- // crazy nested garbage pile #2
- yield [
- '<?php $a = [array("foo", "bar ", [1, 4, function($x = ["foobar", 2]) {}, [/* array( 1) */]]), array("foo", [$y[ 3]()], \'bar\')];',
- '<?php $a = [ array("foo", "bar ", [ 1, 4, function($x = [ "foobar", 2 ]) {}, [/* array( 1) */] ] ), array("foo", [ $y[ 3]() ], \'bar\') ];',
- ];
- yield [
- '<?php
- $foo = array(
- 1 => 2, // comment
- );
- ',
- ];
- yield [
- '<?php
- function a()
- {
- yield array("a" => 1, "b" => 2);
- }',
- '<?php
- function a()
- {
- yield array( "a" => 1, "b" => 2 );
- }',
- ];
- yield [
- '<?php
- function a()
- {
- yield ["a" => 1, "b" => 2];
- }',
- '<?php
- function a()
- {
- yield [ "a" => 1, "b" => 2 ];
- }',
- ];
- yield 'array destructuring' => [
- "<?php ['url' => \$url] = \$data;",
- "<?php [ 'url' => \$url ] = \$data;",
- ];
- yield 'array destructuring with comments' => [
- "<?php [/* foo */ 'url' => \$url /* bar */] = \$data;",
- "<?php [ /* foo */ 'url' => \$url /* bar */ ] = \$data;",
- ];
- yield 'multiline array destructuring' => [
- '<?php
- [
- \'url\' => $url,
- \'token\' => $token,
- ] = $data;
- ',
- ];
- }
- }
|