Skip to content

Commit 188b4c3

Browse files
committed
Tokenizer/PHP: goto is a switch case/default terminating statement
`goto` was not consistently recognized as a switch - case/default terminating statement throughout the code base. Fixed now. Includes tests via the `SwitchDeclaration` sniffs.
1 parent 70426bf commit 188b4c3

File tree

11 files changed

+78
-0
lines changed

11 files changed

+78
-0
lines changed

src/Files/File.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,7 @@ public function findStartOfStatement($start, $ignore=null)
24972497
&& $this->tokens[$i]['code'] !== T_CONTINUE
24982498
&& $this->tokens[$i]['code'] !== T_THROW
24992499
&& $this->tokens[$i]['code'] !== T_EXIT
2500+
&& $this->tokens[$i]['code'] !== T_GOTO
25002501
) {
25012502
// Found the end of the previous scope block.
25022503
return $lastNotEmpty;

src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
380380
T_CONTINUE => T_CONTINUE,
381381
T_THROW => T_THROW,
382382
T_EXIT => T_EXIT,
383+
T_GOTO => T_GOTO,
383384
];
384385

385386
$terminator = $phpcsFile->findStartOfStatement(($lastToken - 1));

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,16 @@ switch (rand()) {
596596
default:
597597
break;
598598
}
599+
600+
// Fix: goto should be recognized as terminating statement.
601+
switch ( $a ) {
602+
case 1:
603+
doSomething();
604+
goto jumpOut;
605+
default:
606+
$other = $code;
607+
break;
608+
}
609+
610+
jumpOut:
611+
doSomething();

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,16 @@ switch (rand()) {
591591
default:
592592
break;
593593
}
594+
595+
// Fix: goto should be recognized as terminating statement.
596+
switch ( $a ) {
597+
case 1:
598+
doSomething();
599+
goto jumpOut;
600+
default:
601+
$other = $code;
602+
break;
603+
}
604+
605+
jumpOut:
606+
doSomething();

src/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public function process(File $phpcsFile, $stackPtr)
142142
|| $tokens[$nextBreak]['code'] === T_CONTINUE
143143
|| $tokens[$nextBreak]['code'] === T_THROW
144144
|| $tokens[$nextBreak]['code'] === T_EXIT
145+
|| $tokens[$nextBreak]['code'] === T_GOTO
145146
) {
146147
if ($tokens[$nextBreak]['scope_condition'] === $nextCase) {
147148
// Only need to check a couple of things once, even if the

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,16 @@ $foo = $foo ?
331331
}
332332
} :
333333
null;
334+
335+
// Fix: goto should be recognized as terminating statement.
336+
switch ( $a ) {
337+
case 1:
338+
doSomething();
339+
goto jumpOut;
340+
default:
341+
$other = $code;
342+
goto jumpOut;
343+
}
344+
345+
jumpOut:
346+
doSomething();

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,17 @@ $foo = $foo ?
340340
}
341341
} :
342342
null;
343+
344+
// Fix: goto should be recognized as terminating statement.
345+
switch ( $a ) {
346+
case 1:
347+
doSomething();
348+
goto jumpOut;
349+
350+
default:
351+
$other = $code;
352+
goto jumpOut;
353+
}
354+
355+
jumpOut:
356+
doSomething();

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function getErrorList()
7676
327 => 1,
7777
329 => 1,
7878
330 => 1,
79+
339 => 2,
80+
342 => 1,
7981
];
8082

8183
}//end getErrorList()

src/Tokenizers/PHP.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ class PHP extends Tokenizer
238238
T_CONTINUE => T_CONTINUE,
239239
T_THROW => T_THROW,
240240
T_EXIT => T_EXIT,
241+
T_GOTO => T_GOTO,
241242
],
242243
'strict' => true,
243244
'shared' => true,
@@ -258,6 +259,7 @@ class PHP extends Tokenizer
258259
T_CONTINUE => T_CONTINUE,
259260
T_THROW => T_THROW,
260261
T_EXIT => T_EXIT,
262+
T_GOTO => T_GOTO,
261263
],
262264
'strict' => true,
263265
'shared' => true,

tests/Core/File/FindStartOfStatementTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,20 @@ switch ($foo) {
157157
/* testInsideCaseThrowStatement */
158158
throw new Exception();
159159

160+
case 6:
161+
$var = doSomething();
162+
/* testInsideCaseGotoStatement */
163+
goto myLabel;
164+
160165
/* testDefaultStatement */
161166
default:
162167
/* testInsideDefaultContinueStatement */
163168
continue $var;
164169
}
165170

171+
myLabel:
172+
do_something();
173+
166174
match ($var) {
167175
true =>
168176
/* test437ClosureDeclaration */

0 commit comments

Comments
 (0)