NoUnneededBracesFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\NoUnneededBracesFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer
  22. */
  23. final class NoUnneededBracesFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param _AutogeneratedInputConfiguration $configuration
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  31. {
  32. $this->fixer->configure($configuration);
  33. $this->doTest($expected, $input);
  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. yield [
  120. '<?php
  121. namespace Foo;
  122. function Bar(){}
  123. ',
  124. '<?php
  125. namespace Foo {
  126. function Bar(){}
  127. }
  128. ',
  129. ['namespaces' => true],
  130. ];
  131. yield [
  132. '<?php
  133. namespace A5 {
  134. function AA(){}
  135. }
  136. namespace B6 {
  137. function BB(){}
  138. }',
  139. null,
  140. ['namespaces' => true],
  141. ];
  142. yield [
  143. '<?php
  144. namespace Foo7;
  145. function Bar(){}
  146. ',
  147. '<?php
  148. namespace Foo7 {
  149. function Bar(){}
  150. }',
  151. ['namespaces' => true],
  152. ];
  153. yield [
  154. '<?php
  155. namespace Foo8\A;
  156. function Bar(){}
  157. ?>',
  158. "<?php
  159. namespace Foo8\\A\t \t {
  160. function Bar(){}
  161. } ?>",
  162. ['namespaces' => true],
  163. ];
  164. yield [
  165. '<?php
  166. namespace A;
  167. class X {}
  168. ',
  169. '<?php
  170. namespace A {
  171. class X {}
  172. }',
  173. ['namespaces' => true],
  174. ];
  175. yield [
  176. '<?php
  177. namespace {
  178. class X {}
  179. }',
  180. null,
  181. ['namespaces' => true],
  182. ];
  183. }
  184. /**
  185. * @dataProvider provideFixPre80Cases
  186. *
  187. * @requires PHP <8.0
  188. */
  189. public function testFixPre80(string $expected, ?string $input = null): void
  190. {
  191. $this->doTest($expected, $input);
  192. }
  193. /**
  194. * @return iterable<string, array{string}>
  195. */
  196. public static function provideFixPre80Cases(): iterable
  197. {
  198. yield 'no fixes, offset access syntax with curly braces' => [
  199. '<?php
  200. echo ${$a};
  201. echo $a{1};
  202. ',
  203. ];
  204. }
  205. }