SwitchCaseSemicolonToColonFixerTest.php 10 KB

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