diff --git a/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php index 281a43eb50208e10ef63f94559a234fedf4ca3cf..bfc38d0a3dffcee901f7a3bd90d3e4ee92bb6679 100644 --- a/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php @@ -49,8 +49,12 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_ // next semicolon. $nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); // If there is a parenthesis owner then this is not a constructor call, - // but rather some array or somehting else. - if ($nextParenthesis === false || isset($tokens[$nextParenthesis]['parenthesis_owner']) === true) { + // but rather some array or somehting else. There seems to be a bug in PHPCS + // 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'; $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 diff --git a/coder_sniffer/Drupal/Test/good/good.php b/coder_sniffer/Drupal/Test/good/good.php index 12507ba26fac3c9d611ef2c185c401677d8744c4..d49a4267e6534e6bc3d615edbefb32f32b76979e 100644 --- a/coder_sniffer/Drupal/Test/good/good.php +++ b/coder_sniffer/Drupal/Test/good/good.php @@ -1547,3 +1547,24 @@ function test22(MyInterface $a) { function test23(): TestReturnType { 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), + ]; + } + +}