SwitchCaseSemicolonToColonFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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\SwitchCaseSemicolonToColonFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchCaseSemicolonToColonFixer>
  20. */
  21. final class SwitchCaseSemicolonToColonFixerTest 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<int|string, array{string, string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php
  37. switch (1) {
  38. case f(function () { return; }):
  39. break;
  40. }
  41. ',
  42. '<?php
  43. switch (1) {
  44. case f(function () { return; });
  45. break;
  46. }
  47. ',
  48. ];
  49. yield [
  50. '<?php
  51. switch ($a) {
  52. case 42:
  53. break;
  54. }
  55. ',
  56. '<?php
  57. switch ($a) {
  58. case 42;
  59. break;
  60. }
  61. ',
  62. ];
  63. yield [
  64. '<?php
  65. switch ($a) {
  66. case ["foo" => "bar"]:
  67. break;
  68. }
  69. ',
  70. '<?php
  71. switch ($a) {
  72. case ["foo" => "bar"];
  73. break;
  74. }
  75. ',
  76. ];
  77. yield [
  78. '<?php
  79. switch ($a) {
  80. case 42:
  81. break;
  82. case 1:
  83. switch ($a) {
  84. case 42:
  85. break;
  86. default :
  87. echo 1;
  88. }
  89. }',
  90. '<?php
  91. switch ($a) {
  92. case 42;
  93. break;
  94. case 1:
  95. switch ($a) {
  96. case 42;
  97. break;
  98. default ;
  99. echo 1;
  100. }
  101. }',
  102. ];
  103. yield [
  104. '<?php
  105. switch ($a) {
  106. case 42:;;// NoEmptyStatementFixer should clean this up (partly)
  107. break;
  108. }
  109. ',
  110. '<?php
  111. switch ($a) {
  112. case 42;;;// NoEmptyStatementFixer should clean this up (partly)
  113. break;
  114. }
  115. ',
  116. ];
  117. yield [
  118. '<?php
  119. switch ($a) {
  120. case $b ? "c" : "d" :
  121. break;
  122. }
  123. ',
  124. '<?php
  125. switch ($a) {
  126. case $b ? "c" : "d" ;
  127. break;
  128. }
  129. ',
  130. ];
  131. yield [
  132. '<?php
  133. switch ($a) {
  134. case $b ? "c" : "d": break;
  135. }
  136. ',
  137. '<?php
  138. switch ($a) {
  139. case $b ? "c" : "d"; break;
  140. }
  141. ',
  142. ];
  143. yield [
  144. '<?php
  145. switch($a) {
  146. case (int) $a < 1: {
  147. echo "leave ; alone";
  148. break;
  149. }
  150. case ($a < 2)/* test */ : {
  151. echo "fix 1";
  152. break;
  153. }
  154. case (3):{
  155. echo "fix 2";
  156. break;
  157. }
  158. case /**/(/**/ // test
  159. 4
  160. /**/)//
  161. /**/: {
  162. echo "fix 3";
  163. break;
  164. }
  165. case (((int)$b) + 4.1) : {
  166. echo "fix 4";
  167. break;
  168. }
  169. case ($b + 1) * 2 : {;;
  170. echo "leave alone";
  171. break;
  172. }
  173. }
  174. ',
  175. '<?php
  176. switch($a) {
  177. case (int) $a < 1; {
  178. echo "leave ; alone";
  179. break;
  180. }
  181. case ($a < 2)/* test */ ; {
  182. echo "fix 1";
  183. break;
  184. }
  185. case (3);{
  186. echo "fix 2";
  187. break;
  188. }
  189. case /**/(/**/ // test
  190. 4
  191. /**/)//
  192. /**/; {
  193. echo "fix 3";
  194. break;
  195. }
  196. case (((int)$b) + 4.1) ; {
  197. echo "fix 4";
  198. break;
  199. }
  200. case ($b + 1) * 2 ; {;;
  201. echo "leave alone";
  202. break;
  203. }
  204. }
  205. ',
  206. ];
  207. yield 'nested switch in switch case' => [
  208. '<?php
  209. switch (1) {
  210. case new class {public function A(){echo 1;switch(time()){case 1: echo 2;}}}:break;}
  211. ',
  212. '<?php
  213. switch (1) {
  214. case new class {public function A(){echo 1;switch(time()){case 1; echo 2;}}};break;}
  215. ',
  216. ];
  217. yield [
  218. '<?php
  219. switch (1) {
  220. case $b ? f(function () { return; }) : new class {public function A(){echo 1;}} :
  221. break;
  222. }
  223. ',
  224. '<?php
  225. switch (1) {
  226. case $b ? f(function () { return; }) : new class {public function A(){echo 1;}} ;
  227. break;
  228. }
  229. ',
  230. ];
  231. }
  232. /**
  233. * @dataProvider provideFixPre80Cases
  234. *
  235. * @requires PHP <8.0
  236. */
  237. public function testFixPre80(string $expected, ?string $input = null): void
  238. {
  239. $this->doTest($expected, $input);
  240. }
  241. /**
  242. * @return iterable<array{string, string}>
  243. */
  244. public static function provideFixPre80Cases(): iterable
  245. {
  246. yield [
  247. '<?php
  248. switch ($a) {
  249. case $b ? "c" : "this" ? "is" : "ugly":
  250. break;
  251. }
  252. ',
  253. '<?php
  254. switch ($a) {
  255. case $b ? "c" : "this" ? "is" : "ugly";
  256. break;
  257. }
  258. ',
  259. ];
  260. }
  261. /**
  262. * @dataProvider provideFix80Cases
  263. *
  264. * @requires PHP 8
  265. */
  266. public function testFix80(string $expected, ?string $input = null): void
  267. {
  268. $this->doTest($expected, $input);
  269. }
  270. /**
  271. * @return iterable<string, array{0: string, 1?: string}>
  272. */
  273. public static function provideFix80Cases(): iterable
  274. {
  275. yield 'Simple match' => [
  276. '<?php
  277. echo match ($a) {
  278. default => "foo",
  279. };
  280. ',
  281. ];
  282. yield 'Match in switch' => [
  283. '<?php
  284. switch ($foo) {
  285. case "bar":
  286. echo match ($a) {
  287. default => "foo",
  288. };
  289. break;
  290. }
  291. ',
  292. ];
  293. yield 'Match in case value' => [
  294. '<?php
  295. switch ($foo) {
  296. case match ($bar) {
  297. default => "foo",
  298. }: echo "It works!";
  299. }
  300. ',
  301. '<?php
  302. switch ($foo) {
  303. case match ($bar) {
  304. default => "foo",
  305. }; echo "It works!";
  306. }
  307. ',
  308. ];
  309. }
  310. /**
  311. * @dataProvider provideFix81Cases
  312. *
  313. * @requires PHP 8.1
  314. */
  315. public function testFix81(string $expected, ?string $input = null): void
  316. {
  317. $this->doTest($expected, $input);
  318. }
  319. /**
  320. * @return iterable<string, array{string, string}>
  321. */
  322. public static function provideFix81Cases(): iterable
  323. {
  324. yield 'enums' => [
  325. '<?php
  326. enum Suit {
  327. case Hearts; // do not fix
  328. }
  329. enum UserStatus: string {
  330. case Pending = "P"; // do not fix
  331. public function label(): string {
  332. switch (foo()) {
  333. case 42: // do fix
  334. bar();
  335. $a = new class() {
  336. public function bar() {
  337. switch (foo()) {
  338. case 43: // do fix
  339. bar();
  340. }
  341. $expressionResult = match ($condition) {
  342. default => baz(),
  343. };
  344. }
  345. };
  346. $a->bar();
  347. break;
  348. }
  349. return "label";
  350. }
  351. }
  352. $expressionResult = match ($condition) {
  353. default => baz(),
  354. };
  355. ',
  356. '<?php
  357. enum Suit {
  358. case Hearts; // do not fix
  359. }
  360. enum UserStatus: string {
  361. case Pending = "P"; // do not fix
  362. public function label(): string {
  363. switch (foo()) {
  364. case 42; // do fix
  365. bar();
  366. $a = new class() {
  367. public function bar() {
  368. switch (foo()) {
  369. case 43; // do fix
  370. bar();
  371. }
  372. $expressionResult = match ($condition) {
  373. default => baz(),
  374. };
  375. }
  376. };
  377. $a->bar();
  378. break;
  379. }
  380. return "label";
  381. }
  382. }
  383. $expressionResult = match ($condition) {
  384. default => baz(),
  385. };
  386. ',
  387. ];
  388. }
  389. }