NoWhitespaceBeforeCommaInArrayFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. return [
  36. // old style array
  37. [
  38. '<?php $x = array(1, "2",3);',
  39. '<?php $x = array(1 , "2",3);',
  40. ],
  41. // old style array with comments
  42. [
  43. '<?php $x = array /* comment */ (1, "2", 3);',
  44. '<?php $x = array /* comment */ (1 , "2", 3);',
  45. ],
  46. // old style array with comments
  47. [
  48. '<?php $x = array(1#
  49. ,#
  50. "2", 3);',
  51. '<?php $x = array(1#
  52. ,#
  53. "2" , 3);',
  54. ],
  55. // short array
  56. [
  57. '<?php $x = [1, "2", 3,$y];',
  58. '<?php $x = [1 , "2", 3 ,$y];',
  59. ],
  60. // don't change function calls
  61. [
  62. '<?php $x = [ 1, "2",getValue(1,2 ,3 ),$y];',
  63. '<?php $x = [ 1 , "2",getValue(1,2 ,3 ) ,$y];',
  64. ],
  65. // don't change function declarations
  66. [
  67. '<?php $x = [1, "2", function( $x ,$y) { return $x + $y; }, $y];',
  68. '<?php $x = [1 , "2", function( $x ,$y) { return $x + $y; }, $y];',
  69. ],
  70. // don't change function declarations but change array inside
  71. [
  72. '<?php $x = [ 1, "2","c" => function( $x ,$y) { return [$x, $y]; }, $y];',
  73. '<?php $x = [ 1 , "2","c" => function( $x ,$y) { return [$x , $y]; }, $y];',
  74. ],
  75. // don't change anonymous class implements list but change array inside
  76. [
  77. '<?php $x = [ 1, "2","c" => new class implements Foo , Bar { const FOO = ["x", "y"]; }, $y];',
  78. '<?php $x = [ 1 , "2","c" => new class implements Foo , Bar { const FOO = ["x" , "y"]; }, $y];',
  79. ],
  80. // associative array (old)
  81. [
  82. '<?php $x = array( "a" => $a, "b" => "b",3=>$this->foo(), "d" => 30);',
  83. '<?php $x = array( "a" => $a , "b" => "b",3=>$this->foo() , "d" => 30);',
  84. ],
  85. // associative array (short)
  86. [
  87. '<?php $x = [ "a" => $a, "b"=>"b",3 => $this->foo(), "d" =>30 ];',
  88. '<?php $x = [ "a" => $a , "b"=>"b",3 => $this->foo() , "d" =>30 ];',
  89. ],
  90. // nested arrays
  91. [
  92. '<?php $x = ["a" => $a, "b" => "b", 3=> [5,6, 7], "d" => array(1, 2,3,4)];',
  93. '<?php $x = ["a" => $a , "b" => "b", 3=> [5 ,6, 7] , "d" => array(1, 2,3 ,4)];',
  94. ],
  95. // multi line array
  96. [
  97. '<?php $x = [ "a" =>$a,
  98. "b"=>
  99. "b",
  100. 3 => $this->foo(),
  101. "d" => 30 ];',
  102. '<?php $x = [ "a" =>$a ,
  103. "b"=>
  104. "b",
  105. 3 => $this->foo() ,
  106. "d" => 30 ];',
  107. ],
  108. // multi line array
  109. [
  110. '<?php $a = [
  111. "foo",
  112. "bar",
  113. ];',
  114. '<?php $a = [
  115. "foo" ,
  116. "bar"
  117. ,
  118. ];',
  119. ],
  120. // nested multiline
  121. [
  122. '<?php $a = array(array(
  123. array(T_OPEN_TAG),
  124. array(T_VARIABLE, "$x"),
  125. ), 1);',
  126. ],
  127. [
  128. '<?php $a = array( // comment
  129. 123,
  130. );',
  131. ],
  132. [
  133. "<?php \$x = array(<<<'EOF'
  134. <?php \$a = '\\foo\\bar\\\\';
  135. EOF
  136. , <<<'EOF'
  137. <?php \$a = \"\\foo\\bar\\\\\";
  138. EOF
  139. );",
  140. ],
  141. [
  142. "<?php \$x = array(<<<'EOF'
  143. <?php \$a = '\\foo\\bar\\\\';
  144. EOF, <<<'EOF'
  145. <?php \$a = \"\\foo\\bar\\\\\";
  146. EOF
  147. );",
  148. "<?php \$x = array(<<<'EOF'
  149. <?php \$a = '\\foo\\bar\\\\';
  150. EOF
  151. , <<<'EOF'
  152. <?php \$a = \"\\foo\\bar\\\\\";
  153. EOF
  154. );",
  155. ['after_heredoc' => true],
  156. ],
  157. [
  158. '<?php $x = array(...$foo, ...$bar);',
  159. '<?php $x = array(...$foo , ...$bar);',
  160. ],
  161. [
  162. '<?php $x = [...$foo, ...$bar];',
  163. '<?php $x = [...$foo , ...$bar];',
  164. ],
  165. ];
  166. }
  167. }