123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\ControlStructure;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ControlStructure\SwitchContinueToBreakFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchContinueToBreakFixer>
- */
- final class SwitchContinueToBreakFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield 'alternative syntax |' => [
- '<?php
- switch($foo):
- case 3:
- continue;
- endswitch?>
- ',
- ];
- yield 'alternative syntax ||' => [
- '<?php
- foreach ([] as $v) {
- continue;
- }
- if ($foo != 0) {
- }
- switch ($foo):
- endswitch;',
- ];
- yield 'nested switches' => [
- '<?php
- switch($z) {
- case 1:
- switch($x) {
- case 2:
- switch($y) {
- case 3:
- switch($z) {
- case 4:
- break; // z.1
- }
- break; // z
- }
- break; // y
- }
- break; // x
- }
- ',
- '<?php
- switch($z) {
- case 1:
- switch($x) {
- case 2:
- switch($y) {
- case 3:
- switch($z) {
- case 4:
- continue; // z.1
- }
- continue; // z
- }
- continue; // y
- }
- continue; // x
- }
- ',
- ];
- yield 'nested 2' => [
- '<?php
- while ($foo) {
- switch ($bar) {
- case "baz":
- while ($xyz) {
- switch($zA) {
- case 1:
- break 3; // fix
- }
- if ($z) continue;
- if ($zz){ continue; }
- if ($zzz) continue 3;
- if ($zzz){ continue 3; }
- if ($b) break 2; // fix
- }
- switch($zG) {
- case 1:
- switch($zF) {
- case 1:
- break 1; // fix
- case 2:
- break 2; // fix
- case 3:
- break 3; // fix
- case 4:
- while($a){
- while($a){
- while($a){
- if ($a) {
- break 4; // fix
- } elseif($z) {
- break 5; // fix
- } else {
- break 6; // fix
- }
- continue 7;
- }
- }
- }
- continue 4;
- }
- break 2; // fix
- }
- }
- }
- ',
- '<?php
- while ($foo) {
- switch ($bar) {
- case "baz":
- while ($xyz) {
- switch($zA) {
- case 1:
- continue 3; // fix
- }
- if ($z) continue;
- if ($zz){ continue; }
- if ($zzz) continue 3;
- if ($zzz){ continue 3; }
- if ($b) continue 2; // fix
- }
- switch($zG) {
- case 1:
- switch($zF) {
- case 1:
- continue 1; // fix
- case 2:
- continue 2; // fix
- case 3:
- continue 3; // fix
- case 4:
- while($a){
- while($a){
- while($a){
- if ($a) {
- continue 4; // fix
- } elseif($z) {
- continue 5; // fix
- } else {
- continue 6; // fix
- }
- continue 7;
- }
- }
- }
- continue 4;
- }
- continue 2; // fix
- }
- }
- }
- ',
- ];
- yield 'nested do while' => [
- '<?php
- switch ($a) {
- case 1:
- do {
- switch ($a) {
- case 1:
- do {
- switch ($a) {
- case 1:
- do {
- continue;
- } while (false);
- break;
- }
- continue;
- } while (false);
- break;
- }
- continue;
- } while (false);
- break;
- }
- ',
- '<?php
- switch ($a) {
- case 1:
- do {
- switch ($a) {
- case 1:
- do {
- switch ($a) {
- case 1:
- do {
- continue;
- } while (false);
- continue;
- }
- continue;
- } while (false);
- continue;
- }
- continue;
- } while (false);
- continue;
- }
- ',
- ];
- yield [
- '<?php
- switch(foo()) {
- case 1: while(bar($i))continue;break;
- default: echo 7;
- }
- ',
- '<?php
- switch(foo()) {
- case 1: while(bar($i))continue;continue;
- default: echo 7;
- }
- ',
- ];
- yield 'do not fix cases' => [
- '<?php
- switch($a) {
- case 1:
- while (false) {
- continue;
- }
- while (false) break 1;
- do {
- continue;
- } while (false);
- for ($a = 0; $a < 1; ++$a) {
- continue;
- }
- foreach ($a as $b) continue;
- for (; $i < 1; ++$i) break 1; echo $i;
- for (;;) continue;
- while(false) continue;
- while(false) continue?><?php
- // so bad and such a mess, not worth adding a ton of logic to fix this
- switch($z) {
- case 1:
- continue ?> <?php 23;
- case 2:
- continue 1?> <?php + $a;
- }
- }
- ',
- ];
- yield 'nested while, do not fix' => [
- '<?php
- switch(foo()) {
- case 1: while(bar($i)){ --$i; echo 1; continue;}break;
- default: echo 8;
- }',
- ];
- yield 'not int cases' => [
- '<?php
- while($b) {
- switch($a) {
- case 1:
- break 01;
- case 2:
- break 0x1;
- case 22:
- break 0x01;
- case 3:
- break 0b1;
- case 32:
- break 0b0001;
- case 4:
- break 0b000001;
- // do not fix
- case 1:
- continue 02;
- case 2:
- continue 0x2;
- case 3:
- continue 0b10;
- }
- }',
- '<?php
- while($b) {
- switch($a) {
- case 1:
- continue 01;
- case 2:
- continue 0x1;
- case 22:
- continue 0x01;
- case 3:
- continue 0b1;
- case 32:
- continue 0b0001;
- case 4:
- continue 0b000001;
- // do not fix
- case 1:
- continue 02;
- case 2:
- continue 0x2;
- case 3:
- continue 0b10;
- }
- }',
- ];
- yield 'deep nested case' => [
- '<?php
- switch ($a) {
- case $b:
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- switch ($a) {
- case 1:
- echo 1;
- break 10;
- }}}}}}}}}}',
- '<?php
- switch ($a) {
- case $b:
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- switch ($a) {
- case 1:
- echo 1;
- continue 10;
- }}}}}}}}}}',
- ];
- yield 'numeric literal separator' => [
- '<?php
- switch($a) {
- case "a":
- echo __FILE__;
- break;
- }
- ',
- '<?php
- switch($a) {
- case "a":
- echo __FILE__;
- continue;
- }
- ',
- [
- '<?php
- switch ($a) {
- case $b:
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- switch ($a) {
- case 1:
- echo 1;
- break 1_0;
- }}}}}}}}}}',
- '<?php
- switch ($a) {
- case $b:
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- while (false) {
- switch ($a) {
- case 1:
- echo 1;
- continue 1_0;
- }}}}}}}}}}',
- ],
- ];
- }
- }
|