NoUnneededCurlyBracesFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\ControlStructure;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer
  22. */
  23. final class NoUnneededCurlyBracesFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<int|string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'simple sample, last token candidate' => [
  38. '<?php echo 1;',
  39. '<?php { echo 1;}',
  40. ];
  41. yield 'minimal sample, first token candidate' => [
  42. '<?php // {}',
  43. '<?php {} // {}',
  44. ];
  45. yield [
  46. '<?php
  47. echo 0; //
  48. echo 1;
  49. switch($a) {
  50. case 2: echo 3; break;
  51. }
  52. echo 4; echo 5; //
  53. ',
  54. '<?php
  55. { { echo 0; } } //
  56. {echo 1;}
  57. switch($a) {
  58. case 2: {echo 3; break;}
  59. }
  60. echo 4; { echo 5; }//
  61. ',
  62. ];
  63. yield 'no fixes' => [
  64. '<?php
  65. foreach($a as $b){}
  66. while($a){}
  67. do {} while($a);
  68. if ($c){}
  69. if ($c){}else{}
  70. if ($c){}elseif($d){}
  71. if ($c) {}elseif($d)/** */{ } else/**/{ }
  72. try {} catch(\Exception $e) {}
  73. function test(){}
  74. $a = function() use ($c){};
  75. class A extends B {}
  76. interface D {}
  77. trait E {}
  78. ',
  79. ];
  80. yield 'no fixes II' => [
  81. '<?php
  82. declare(ticks=1) {
  83. // entire script here
  84. }
  85. #',
  86. ];
  87. yield 'no fix catch/try/finally' => [
  88. '<?php
  89. try {
  90. } catch(\Exception $e) {
  91. } finally {
  92. }
  93. ',
  94. ];
  95. yield 'no fix namespace block' => [
  96. '<?php
  97. namespace {
  98. }
  99. namespace A {
  100. }
  101. namespace A\B {
  102. }
  103. ',
  104. ];
  105. yield 'provideNoFix7Cases' => [
  106. '<?php
  107. use some\a\{ClassA, ClassB, ClassC as C};
  108. use function some\a\{fn_a, fn_b, fn_c};
  109. use const some\a\{ConstA, ConstB, ConstC};
  110. use some\x\{ClassD, function CC as C, function D, const E, function A\B};
  111. class Foo
  112. {
  113. public function getBar(): array
  114. {
  115. }
  116. }
  117. ',
  118. ];
  119. }
  120. /**
  121. * @dataProvider provideFixPre80Cases
  122. *
  123. * @requires PHP <8.0
  124. */
  125. public function testFixPre80(string $expected, ?string $input = null): void
  126. {
  127. $this->doTest($expected, $input);
  128. }
  129. /**
  130. * @return iterable<string, array{string}>
  131. */
  132. public static function provideFixPre80Cases(): iterable
  133. {
  134. yield 'no fixes, offset access syntax with curly braces' => [
  135. '<?php
  136. echo ${$a};
  137. echo $a{1};
  138. ',
  139. ];
  140. }
  141. /**
  142. * @dataProvider provideFixNamespaceCases
  143. */
  144. public function testFixNamespace(string $expected, ?string $input = null): void
  145. {
  146. $this->fixer->configure(['namespaces' => true]);
  147. $this->doTest($expected, $input);
  148. }
  149. /**
  150. * @return iterable<array{0: string, 1?: string}>
  151. */
  152. public static function provideFixNamespaceCases(): iterable
  153. {
  154. yield [
  155. '<?php
  156. namespace Foo;
  157. function Bar(){}
  158. ',
  159. '<?php
  160. namespace Foo {
  161. function Bar(){}
  162. }
  163. ',
  164. ];
  165. yield [
  166. '<?php
  167. namespace A5 {
  168. function AA(){}
  169. }
  170. namespace B6 {
  171. function BB(){}
  172. }',
  173. ];
  174. yield [
  175. '<?php
  176. namespace Foo7;
  177. function Bar(){}
  178. ',
  179. '<?php
  180. namespace Foo7 {
  181. function Bar(){}
  182. }',
  183. ];
  184. yield [
  185. '<?php
  186. namespace Foo8\A;
  187. function Bar(){}
  188. ?>',
  189. "<?php
  190. namespace Foo8\\A\t \t {
  191. function Bar(){}
  192. } ?>",
  193. ];
  194. }
  195. }