SingleLineEmptyBodyFixerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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\Basic;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Basic\SingleLineEmptyBodyFixer
  18. */
  19. final class SingleLineEmptyBodyFixerTest 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. /**
  29. * @return iterable<array{0: string, 1?: string}>
  30. */
  31. public static function provideFixCases(): iterable
  32. {
  33. yield 'non-empty class' => [
  34. '<?php class Foo
  35. {
  36. public function bar () {}
  37. }
  38. ',
  39. ];
  40. yield 'non-empty function body' => [
  41. '<?php
  42. function f1()
  43. { /* foo */ }
  44. function f2()
  45. { /** foo */ }
  46. function f3()
  47. { // foo
  48. }
  49. function f4()
  50. {
  51. return true;
  52. }
  53. ',
  54. ];
  55. yield 'classes' => [
  56. '<?php
  57. class Foo {}
  58. class Bar extends BarParent {}
  59. class Baz implements BazInteface {}
  60. abstract class A {}
  61. final class F {}
  62. ',
  63. '<?php
  64. class Foo
  65. {
  66. }
  67. class Bar extends BarParent
  68. {}
  69. class Baz implements BazInteface {}
  70. abstract class A
  71. {}
  72. final class F
  73. {
  74. }
  75. ',
  76. ];
  77. yield 'multiple functions' => [
  78. '<?php
  79. function notThis1() { return 1; }
  80. function f1() {}
  81. function f2() {}
  82. function f3() {}
  83. function notThis2(){ return 1; }
  84. ',
  85. '<?php
  86. function notThis1() { return 1; }
  87. function f1()
  88. {}
  89. function f2() {
  90. }
  91. function f3()
  92. {
  93. }
  94. function notThis2(){ return 1; }
  95. ',
  96. ];
  97. yield 'remove spaces' => [
  98. '<?php
  99. function f1() {}
  100. function f2() {}
  101. function f3() {}
  102. ',
  103. '<?php
  104. function f1() { }
  105. function f2() { }
  106. function f3() { }
  107. ',
  108. ];
  109. yield 'add spaces' => [
  110. '<?php
  111. function f1() {}
  112. function f2() {}
  113. function f3() {}
  114. ',
  115. '<?php
  116. function f1(){}
  117. function f2(){}
  118. function f3(){}
  119. ',
  120. ];
  121. yield 'with return types' => [
  122. '<?php
  123. function f1(): void {}
  124. function f2(): \Foo\Bar {}
  125. function f3(): ?string {}
  126. ',
  127. '<?php
  128. function f1(): void
  129. {}
  130. function f2(): \Foo\Bar { }
  131. function f3(): ?string {
  132. }
  133. ',
  134. ];
  135. yield 'abstract functions' => [
  136. '<?php abstract class C {
  137. abstract function f1();
  138. function f2() {}
  139. abstract function f3();
  140. }
  141. if (true) { }
  142. ',
  143. '<?php abstract class C {
  144. abstract function f1();
  145. function f2() { }
  146. abstract function f3();
  147. }
  148. if (true) { }
  149. ',
  150. ];
  151. yield 'every token in separate line' => [
  152. '<?php
  153. function
  154. foo
  155. (
  156. )
  157. :
  158. void {}
  159. ',
  160. '<?php
  161. function
  162. foo
  163. (
  164. )
  165. :
  166. void
  167. {
  168. }
  169. ',
  170. ];
  171. yield 'comments before body' => [
  172. '<?php
  173. function f1()
  174. // foo
  175. {}
  176. function f2()
  177. /* foo */
  178. {}
  179. function f3()
  180. /** foo */
  181. {}
  182. function f4()
  183. /** foo */
  184. /** bar */
  185. {}
  186. ',
  187. '<?php
  188. function f1()
  189. // foo
  190. {
  191. }
  192. function f2()
  193. /* foo */
  194. {
  195. }
  196. function f3()
  197. /** foo */
  198. {
  199. }
  200. function f4()
  201. /** foo */
  202. /** bar */
  203. { }
  204. ',
  205. ];
  206. yield 'anonymous class' => [
  207. '<?php
  208. $o = new class() {};
  209. ',
  210. '<?php
  211. $o = new class() {
  212. };
  213. ',
  214. ];
  215. yield 'anonymous function' => [
  216. '<?php
  217. $x = function () {};
  218. ',
  219. '<?php
  220. $x = function () {
  221. };
  222. ',
  223. ];
  224. yield 'interface' => [
  225. '<?php interface Foo {}
  226. ',
  227. '<?php interface Foo
  228. {
  229. }
  230. ',
  231. ];
  232. yield 'trait' => [
  233. '<?php trait Foo {}
  234. ',
  235. '<?php trait Foo
  236. {
  237. }
  238. ',
  239. ];
  240. }
  241. /**
  242. * @requires PHP 8.0
  243. *
  244. * @dataProvider provideFix80Cases
  245. */
  246. public function testFix80(string $expected, ?string $input = null): void
  247. {
  248. $this->doTest($expected, $input);
  249. }
  250. /**
  251. * @return iterable<array{0: string, 1?: string}>
  252. */
  253. public static function provideFix80Cases(): iterable
  254. {
  255. yield 'single-line promoted properties' => [
  256. '<?php class Foo
  257. {
  258. public function __construct(private int $x, private int $y) {}
  259. }
  260. ',
  261. '<?php class Foo
  262. {
  263. public function __construct(private int $x, private int $y)
  264. {
  265. }
  266. }
  267. ',
  268. ];
  269. yield 'multi-line promoted properties' => [
  270. '<?php class Foo
  271. {
  272. public function __construct(
  273. private int $x,
  274. private int $y,
  275. ) {}
  276. }
  277. ',
  278. '<?php class Foo
  279. {
  280. public function __construct(
  281. private int $x,
  282. private int $y,
  283. ) {
  284. }
  285. }
  286. ',
  287. ];
  288. }
  289. /**
  290. * @dataProvider provideFix81Cases
  291. *
  292. * @requires PHP 8.1
  293. */
  294. public function testFix81(string $expected, ?string $input = null): void
  295. {
  296. $this->doTest($expected, $input);
  297. }
  298. /**
  299. * @return iterable<array{0: string, 1?: string}>
  300. */
  301. public static function provideFix81Cases(): iterable
  302. {
  303. yield 'enum' => [
  304. '<?php enum Foo {}
  305. ',
  306. '<?php enum Foo
  307. {
  308. }
  309. ',
  310. ];
  311. }
  312. }