SwitchCaseSemicolonToColonFixerTest.php 10 KB

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