Skip to content

Commit 4f7ccf3

Browse files
authored
Merge pull request #916 from PHPCSStandards/feature/tokenizer-php-goto-is-terminating-statement
Tokenizer/PHP: goto is a switch case/default terminating statement
2 parents 835d578 + 4f50d88 commit 4f7ccf3

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
@@ -2558,6 +2558,7 @@ public function findStartOfStatement($start, $ignore=null)
25582558
&& $this->tokens[$i]['code'] !== T_CONTINUE
25592559
&& $this->tokens[$i]['code'] !== T_THROW
25602560
&& $this->tokens[$i]['code'] !== T_EXIT
2561+
&& $this->tokens[$i]['code'] !== T_GOTO
25612562
) {
25622563
// Found the end of the previous scope block.
25632564
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
@@ -153,6 +153,7 @@ public function process(File $phpcsFile, $stackPtr)
153153
|| $tokens[$nextBreak]['code'] === T_CONTINUE
154154
|| $tokens[$nextBreak]['code'] === T_THROW
155155
|| $tokens[$nextBreak]['code'] === T_EXIT
156+
|| $tokens[$nextBreak]['code'] === T_GOTO
156157
) {
157158
if ($tokens[$nextBreak]['scope_condition'] === $nextCase) {
158159
// 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
@@ -80,6 +80,8 @@ public function getErrorList($testFile='')
8080
327 => 1,
8181
329 => 1,
8282
330 => 1,
83+
339 => 2,
84+
342 => 1,
8385
];
8486

8587
case 'SwitchDeclarationUnitTest.js':

src/Tokenizers/PHP.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class PHP extends Tokenizer
231231
T_CONTINUE => T_CONTINUE,
232232
T_THROW => T_THROW,
233233
T_EXIT => T_EXIT,
234+
T_GOTO => T_GOTO,
234235
],
235236
'strict' => true,
236237
'shared' => true,
@@ -251,6 +252,7 @@ class PHP extends Tokenizer
251252
T_CONTINUE => T_CONTINUE,
252253
T_THROW => T_THROW,
253254
T_EXIT => T_EXIT,
255+
T_GOTO => T_GOTO,
254256
],
255257
'strict' => true,
256258
'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)