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

fix(ClassCreateInstanceSniff): Improve missing parenthesis detection within arrays (#2855714)

parent f4ae9081
No related branches found
No related tags found
No related merge requests found
...@@ -45,16 +45,16 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_ ...@@ -45,16 +45,16 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_
{ {
$tokens = $phpcsFile->getTokens(); $tokens = $phpcsFile->getTokens();
$commaOrColon = $phpcsFile->findNext([T_SEMICOLON, T_COLON, T_COMMA], ($stackPtr + 1));
if ($commaOrColon === false) {
// Syntax error, nothing we can do.
return;
}
// Search for an opening parenthesis in the current statement until the // Search for an opening parenthesis in the current statement until the
// next semicolon. // next semicolon or comma.
$nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); $nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1), $commaOrColon);
// If there is a parenthesis owner then this is not a constructor call, if ($nextParenthesis === false) {
// 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'; $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
......
...@@ -14,3 +14,24 @@ $obj2 = $obj1->add(new Vendor\DateTools\DateInterval); ...@@ -14,3 +14,24 @@ $obj2 = $obj1->add(new Vendor\DateTools\DateInterval);
$obj2 = $obj1->add(new \Vendor\DateTools\DateInterval); $obj2 = $obj1->add(new \Vendor\DateTools\DateInterval);
$bar = new $foo[$x + 1][$y + 1]; $bar = new $foo[$x + 1][$y + 1];
/**
* 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),
];
}
}
...@@ -21,3 +21,24 @@ $obj2 = $obj1->add(new DateInterval()); ...@@ -21,3 +21,24 @@ $obj2 = $obj1->add(new DateInterval());
$obj2 = $obj1->add(new DateInterval()); $obj2 = $obj1->add(new DateInterval());
$bar = new $foo[$x + 1][$y + 1](); $bar = new $foo[$x + 1][$y + 1]();
/**
* 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),
];
}
}
...@@ -27,6 +27,7 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceUnitTest extends CoderSniffUnitTe ...@@ -27,6 +27,7 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceUnitTest extends CoderSniffUnitTe
13 => 1, 13 => 1,
14 => 1, 14 => 1,
16 => 1, 16 => 1,
31 => 1,
); );
}//end getErrorList() }//end getErrorList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment