NoUnneededCurlyBracesFixerTest.php 4.9 KB

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