NoWhitespaceBeforeCommaInArrayFixerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\ArrayNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Adam Marczuk <adam@marczuk.info>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer
  20. */
  21. final class NoWhitespaceBeforeCommaInArrayFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param array<string, mixed> $config
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, string $input = null, array $config = []): void
  29. {
  30. $this->fixer->configure($config);
  31. $this->doTest($expected, $input);
  32. }
  33. public static function provideFixCases(): iterable
  34. {
  35. // old style array
  36. yield [
  37. '<?php $x = array(1, "2",3);',
  38. '<?php $x = array(1 , "2",3);',
  39. ];
  40. // old style array with comments
  41. yield [
  42. '<?php $x = array /* comment */ (1, "2", 3);',
  43. '<?php $x = array /* comment */ (1 , "2", 3);',
  44. ];
  45. // old style array with comments
  46. yield [
  47. '<?php $x = array(1#
  48. ,#
  49. "2", 3);',
  50. '<?php $x = array(1#
  51. ,#
  52. "2" , 3);',
  53. ];
  54. // short array
  55. yield [
  56. '<?php $x = [1, "2", 3,$y];',
  57. '<?php $x = [1 , "2", 3 ,$y];',
  58. ];
  59. // don't change function calls
  60. yield [
  61. '<?php $x = [ 1, "2",getValue(1,2 ,3 ),$y];',
  62. '<?php $x = [ 1 , "2",getValue(1,2 ,3 ) ,$y];',
  63. ];
  64. // don't change function declarations
  65. yield [
  66. '<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
  67. '<?php $x = [1 , "2", function( $x ,$y) { return $x + $y; }, $y];',
  68. ];
  69. // don't change function declarations but change array inside
  70. yield [
  71. '<?php $x = [ 1, "2","c" => function( $x ,$y) { return [$x, $y]; }, $y];',
  72. '<?php $x = [ 1 , "2","c" => function( $x ,$y) { return [$x , $y]; }, $y];',
  73. ];
  74. // don't change anonymous class implements list but change array inside
  75. yield [
  76. '<?php $x = [ 1, "2","c" => new class implements Foo , Bar { const FOO = ["x", "y"]; }, $y];',
  77. '<?php $x = [ 1 , "2","c" => new class implements Foo , Bar { const FOO = ["x" , "y"]; }, $y];',
  78. ];
  79. // associative array (old)
  80. yield [
  81. '<?php $x = array( "a" => $a, "b" => "b",3=>$this->foo(), "d" => 30);',
  82. '<?php $x = array( "a" => $a , "b" => "b",3=>$this->foo() , "d" => 30);',
  83. ];
  84. // associative array (short)
  85. yield [
  86. '<?php $x = [ "a" => $a, "b"=>"b",3 => $this->foo(), "d" =>30 ];',
  87. '<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo() , "d" =>30 ];',
  88. ];
  89. // nested arrays
  90. yield [
  91. '<?php $x = ["a" => $a, "b" => "b", 3=> [5,6, 7], "d" => array(1, 2,3,4)];',
  92. '<?php $x = ["a" => $a , "b" => "b", 3=> [5 ,6, 7] , "d" => array(1, 2,3 ,4)];',
  93. ];
  94. // multi line array
  95. yield [
  96. '<?php $x = [ "a" =>$a,
  97. "b"=>
  98. "b",
  99. 3 => $this->foo(),
  100. "d" => 30 ];',
  101. '<?php $x = [ "a" =>$a ,
  102. "b"=>
  103. "b",
  104. 3 => $this->foo() ,
  105. "d" => 30 ];',
  106. ];
  107. // multi line array
  108. yield [
  109. '<?php $a = [
  110. "foo",
  111. "bar",
  112. ];',
  113. '<?php $a = [
  114. "foo" ,
  115. "bar"
  116. ,
  117. ];',
  118. ];
  119. // nested multiline
  120. yield [
  121. '<?php $a = array(array(
  122. array(T_OPEN_TAG),
  123. array(T_VARIABLE, "$x"),
  124. ), 1);',
  125. ];
  126. yield [
  127. '<?php $a = array( // comment
  128. 123,
  129. );',
  130. ];
  131. yield [
  132. "<?php \$x = array(<<<'EOF'
  133. <?php \$a = '\\foo\\bar\\\\';
  134. EOF
  135. , <<<'EOF'
  136. <?php \$a = \"\\foo\\bar\\\\\";
  137. EOF
  138. );",
  139. ];
  140. yield [
  141. "<?php \$x = array(<<<'EOF'
  142. <?php \$a = '\\foo\\bar\\\\';
  143. EOF, <<<'EOF'
  144. <?php \$a = \"\\foo\\bar\\\\\";
  145. EOF
  146. );",
  147. "<?php \$x = array(<<<'EOF'
  148. <?php \$a = '\\foo\\bar\\\\';
  149. EOF
  150. , <<<'EOF'
  151. <?php \$a = \"\\foo\\bar\\\\\";
  152. EOF
  153. );",
  154. ['after_heredoc' => true],
  155. ];
  156. yield [
  157. '<?php $x = array(...$foo, ...$bar);',
  158. '<?php $x = array(...$foo , ...$bar);',
  159. ];
  160. yield [
  161. '<?php $x = [...$foo, ...$bar];',
  162. '<?php $x = [...$foo , ...$bar];',
  163. ];
  164. }
  165. }