Skip to content

Tokenizer/PHP: goto is a switch case/default terminating statement #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,7 @@ public function findStartOfStatement($start, $ignore=null)
&& $this->tokens[$i]['code'] !== T_CONTINUE
&& $this->tokens[$i]['code'] !== T_THROW
&& $this->tokens[$i]['code'] !== T_EXIT
&& $this->tokens[$i]['code'] !== T_GOTO
) {
// Found the end of the previous scope block.
return $lastNotEmpty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
T_CONTINUE => T_CONTINUE,
T_THROW => T_THROW,
T_EXIT => T_EXIT,
T_GOTO => T_GOTO,
];

$terminator = $phpcsFile->findStartOfStatement(($lastToken - 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,16 @@ switch (rand()) {
default:
break;
}

// Fix: goto should be recognized as terminating statement.
switch ( $a ) {
case 1:
doSomething();
goto jumpOut;
default:
$other = $code;
break;
}

jumpOut:
doSomething();
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,16 @@ switch (rand()) {
default:
break;
}

// Fix: goto should be recognized as terminating statement.
switch ( $a ) {
case 1:
doSomething();
goto jumpOut;
default:
$other = $code;
break;
}

jumpOut:
doSomething();
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function process(File $phpcsFile, $stackPtr)
|| $tokens[$nextBreak]['code'] === T_CONTINUE
|| $tokens[$nextBreak]['code'] === T_THROW
|| $tokens[$nextBreak]['code'] === T_EXIT
|| $tokens[$nextBreak]['code'] === T_GOTO
) {
if ($tokens[$nextBreak]['scope_condition'] === $nextCase) {
// Only need to check a couple of things once, even if the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,16 @@ $foo = $foo ?
}
} :
null;

// Fix: goto should be recognized as terminating statement.
switch ( $a ) {
case 1:
doSomething();
goto jumpOut;
default:
$other = $code;
goto jumpOut;
}

jumpOut:
doSomething();
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,17 @@ $foo = $foo ?
}
} :
null;

// Fix: goto should be recognized as terminating statement.
switch ( $a ) {
case 1:
doSomething();
goto jumpOut;

default:
$other = $code;
goto jumpOut;
}

jumpOut:
doSomething();
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function getErrorList($testFile='')
327 => 1,
329 => 1,
330 => 1,
339 => 2,
342 => 1,
];

case 'SwitchDeclarationUnitTest.js':
Expand Down
2 changes: 2 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class PHP extends Tokenizer
T_CONTINUE => T_CONTINUE,
T_THROW => T_THROW,
T_EXIT => T_EXIT,
T_GOTO => T_GOTO,
],
'strict' => true,
'shared' => true,
Expand All @@ -251,6 +252,7 @@ class PHP extends Tokenizer
T_CONTINUE => T_CONTINUE,
T_THROW => T_THROW,
T_EXIT => T_EXIT,
T_GOTO => T_GOTO,
],
'strict' => true,
'shared' => true,
Expand Down
8 changes: 8 additions & 0 deletions tests/Core/File/FindStartOfStatementTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,20 @@ switch ($foo) {
/* testInsideCaseThrowStatement */
throw new Exception();

case 6:
$var = doSomething();
/* testInsideCaseGotoStatement */
goto myLabel;

/* testDefaultStatement */
default:
/* testInsideDefaultContinueStatement */
continue $var;
}

myLabel:
do_something();

match ($var) {
true =>
/* test437ClosureDeclaration */
Expand Down
10 changes: 10 additions & 0 deletions tests/Core/File/FindStartOfStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@ public static function dataFindStartInsideSwitchCaseDefaultStatements()
'targets' => T_CLOSE_PARENTHESIS,
'expectedTarget' => T_THROW,
],
'Goto should be start for contents of the goto statement - goto label' => [
'testMarker' => '/* testInsideCaseGotoStatement */',
'targets' => T_STRING,
'expectedTarget' => T_GOTO,
],
'Goto should be start for contents of the goto statement - semicolon' => [
'testMarker' => '/* testInsideCaseGotoStatement */',
'targets' => T_SEMICOLON,
'expectedTarget' => T_GOTO,
],
'Default keyword should be start of default statement - default itself' => [
'testMarker' => '/* testDefaultStatement */',
'targets' => T_DEFAULT,
Expand Down