Skip to content
Snippets Groups Projects
Commit d35c1629 authored by Anoop John's avatar Anoop John Committed by Klaus Purer
Browse files

Issue #2573195 by pfrenssen, anoopjohn, attiks: Not all...

Issue #2573195 by pfrenssen, anoopjohn, attiks: Not all Drupal.Classes.ClassCreateInstance violations are marked as fixable
parent 2a7a3fab
Branches 1.0.x
No related tags found
No related merge requests found
......@@ -55,9 +55,15 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_
if ($nextParenthesis === false || isset($tokens[$nextParenthesis]['parenthesis_owner']) === true) {
$error = 'Calling class constructors must always include parentheses';
$constructor = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
// We can only invoke the fixer if we know this is a static constructor
// function call.
if ($tokens[$constructor]['code'] === T_STRING || $tokens[$constructor]['code'] === T_NS_SEPARATOR) {
// We can invoke the fixer if we know this is a static constructor
// function call or constructor calls with namespaces, example
// "new \DOMDocument;" or constructor with class names in variables
// "new $controller;".
if ($tokens[$constructor]['code'] === T_STRING
|| $tokens[$constructor]['code'] === T_NS_SEPARATOR
|| ($tokens[$constructor]['code'] === T_VARIABLE
&& $tokens[($constructor + 1)]['code'] === T_SEMICOLON)
) {
// Scan to the end of possible string\namespace parts.
$nextConstructorPart = $constructor;
while (true) {
......@@ -83,6 +89,23 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_
if ($fix === true) {
$phpcsFile->fixer->addContent($constructor, '()');
}
// We can invoke the fixer if we know this is a
// constructor call with class names in an array
// example "new $controller[$i];".
} else if ($tokens[$constructor]['code'] === T_VARIABLE
&& $tokens[($constructor + 1)]['code'] === T_OPEN_SQUARE_BRACKET
) {
// Scan to the end of possible multilevel arrays.
$nextConstructorPart = $constructor;
do {
$nextConstructorPart = $tokens[($nextConstructorPart + 1)]['bracket_closer'];
} while ($tokens[($nextConstructorPart + 1)]['code'] === T_OPEN_SQUARE_BRACKET);
$fix = $phpcsFile->addFixableError($error, $nextConstructorPart, 'ParenthesisMissing');
if ($fix === true) {
$phpcsFile->fixer->addContent($nextConstructorPart, '()');
}
} else {
$phpcsFile->addError($error, $stackPtr, 'ParenthesisMissing');
}//end if
......
......@@ -12,3 +12,5 @@ $obj2 = $obj1->add(new Vendor);
$obj2 = $obj1->add(new Vendor\DateTools);
$obj2 = $obj1->add(new Vendor\DateTools\DateInterval);
$obj2 = $obj1->add(new \Vendor\DateTools\DateInterval);
$bar = new $foo[$x + 1][$y + 1];
......@@ -9,8 +9,8 @@ use Vendor\DateTools;
$x = array(new Foo(), array());
$y = new Foo();
$z = new $class;
$bar = new $foo[$x + 1];
$z = new $class();
$bar = new $foo[$x + 1]();
$obj1 = new DateTime();
$obj1 = new \DateTime();
......@@ -19,3 +19,5 @@ $obj2 = $obj1->add(new Vendor());
$obj2 = $obj1->add(new DateTools());
$obj2 = $obj1->add(new DateInterval());
$obj2 = $obj1->add(new DateInterval());
$bar = new $foo[$x + 1][$y + 1]();
......@@ -26,6 +26,7 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceUnitTest extends CoderSniffUnitTe
12 => 1,
13 => 1,
14 => 1,
16 => 1,
);
}//end getErrorList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment