Skip to content

Commit df7af3c

Browse files
committed
PSR2.ControlStructures.ControlStructureSpacing now checks whitespace before the closing parenthesis of multi-line control structures (ref #2499)
1 parent ebc9b7a commit df7af3c

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
3737
- Generic.CodeAnalysis.EmptyPhpStatement now reports unnecessary semicolons after control structure closing braces
3838
-- Thanks to Vincent Langlet for the patch
3939
- Generic.WhiteSpace.ScopeIndent now supports static arrow functions
40+
- PSR2.ControlStructures.ControlStructureSpacing now checks whitespace before the closing parenthesis of multi-line control structures
41+
-- Previously, it incorrectly applied the whitespace check for single-line definitions only
4042
- Fixed bug #2638 : Squiz.CSS.DuplicateClassDefinitionSniff sees comments as part of the class name
4143
-- Thanks to Raphael Horber for the patch
4244
- Fixed bug #2674 : Squiz.Functions.FunctionDeclarationArgumentSpacing prints wrong argument name in error message

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public function process(File $phpcsFile, $stackPtr)
108108
}
109109
}//end if
110110

111-
if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) {
111+
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($parenCloser - 1), $parenOpener, true);
112+
if ($tokens[$prev]['line'] === $tokens[$parenCloser]['line']) {
112113
$spaceBeforeClose = 0;
113114
if ($tokens[($parenCloser - 1)]['code'] === T_WHITESPACE) {
114115
$spaceBeforeClose = strlen(ltrim($tokens[($parenCloser - 1)]['content'], $phpcsFile->eolChar));

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,16 @@ foreach ( $something as $blah => $that ) {}
5555
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 0
5656

5757
$binary = b"binary string";
58+
59+
if ($expr1
60+
&& $expr2 ) {
61+
}
62+
63+
if ($expr1
64+
&& $expr2 /* comment */ ) {
65+
}
66+
67+
if ($expr1
68+
&& $expr2
69+
/* comment */ ) {
70+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ foreach ( $something as $blah => $that ) {}
5454
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 0
5555

5656
$binary = b"binary string";
57+
58+
if ($expr1
59+
&& $expr2) {
60+
}
61+
62+
if ($expr1
63+
&& $expr2 /* comment */) {
64+
}
65+
66+
if ($expr1
67+
&& $expr2
68+
/* comment */) {
69+
}

src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function getErrorList()
3333
31 => 1,
3434
51 => 2,
3535
53 => 2,
36+
60 => 1,
37+
64 => 1,
38+
69 => 1,
3639
];
3740

3841
}//end getErrorList()

0 commit comments

Comments
 (0)