Skip to content

Commit e988f62

Browse files
committed
Fixed bug #2511 : PSR2 standard not checking if closing paren of single-line function declaration is on new line
1 parent 99091ca commit e988f62

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
7575
- Fixed bug #2498 : Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed autofix breaks heredoc
7676
- Fixed bug #2502 : Generic.WhiteSpace.ScopeIndent false positives with nested switch indentation and case fall-through
7777
- Fixed bug #2504 : Generic.WhiteSpace.ScopeIndent false positives with nested arrays and nowdoc string
78+
- Fixed bug #2511 : PSR2 standard not checking if closing paren of single-line function declaration is on new line
7879
</notes>
7980
<contents>
8081
<dir name="/">

src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,54 @@ public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tok
9393
}//end isMultiLineDeclaration()
9494

9595

96+
/**
97+
* Processes single-line declarations.
98+
*
99+
* Just uses the Generic BSD-Allman brace sniff.
100+
*
101+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
102+
* @param int $stackPtr The position of the current token
103+
* in the stack passed in $tokens.
104+
* @param array $tokens The stack of tokens that make up
105+
* the file.
106+
*
107+
* @return void
108+
*/
109+
public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens)
110+
{
111+
// We do everything the parent sniff does, and a bit more because we
112+
// define multi-line declarations a bit differently.
113+
parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
114+
115+
$openingBracket = $tokens[$stackPtr]['parenthesis_opener'];
116+
$closingBracket = $tokens[$stackPtr]['parenthesis_closer'];
117+
118+
$prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingBracket - 1), $openingBracket, true);
119+
if ($tokens[$prevNonWhiteSpace]['line'] !== $tokens[$closingBracket]['line']) {
120+
$error = 'There must not be a newline before the closing parenthesis of a single-line function declaration';
121+
122+
if (isset(Tokens::$emptyTokens[$tokens[$prevNonWhiteSpace]['code']]) === true) {
123+
$phpcsFile->addError($error, $closingBracket, 'CloseBracketNewLine');
124+
} else {
125+
$fix = $phpcsFile->addFixableError($error, $closingBracket, 'CloseBracketNewLine');
126+
if ($fix === true) {
127+
$phpcsFile->fixer->beginChangeset();
128+
for ($i = ($closingBracket - 1); $i > $openingBracket; $i--) {
129+
if ($tokens[$i]['code'] !== T_WHITESPACE) {
130+
break;
131+
}
132+
133+
$phpcsFile->fixer->replaceToken($i, '');
134+
}
135+
136+
$phpcsFile->fixer->endChangeset();
137+
}
138+
}
139+
}//end if
140+
141+
}//end processSingleLineDeclaration()
142+
143+
96144
/**
97145
* Processes multi-line declarations.
98146
*

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,15 @@ function __construct(
177177
$foo, // phpcs:ignore Standard.Category.Sniff -- for reasons.
178178
$bar
179179
) {}
180+
181+
public function hello(string $greeting
182+
) {
183+
}
184+
185+
public function hello(string $greeting // cant fix this
186+
) {
187+
}
188+
189+
public function hello(string $greeting // phpcs:ignore Standard.Category.Sniff -- for reasons.
190+
) {
191+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,17 @@ function __construct(
189189
$foo, // phpcs:ignore Standard.Category.Sniff -- for reasons.
190190
$bar
191191
) {}
192+
193+
public function hello(string $greeting)
194+
{
195+
}
196+
197+
public function hello(string $greeting // cant fix this
198+
)
199+
{
200+
}
201+
202+
public function hello(string $greeting // phpcs:ignore Standard.Category.Sniff -- for reasons.
203+
)
204+
{
205+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function getErrorList($testFile='MultiLineFunctionDeclarationUnitTest.inc
5050
142 => 1,
5151
158 => 1,
5252
160 => 1,
53+
182 => 2,
54+
186 => 2,
55+
190 => 2,
5356
];
5457
} else {
5558
$errors = [

0 commit comments

Comments
 (0)