diff --git a/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php index ebc9b865d6a5f0ae7658c2f2e02dcd883e537dc0..70f6a17fbacf56690330c7c074515fc29920ee9d 100644 --- a/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php @@ -57,15 +57,36 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_ $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) { + if ($tokens[$constructor]['code'] === T_STRING || $tokens[$constructor]['code'] === T_NS_SEPARATOR) { + // Scan to the end of possible string\namespace parts. + $nextConstructorPart = $constructor; + while (true) { + $nextConstructorPart = $phpcsFile->findNext( + PHP_CodeSniffer_Tokens::$emptyTokens, + ($nextConstructorPart + 1), + null, + true, + null, + true + ); + if ($nextConstructorPart === false + || ($tokens[$nextConstructorPart]['code'] !== T_STRING + && $tokens[$nextConstructorPart]['code'] !== T_NS_SEPARATOR) + ) { + break; + } + + $constructor = $nextConstructorPart; + } + $fix = $phpcsFile->addFixableError($error, $constructor, 'ParenthesisMissing'); if ($fix === true) { $phpcsFile->fixer->addContent($constructor, '()'); } } else { $phpcsFile->addError($error, $stackPtr, 'ParenthesisMissing'); - } - } + }//end if + }//end if }//end process() diff --git a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc index aa125f0138a5056705aa852d3f11ab8fe6fa9746..8cb98ff2bd1ded069aa3252098f4c49370a9cdfb 100644 --- a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc +++ b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc @@ -4,3 +4,11 @@ $x = array(new Foo, array()); $y = new Foo; $z = new $class; $bar = new $foo[$x + 1]; + +$obj1 = new DateTime; +$obj1 = new \DateTime; +$obj2 = $obj1->add(new \DateTime); +$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); diff --git a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc.fixed b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc.fixed new file mode 100644 index 0000000000000000000000000000000000000000..acc50796a11c91a1cf33b5cab2de4bdfdc2dcfb6 --- /dev/null +++ b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc.fixed @@ -0,0 +1,18 @@ +<?php + +/** + * @file + */ + +$x = array(new Foo(), array()); +$y = new Foo(); +$z = new $class; +$bar = new $foo[$x + 1]; + +$obj1 = new DateTime(); +$obj1 = new \DateTime(); +$obj2 = $obj1->add(new \DateTime()); +$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()); diff --git a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php index c9aebcadc9d94ca7bd2bb3d2aacd6bd15105d266..3f734ec1fa72f113374c96281177be0a87885ea0 100644 --- a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php +++ b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php @@ -19,6 +19,13 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceUnitTest extends CoderSniffUnitTe 4 => 1, 5 => 1, 6 => 1, + 8 => 1, + 9 => 1, + 10 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 1, ); }//end getErrorList()