SwitchContinueToBreakFixerTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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\SwitchContinueToBreakFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchContinueToBreakFixer>
  20. */
  21. final class SwitchContinueToBreakFixerTest 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. public static function provideFixCases(): iterable
  31. {
  32. yield 'alternative syntax |' => [
  33. '<?php
  34. switch($foo):
  35. case 3:
  36. continue;
  37. endswitch?>
  38. ',
  39. ];
  40. yield 'alternative syntax ||' => [
  41. '<?php
  42. foreach ([] as $v) {
  43. continue;
  44. }
  45. if ($foo != 0) {
  46. }
  47. switch ($foo):
  48. endswitch;',
  49. ];
  50. yield 'nested switches' => [
  51. '<?php
  52. switch($z) {
  53. case 1:
  54. switch($x) {
  55. case 2:
  56. switch($y) {
  57. case 3:
  58. switch($z) {
  59. case 4:
  60. break; // z.1
  61. }
  62. break; // z
  63. }
  64. break; // y
  65. }
  66. break; // x
  67. }
  68. ',
  69. '<?php
  70. switch($z) {
  71. case 1:
  72. switch($x) {
  73. case 2:
  74. switch($y) {
  75. case 3:
  76. switch($z) {
  77. case 4:
  78. continue; // z.1
  79. }
  80. continue; // z
  81. }
  82. continue; // y
  83. }
  84. continue; // x
  85. }
  86. ',
  87. ];
  88. yield 'nested 2' => [
  89. '<?php
  90. while ($foo) {
  91. switch ($bar) {
  92. case "baz":
  93. while ($xyz) {
  94. switch($zA) {
  95. case 1:
  96. break 3; // fix
  97. }
  98. if ($z) continue;
  99. if ($zz){ continue; }
  100. if ($zzz) continue 3;
  101. if ($zzz){ continue 3; }
  102. if ($b) break 2; // fix
  103. }
  104. switch($zG) {
  105. case 1:
  106. switch($zF) {
  107. case 1:
  108. break 1; // fix
  109. case 2:
  110. break 2; // fix
  111. case 3:
  112. break 3; // fix
  113. case 4:
  114. while($a){
  115. while($a){
  116. while($a){
  117. if ($a) {
  118. break 4; // fix
  119. } elseif($z) {
  120. break 5; // fix
  121. } else {
  122. break 6; // fix
  123. }
  124. continue 7;
  125. }
  126. }
  127. }
  128. continue 4;
  129. }
  130. break 2; // fix
  131. }
  132. }
  133. }
  134. ',
  135. '<?php
  136. while ($foo) {
  137. switch ($bar) {
  138. case "baz":
  139. while ($xyz) {
  140. switch($zA) {
  141. case 1:
  142. continue 3; // fix
  143. }
  144. if ($z) continue;
  145. if ($zz){ continue; }
  146. if ($zzz) continue 3;
  147. if ($zzz){ continue 3; }
  148. if ($b) continue 2; // fix
  149. }
  150. switch($zG) {
  151. case 1:
  152. switch($zF) {
  153. case 1:
  154. continue 1; // fix
  155. case 2:
  156. continue 2; // fix
  157. case 3:
  158. continue 3; // fix
  159. case 4:
  160. while($a){
  161. while($a){
  162. while($a){
  163. if ($a) {
  164. continue 4; // fix
  165. } elseif($z) {
  166. continue 5; // fix
  167. } else {
  168. continue 6; // fix
  169. }
  170. continue 7;
  171. }
  172. }
  173. }
  174. continue 4;
  175. }
  176. continue 2; // fix
  177. }
  178. }
  179. }
  180. ',
  181. ];
  182. yield 'nested do while' => [
  183. '<?php
  184. switch ($a) {
  185. case 1:
  186. do {
  187. switch ($a) {
  188. case 1:
  189. do {
  190. switch ($a) {
  191. case 1:
  192. do {
  193. continue;
  194. } while (false);
  195. break;
  196. }
  197. continue;
  198. } while (false);
  199. break;
  200. }
  201. continue;
  202. } while (false);
  203. break;
  204. }
  205. ',
  206. '<?php
  207. switch ($a) {
  208. case 1:
  209. do {
  210. switch ($a) {
  211. case 1:
  212. do {
  213. switch ($a) {
  214. case 1:
  215. do {
  216. continue;
  217. } while (false);
  218. continue;
  219. }
  220. continue;
  221. } while (false);
  222. continue;
  223. }
  224. continue;
  225. } while (false);
  226. continue;
  227. }
  228. ',
  229. ];
  230. yield [
  231. '<?php
  232. switch(foo()) {
  233. case 1: while(bar($i))continue;break;
  234. default: echo 7;
  235. }
  236. ',
  237. '<?php
  238. switch(foo()) {
  239. case 1: while(bar($i))continue;continue;
  240. default: echo 7;
  241. }
  242. ',
  243. ];
  244. yield 'do not fix cases' => [
  245. '<?php
  246. switch($a) {
  247. case 1:
  248. while (false) {
  249. continue;
  250. }
  251. while (false) break 1;
  252. do {
  253. continue;
  254. } while (false);
  255. for ($a = 0; $a < 1; ++$a) {
  256. continue;
  257. }
  258. foreach ($a as $b) continue;
  259. for (; $i < 1; ++$i) break 1; echo $i;
  260. for (;;) continue;
  261. while(false) continue;
  262. while(false) continue?><?php
  263. // so bad and such a mess, not worth adding a ton of logic to fix this
  264. switch($z) {
  265. case 1:
  266. continue ?> <?php 23;
  267. case 2:
  268. continue 1?> <?php + $a;
  269. }
  270. }
  271. ',
  272. ];
  273. yield 'nested while, do not fix' => [
  274. '<?php
  275. switch(foo()) {
  276. case 1: while(bar($i)){ --$i; echo 1; continue;}break;
  277. default: echo 8;
  278. }',
  279. ];
  280. yield 'not int cases' => [
  281. '<?php
  282. while($b) {
  283. switch($a) {
  284. case 1:
  285. break 01;
  286. case 2:
  287. break 0x1;
  288. case 22:
  289. break 0x01;
  290. case 3:
  291. break 0b1;
  292. case 32:
  293. break 0b0001;
  294. case 4:
  295. break 0b000001;
  296. // do not fix
  297. case 1:
  298. continue 02;
  299. case 2:
  300. continue 0x2;
  301. case 3:
  302. continue 0b10;
  303. }
  304. }',
  305. '<?php
  306. while($b) {
  307. switch($a) {
  308. case 1:
  309. continue 01;
  310. case 2:
  311. continue 0x1;
  312. case 22:
  313. continue 0x01;
  314. case 3:
  315. continue 0b1;
  316. case 32:
  317. continue 0b0001;
  318. case 4:
  319. continue 0b000001;
  320. // do not fix
  321. case 1:
  322. continue 02;
  323. case 2:
  324. continue 0x2;
  325. case 3:
  326. continue 0b10;
  327. }
  328. }',
  329. ];
  330. yield 'deep nested case' => [
  331. '<?php
  332. switch ($a) {
  333. case $b:
  334. while (false) {
  335. while (false) {
  336. while (false) {
  337. while (false) {
  338. while (false) {
  339. while (false) {
  340. while (false) {
  341. while (false) {
  342. switch ($a) {
  343. case 1:
  344. echo 1;
  345. break 10;
  346. }}}}}}}}}}',
  347. '<?php
  348. switch ($a) {
  349. case $b:
  350. while (false) {
  351. while (false) {
  352. while (false) {
  353. while (false) {
  354. while (false) {
  355. while (false) {
  356. while (false) {
  357. while (false) {
  358. switch ($a) {
  359. case 1:
  360. echo 1;
  361. continue 10;
  362. }}}}}}}}}}',
  363. ];
  364. yield 'numeric literal separator' => [
  365. '<?php
  366. switch($a) {
  367. case "a":
  368. echo __FILE__;
  369. break;
  370. }
  371. ',
  372. '<?php
  373. switch($a) {
  374. case "a":
  375. echo __FILE__;
  376. continue;
  377. }
  378. ',
  379. [
  380. '<?php
  381. switch ($a) {
  382. case $b:
  383. while (false) {
  384. while (false) {
  385. while (false) {
  386. while (false) {
  387. while (false) {
  388. while (false) {
  389. while (false) {
  390. while (false) {
  391. switch ($a) {
  392. case 1:
  393. echo 1;
  394. break 1_0;
  395. }}}}}}}}}}',
  396. '<?php
  397. switch ($a) {
  398. case $b:
  399. while (false) {
  400. while (false) {
  401. while (false) {
  402. while (false) {
  403. while (false) {
  404. while (false) {
  405. while (false) {
  406. while (false) {
  407. switch ($a) {
  408. case 1:
  409. echo 1;
  410. continue 1_0;
  411. }}}}}}}}}}',
  412. ],
  413. ];
  414. }
  415. }