Skip to content
Snippets Groups Projects
Commit f4ae9081 authored by Klaus Purer's avatar Klaus Purer
Browse files

fix(ClassCreateInstanceSniff): Fix false positive for PHP 7 return type hint (#2855714)

parent 58b65c0d
No related branches found
No related tags found
No related merge requests found
...@@ -49,8 +49,12 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_ ...@@ -49,8 +49,12 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_
// next semicolon. // next semicolon.
$nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); $nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
// If there is a parenthesis owner then this is not a constructor call, // If there is a parenthesis owner then this is not a constructor call,
// but rather some array or somehting else. // but rather some array or somehting else. There seems to be a bug in PHPCS
if ($nextParenthesis === false || isset($tokens[$nextParenthesis]['parenthesis_owner']) === true) { // that finds PHP 7 array return type hints as parenthesis owner, exclude
// that.
if ($nextParenthesis === false || (isset($tokens[$nextParenthesis]['parenthesis_owner']) === true
&& $tokens[$tokens[$nextParenthesis]['parenthesis_owner']]['code'] !== T_RETURN_TYPE)
) {
$error = 'Calling class constructors must always include parentheses'; $error = 'Calling class constructors must always include parentheses';
$constructor = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); $constructor = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
// We can invoke the fixer if we know this is a static constructor // We can invoke the fixer if we know this is a static constructor
......
...@@ -1547,3 +1547,24 @@ function test22(MyInterface $a) { ...@@ -1547,3 +1547,24 @@ function test22(MyInterface $a) {
function test23(): TestReturnType { function test23(): TestReturnType {
return foo(); return foo();
} }
/**
* Test class.
*/
class Test2 {
/**
* Using PHP 7 return type hints is fine.
*
* @return ValidatorInterface[]
* The validators.
*/
public function getValidators(): array {
return [
new PublishedNodesValidator(),
new MinimumNodesValidator($this->nrOfArticles),
new AccessibleOnCurrentDomainValidator($this->sectionService),
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment