NoUnneededCurlyBracesFixerTest.php 5.2 KB

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