Skip to content

Commit d55deff

Browse files
committed
Generic/ArbitraryParenthesesSpacing: improve handling of switch-case scope closers
Follow up on squizlabs/PHP_CodeSniffer 2826 which was fixed via squizlabs/PHP_CodeSniffer 2876. The sniff explicitly only intends to handle _arbitrary_ parentheses and tries to avoid clashes with sniffs handling the parentheses spacing of function calls and language construct invocations. This is done by the sniff bowing out early when the token before the open parenthesis is a token which can be used in a (variable) function call or one of a select list of reserved keywords. The previous PR tweaked the "bowing out" to allow the sniff to operate on arbitrary parentheses which directly follow the close curly brace of a a previous scope. The additional condition added was, however, not limited to `T_CLOSE_CURLY_BRACKET` tokens. This has led to a new false positive: When a `die()`/`exit()` is used within a `switch-case` statement, the `die`/`exit` keyword will be regarded as a scope closer for the `case` statement, in which case, the parentheses for the `die`/`exit` would now be treated as arbitrary instead of as belonging to the `die`/`exit` keyword. This commit fixes this by limiting the previously introduced check for a scope closer to curly braces only. Includes tests proving the bug and safeguarding the fix.
1 parent c68b879 commit d55deff

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public function process(File $phpcsFile, $stackPtr)
103103
$preOpener = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($opener - 1), null, true);
104104
if ($preOpener !== false
105105
&& isset($this->ignoreTokens[$tokens[$preOpener]['code']]) === true
106-
&& isset($tokens[$preOpener]['scope_condition']) === false
106+
&& ($tokens[$preOpener]['code'] !== T_CLOSE_CURLY_BRACKET
107+
|| isset($tokens[$preOpener]['scope_condition']) === false )
107108
) {
108109
// Function or language construct call.
109110
return;

src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc

+10
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,13 @@ class NonArbitraryParenthesesWithKeywords {
184184
$b = match ( $a ) {
185185
1 => true,
186186
};
187+
188+
// Parentheses after die/exit in a switch case should be ignored.
189+
switch ( $type ) {
190+
case A:
191+
exit( 1 );
192+
case B:
193+
die();
194+
default:
195+
break;
196+
}

src/Standards/Generic/Tests/WhiteSpace/ArbitraryParenthesesSpacingUnitTest.1.inc.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,13 @@ class NonArbitraryParenthesesWithKeywords {
172172
$b = match ( $a ) {
173173
1 => true,
174174
};
175+
176+
// Parentheses after die/exit in a switch case should be ignored.
177+
switch ( $type ) {
178+
case A:
179+
exit( 1 );
180+
case B:
181+
die();
182+
default:
183+
break;
184+
}

0 commit comments

Comments
 (0)