ArraySyntaxFixerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Sebastiaan Stok <s.stok@rollerscapes.net>
  17. * @author Gregor Harlan <gharlan@web.de>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer
  26. */
  27. final class ArraySyntaxFixerTest extends AbstractFixerTestCase
  28. {
  29. public function testInvalidConfiguration(): void
  30. {
  31. $this->expectException(InvalidFixerConfigurationException::class);
  32. $this->expectExceptionMessageMatches('#^\[array_syntax\] Invalid configuration: The option "a" does not exist\. Defined options are: "syntax"\.$#');
  33. $this->fixer->configure(['a' => 1]);
  34. }
  35. /**
  36. * @param _AutogeneratedInputConfiguration $configuration
  37. *
  38. * @dataProvider provideFixCases
  39. */
  40. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  41. {
  42. $this->fixer->configure($configuration);
  43. $this->doTest($expected, $input);
  44. }
  45. /**
  46. * @return iterable<array{string, null|string, array{syntax?: string}}>
  47. */
  48. public static function provideFixCases(): iterable
  49. {
  50. yield 'default configuration' => [
  51. '<?php $a = []; $b = [];',
  52. '<?php $a = []; $b = array();',
  53. [],
  54. ];
  55. yield [
  56. '<?php $x = array();',
  57. '<?php $x = [];',
  58. ['syntax' => 'long'],
  59. ];
  60. yield [
  61. '<?php $x = array(); $y = array();',
  62. '<?php $x = []; $y = [];',
  63. ['syntax' => 'long'],
  64. ];
  65. yield [
  66. '<?php $x = array( );',
  67. '<?php $x = [ ];',
  68. ['syntax' => 'long'],
  69. ];
  70. yield [
  71. '<?php $x = array(\'foo\');',
  72. '<?php $x = [\'foo\'];',
  73. ['syntax' => 'long'],
  74. ];
  75. yield [
  76. '<?php $x = array( \'foo\' );',
  77. '<?php $x = [ \'foo\' ];',
  78. ['syntax' => 'long'],
  79. ];
  80. yield [
  81. '<?php $x = array(($y ? true : false));',
  82. '<?php $x = [($y ? true : false)];',
  83. ['syntax' => 'long'],
  84. ];
  85. yield [
  86. '<?php $x = array(($y ? array(true) : array(false)));',
  87. '<?php $x = [($y ? [true] : [false])];',
  88. ['syntax' => 'long'],
  89. ];
  90. yield [
  91. '<?php $x = array(($y ? array(true) : array( false )));',
  92. '<?php $x = [($y ? [true] : [ false ])];',
  93. ['syntax' => 'long'],
  94. ];
  95. yield [
  96. '<?php $x = array(($y ? array("t" => true) : array("f" => false)));',
  97. '<?php $x = [($y ? ["t" => true] : ["f" => false])];',
  98. ['syntax' => 'long'], ];
  99. yield [
  100. '<?php print_r(array(($y ? true : false)));',
  101. '<?php print_r([($y ? true : false)]);',
  102. ['syntax' => 'long'],
  103. ];
  104. yield [
  105. '<?php $x = array(array(array()));',
  106. '<?php $x = [[[]]];',
  107. ['syntax' => 'long'],
  108. ];
  109. yield [
  110. '<?php $x = array(array(array())); $y = array(array(array()));',
  111. '<?php $x = [[[]]]; $y = [[[]]];',
  112. ['syntax' => 'long'],
  113. ];
  114. yield [
  115. '<?php function(array $foo = array()) {};',
  116. '<?php function(array $foo = []) {};',
  117. ['syntax' => 'long'],
  118. ];
  119. yield [
  120. '<?php $x = array(1, 2)[0];',
  121. '<?php $x = [1, 2][0];',
  122. ['syntax' => 'long'],
  123. ];
  124. yield [
  125. '<?php $x[] = 1;',
  126. null,
  127. ['syntax' => 'long'],
  128. ];
  129. yield [
  130. '<?php $x[ ] = 1;',
  131. null,
  132. ['syntax' => 'long'],
  133. ];
  134. yield [
  135. '<?php $x[2] = 1;',
  136. null,
  137. ['syntax' => 'long'],
  138. ];
  139. yield [
  140. '<?php $x["a"] = 1;',
  141. null,
  142. ['syntax' => 'long'],
  143. ];
  144. yield [
  145. '<?php $x = func()[$x];',
  146. null,
  147. ['syntax' => 'long'],
  148. ];
  149. yield [
  150. '<?php $x = "foo"[$x];',
  151. null,
  152. ['syntax' => 'long'],
  153. ];
  154. yield [
  155. '<?php $text = "foo ${aaa[123]} bar $bbb[0] baz";',
  156. null,
  157. ['syntax' => 'long'],
  158. ];
  159. yield [
  160. '<?php foreach ($array as [$x, $y]) {}',
  161. null,
  162. ['syntax' => 'long'],
  163. ];
  164. yield [
  165. '<?php foreach ($array as $key => [$x, $y]) {}',
  166. null,
  167. ['syntax' => 'long'],
  168. ];
  169. yield [
  170. '<?php $x = [];',
  171. '<?php $x = array();',
  172. ['syntax' => 'short'],
  173. ];
  174. yield [
  175. '<?php $x = []; $y = [];',
  176. '<?php $x = array(); $y = array();',
  177. ['syntax' => 'short'],
  178. ];
  179. yield [
  180. '<?php $x = [ ];',
  181. '<?php $x = array( );',
  182. ['syntax' => 'short'],
  183. ];
  184. yield [
  185. '<?php $x = [\'foo\'];',
  186. '<?php $x = array(\'foo\');',
  187. ['syntax' => 'short'],
  188. ];
  189. yield [
  190. '<?php $x = [ \'foo\' ];',
  191. '<?php $x = array( \'foo\' );',
  192. ['syntax' => 'short'],
  193. ];
  194. yield [
  195. '<?php $x = [($y ? true : false)];',
  196. '<?php $x = array(($y ? true : false));',
  197. ['syntax' => 'short'],
  198. ];
  199. yield [
  200. '<?php $x = [($y ? [true] : [false])];',
  201. '<?php $x = array(($y ? array(true) : array(false)));',
  202. ['syntax' => 'short'],
  203. ];
  204. yield [
  205. '<?php $x = [($y ? [true] : [ false ])];',
  206. '<?php $x = array(($y ? array(true) : array( false )));',
  207. ['syntax' => 'short'],
  208. ];
  209. yield [
  210. '<?php $x = [($y ? ["t" => true] : ["f" => false])];',
  211. '<?php $x = array(($y ? array("t" => true) : array("f" => false)));',
  212. ['syntax' => 'short'],
  213. ];
  214. yield [
  215. '<?php print_r([($y ? true : false)]);',
  216. '<?php print_r(array(($y ? true : false)));',
  217. ['syntax' => 'short'],
  218. ];
  219. yield [
  220. '<?php $x = [[[]]];',
  221. '<?php $x = array(array(array()));',
  222. ['syntax' => 'short'],
  223. ];
  224. yield [
  225. '<?php $x = [[[]]]; $y = [[[]]];',
  226. '<?php $x = array(array(array())); $y = array(array(array()));',
  227. ['syntax' => 'short'],
  228. ];
  229. yield [
  230. '<?php function(array $foo = []) {};',
  231. '<?php function(array $foo = array()) {};',
  232. ['syntax' => 'short'],
  233. ];
  234. yield [
  235. '<?php function(array $foo) {};',
  236. null,
  237. ['syntax' => 'short'],
  238. ];
  239. yield [
  240. '<?php $a = [ ];',
  241. '<?php $a = array ( );',
  242. ['syntax' => 'short'],
  243. ];
  244. }
  245. }