Skip to content

Commit c1b0593

Browse files
committed
Fixed bug #781 : Incorrect checking for PHP7 return types on multi-line function declartions
1 parent 578a084 commit c1b0593

File tree

6 files changed

+72
-39
lines changed

6 files changed

+72
-39
lines changed

CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -364,54 +364,43 @@ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $st
364364
}
365365
}//end for
366366

367-
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
368-
// The opening brace needs to be one space away
369-
// from the closing parenthesis.
370-
$next = $tokens[($closeBracket + 1)];
371-
if ($next['code'] !== T_WHITESPACE) {
367+
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
368+
return;
369+
}
370+
371+
// The opening brace needs to be one space away from the closing parenthesis.
372+
$opener = $tokens[$stackPtr]['scope_opener'];
373+
if ($tokens[$opener]['line'] !== $tokens[$closeBracket]['line']) {
374+
$error = 'The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line';
375+
$fix = $phpcsFile->addFixableError($error, $opener, 'NewlineBeforeOpenBrace');
376+
if ($fix === true) {
377+
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($opener - 1), $closeBracket, true);
378+
$phpcsFile->fixer->beginChangeset();
379+
$phpcsFile->fixer->addContent($prev, ' {');
380+
$phpcsFile->fixer->replaceToken($opener, '');
381+
$phpcsFile->fixer->endChangeset();
382+
}
383+
} else {
384+
$prev = $tokens[($opener - 1)];
385+
if ($prev['code'] !== T_WHITESPACE) {
372386
$length = 0;
373-
} else if ($next['content'] === $phpcsFile->eolChar) {
374-
$length = -1;
375387
} else {
376-
$length = strlen($next['content']);
388+
$length = strlen($prev['content']);
377389
}
378390

379391
if ($length !== 1) {
380-
$data = array($length);
381-
$code = 'SpaceBeforeOpenBrace';
382-
383-
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found ';
384-
if ($length === -1) {
385-
$error .= 'newline';
386-
$code = 'NewlineBeforeOpenBrace';
387-
} else {
388-
$error .= '%s spaces';
389-
}
390-
391-
$fix = $phpcsFile->addFixableError($error, ($closeBracket + 1), $code, $data);
392+
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found %s spaces';
393+
$fix = $phpcsFile->addFixableError($error, ($opener - 1), 'SpaceBeforeOpenBrace', array($length));
392394
if ($fix === true) {
393395
if ($length === 0) {
394-
$phpcsFile->fixer->addContent($closeBracket, ' ');
396+
$phpcsFile->fixer->addContentBefore($opener, ' ');
395397
} else {
396-
$phpcsFile->fixer->replaceToken(($closeBracket + 1), ' ');
398+
$phpcsFile->fixer->replaceToken(($opener - 1), ' ');
397399
}
398400
}
399401

400402
return;
401403
}//end if
402-
403-
// And just in case they do something funny before the brace...
404-
$next = $phpcsFile->findNext(
405-
T_WHITESPACE,
406-
($closeBracket + 1),
407-
null,
408-
true
409-
);
410-
411-
if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
412-
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration';
413-
$phpcsFile->addError($error, $next, 'NoSpaceBeforeOpenBrace');
414-
}
415404
}//end if
416405

417406
}//end processMultiLineDeclaration()

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,25 @@ blah
178178
$b = function &() {
179179
echo "hello";
180180
};
181+
182+
function foo(
183+
$param1,
184+
$param2,
185+
$param3,
186+
) : SomeClass {
187+
}
188+
189+
function foo(
190+
$param1,
191+
$param2,
192+
$param3,
193+
): SomeClass {
194+
}
195+
196+
function foo(
197+
$param1,
198+
$param2,
199+
$param3,
200+
): SomeClass // Comment here
201+
{
202+
}

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function getInstalledStandards(
5151
$includeGeneric=false,
5252
$standardsDir=''
5353
) {
54+
5455
}
5556

5657
function &testFunction($arg1,
@@ -182,3 +183,25 @@ function blah()
182183
$b = function &() {
183184
echo "hello";
184185
};
186+
187+
function foo(
188+
$param1,
189+
$param2,
190+
$param3,
191+
) : SomeClass {
192+
}
193+
194+
function foo(
195+
$param1,
196+
$param2,
197+
$param3,
198+
): SomeClass {
199+
}
200+
201+
function foo(
202+
$param1,
203+
$param2,
204+
$param3,
205+
): SomeClass { // Comment here
206+
207+
}

CodeSniffer/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ public function getErrorList()
5151
11 => 1,
5252
14 => 1,
5353
17 => 1,
54-
27 => 1,
5554
44 => 1,
56-
51 => 1,
55+
52 => 1,
5756
61 => 2,
5857
98 => 1,
5958
110 => 2,
@@ -67,6 +66,7 @@ public function getErrorList()
6766
167 => 2,
6867
171 => 1,
6968
173 => 1,
69+
201 => 1,
7070
);
7171

7272
}//end getErrorList()

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public function getErrorList($testFile='MultiLineFunctionDeclarationUnitTest.inc
5353
12 => 1,
5454
13 => 1,
5555
16 => 1,
56-
33 => 1,
5756
36 => 1,
5857
43 => 2,
5958
48 => 1,
@@ -74,7 +73,6 @@ public function getErrorList($testFile='MultiLineFunctionDeclarationUnitTest.inc
7473
13 => 1,
7574
16 => 1,
7675
26 => 1,
77-
33 => 1,
7876
36 => 1,
7977
43 => 2,
8078
48 => 1,

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
7373
- Fixed bug #769 : Incorrect detection of variable reference operator when used with short array syntax
7474
-- Thanks to Klaus Purer for the patch
7575
- Fixed bug #772 : Syntax error when using PHPCBF on alternative style foreach loops
76+
- Fixed bug #781 : Incorrect checking for PHP7 return types on multi-line function declartions
7677
- Fixed bug #782 : Conditional function declarations cause fixing conflicts in Squiz standard
7778
-- Squiz.ControlStructures.ControlSignature no longer enforces a single newline after open brace
7879
-- Squiz.WhiteSpace.ControlStructureSpacing can be used to checl spacing at the start/end of control structures

0 commit comments

Comments
 (0)