SingleLineEmptyBodyFixerTest.php 7.8 KB

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