ListSyntaxFixerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\ListNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\Tests\Test\TestCaseUtils;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer
  19. */
  20. final class ListSyntaxFixerTest extends AbstractFixerTestCase
  21. {
  22. public function testFixWithDefaultConfiguration(): void
  23. {
  24. $this->fixer->configure([]);
  25. $this->doTest(
  26. '<?php $a = [$a, $b] = $a; [$b] = $a;',
  27. '<?php $a = list($a, $b) = $a; [$b] = $a;'
  28. );
  29. }
  30. /**
  31. * @dataProvider provideFixToLongSyntaxCases
  32. */
  33. public function testFixToLongSyntax(string $expected, ?string $input = null): void
  34. {
  35. $this->fixer->configure(['syntax' => 'long']);
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixToLongSyntaxCases(): iterable
  39. {
  40. // reverse testing
  41. $shortCases = self::provideFixToShortSyntaxCases();
  42. foreach ($shortCases as $label => $shortCase) {
  43. if ('messy comments case' === $label) {
  44. continue;
  45. }
  46. yield $label => [$shortCase[1], $shortCase[0]];
  47. }
  48. // the reverse of this is different because of all the comments and white space,
  49. // therefore we override with a similar case here
  50. yield 'comment case' => [
  51. '<?php
  52. #
  53. list(#
  54. $a#
  55. )#
  56. =#
  57. $a#
  58. ;#',
  59. '<?php
  60. #
  61. [#
  62. $a#
  63. ]#
  64. =#
  65. $a#
  66. ;#',
  67. ];
  68. yield ['<?php
  69. class Test
  70. {
  71. public function updateAttributeKey($key, $value)
  72. {
  73. $this->{camel_case($attributes)}[$key] = $value;
  74. }
  75. }',
  76. ];
  77. yield ['<?php [$b[$a]] = $foo();'];
  78. yield [
  79. '<?php [$iHaveList => list($x, $y) = getList()];',
  80. '<?php [$iHaveList => [$x, $y] = getList()];',
  81. ];
  82. }
  83. /**
  84. * @dataProvider provideFixToShortSyntaxCases
  85. */
  86. public function testFixToShortSyntax(string $expected, ?string $input = null): void
  87. {
  88. $this->fixer->configure(['syntax' => 'short']);
  89. $this->doTest($expected, $input);
  90. }
  91. public static function provideFixToShortSyntaxCases(): iterable
  92. {
  93. yield [
  94. '<?php [$x] = $a;',
  95. '<?php list($x) = $a;',
  96. ];
  97. yield [
  98. '<?php [$a, $b, $c] = $array;',
  99. '<?php list($a, $b, $c) = $array;',
  100. ];
  101. yield [
  102. '<?php ["a" => $a, "b" => $b, "c" => $c] = $array;',
  103. '<?php list("a" => $a, "b" => $b, "c" => $c) = $array;',
  104. ];
  105. yield [
  106. '<?php
  107. #
  108. [//
  109. $x] =/**/$a?>',
  110. '<?php
  111. #
  112. list(//
  113. $x) =/**/$a?>',
  114. ];
  115. yield 'messy comments case' => [
  116. '<?php
  117. #a
  118. #g
  119. [#h
  120. #f
  121. $a#
  122. #e
  123. ]#
  124. #
  125. =#c
  126. #
  127. $a;#
  128. #
  129. ',
  130. '<?php
  131. #a
  132. list#g
  133. (#h
  134. #f
  135. $a#
  136. #e
  137. )#
  138. #
  139. =#c
  140. #
  141. $a;#
  142. #
  143. ',
  144. ];
  145. yield [
  146. '<?php [$a, $b,, [$c, $d]] = $a;',
  147. '<?php list($a, $b,, list($c, $d)) = $a;',
  148. ];
  149. yield [
  150. '<?php [[$a, $b], [$c, $d]] = $a;',
  151. '<?php list(list($a, $b), list($c, $d)) = $a;',
  152. ];
  153. yield [
  154. '<?php [[$a, [$b]], [[$c, [$d]]]] = $a;',
  155. '<?php list(list($a, list($b)), list(list($c, list($d)))) = $a;',
  156. ];
  157. yield [
  158. '<?php [[$a]] = $foo();',
  159. '<?php list(list($a)) = $foo();',
  160. ];
  161. yield [
  162. '<?php foreach ($z as [$a, $b]) {}',
  163. '<?php foreach ($z as list($a, $b)) {}',
  164. ];
  165. }
  166. /**
  167. * @dataProvider provideFixToShortSyntaxPhp72Cases
  168. */
  169. public function testFixToShortSyntaxPhp72(string $expected, string $input): void
  170. {
  171. $this->fixer->configure(['syntax' => 'short']);
  172. $this->doTest($expected, $input);
  173. }
  174. /**
  175. * @dataProvider provideFixToLongSyntaxPhp72Cases
  176. */
  177. public function testFixToLongSyntaxPhp72(string $expected, string $input): void
  178. {
  179. $this->fixer->configure(['syntax' => 'long']);
  180. $this->doTest($expected, $input);
  181. }
  182. public static function provideFixToShortSyntaxPhp72Cases(): iterable
  183. {
  184. yield [
  185. '<?php [$a, $b,, [$c, $d]] = $a;',
  186. '<?php list($a, $b,, list($c, $d)) = $a;',
  187. ];
  188. }
  189. public static function provideFixToLongSyntaxPhp72Cases(): iterable
  190. {
  191. return TestCaseUtils::swapExpectedInputTestCases(self::provideFixToShortSyntaxPhp72Cases());
  192. }
  193. /**
  194. * @dataProvider provideFixToShortSyntaxPhp73Cases
  195. */
  196. public function testFixToShortSyntaxPhp73(string $expected, string $input): void
  197. {
  198. $this->fixer->configure(['syntax' => 'short']);
  199. $this->doTest($expected, $input);
  200. }
  201. /**
  202. * @dataProvider provideFixToLongSyntaxPhp73Cases
  203. */
  204. public function testFixToLongSyntaxPhp73(string $expected, string $input): void
  205. {
  206. $this->fixer->configure(['syntax' => 'long']);
  207. $this->doTest($expected, $input);
  208. }
  209. public static function provideFixToShortSyntaxPhp73Cases(): iterable
  210. {
  211. yield [
  212. '<?php [&$a, $b] = $a;',
  213. '<?php list(&$a, $b) = $a;',
  214. ];
  215. yield [
  216. '<?php [&$a,/* */&$b] = $a;',
  217. '<?php list(&$a,/* */&$b) = $a;',
  218. ];
  219. yield [
  220. '<?php [&$a, $b,, [&$c, $d]] = $a;',
  221. '<?php list(&$a, $b,, list(&$c, $d)) = $a;',
  222. ];
  223. }
  224. public static function provideFixToLongSyntaxPhp73Cases(): iterable
  225. {
  226. return TestCaseUtils::swapExpectedInputTestCases(self::provideFixToShortSyntaxPhp73Cases());
  227. }
  228. /**
  229. * @dataProvider provideFix81Cases
  230. *
  231. * @requires PHP 8.1
  232. */
  233. public function testFix81(string $expected, string $input = null): void
  234. {
  235. $this->doTest($expected, $input);
  236. }
  237. public static function provideFix81Cases(): iterable
  238. {
  239. yield 'simple 8.1' => [
  240. '<?php $a = _list(...);',
  241. ];
  242. }
  243. }