diff --git a/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php index 35f0de24f300cb2b3107da4de7bd3393df7efecb..ebc9b865d6a5f0ae7658c2f2e02dcd883e537dc0 100644 --- a/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Classes/ClassCreateInstanceSniff.php @@ -4,10 +4,9 @@ * * PHP version 5 * - * @category PHP - * @package PHP_CodeSniffer - * @author Peter Philipp <peter.philipp@cando-image.com> - * @link http://pear.php.net/package/PHP_CodeSniffer + * @category PHP + * @package PHP_CodeSniffer + * @link http://pear.php.net/package/PHP_CodeSniffer */ /** @@ -15,10 +14,9 @@ * * Checks the declaration of the class is correct. * - * @category PHP - * @package PHP_CodeSniffer - * @author Peter Philipp <peter.philipp@cando-image.com> - * @link http://pear.php.net/package/PHP_CodeSniffer + * @category PHP + * @package PHP_CodeSniffer + * @link http://pear.php.net/package/PHP_CodeSniffer */ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_Sniff { @@ -31,9 +29,7 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_ */ public function register() { - return array( - T_NEW, - ); + return array(T_NEW); }//end register() @@ -51,21 +47,27 @@ class Drupal_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_ { $tokens = $phpcsFile->getTokens(); - $nextParenthesis = $phpcsFile->findNext(array(T_OPEN_PARENTHESIS,T_SEMICOLON), $stackPtr, null, false, null, true); - if ($tokens[$nextParenthesis]['code'] != T_OPEN_PARENTHESIS || $tokens[$nextParenthesis]['line'] != $tokens[$stackPtr]['line']) { - $error = 'Calling class constructors must always include parentheses'; - $phpcsFile->addError($error, $nextParenthesis); - return; - } - - if ($tokens[$nextParenthesis-1]['code'] == T_WHITESPACE) { - $error = 'Between the class name and the opening parenthesis spaces are not welcome'; - $phpcsFile->addError($error, $nextParenthesis-1); - return; + // Search for an opening parenthesis in the current statement untill the + // 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) { + $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) { + $fix = $phpcsFile->addFixableError($error, $constructor, 'ParenthesisMissing'); + if ($fix === true) { + $phpcsFile->fixer->addContent($constructor, '()'); + } + } else { + $phpcsFile->addError($error, $stackPtr, 'ParenthesisMissing'); + } } + }//end process() }//end class - -?> diff --git a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc new file mode 100644 index 0000000000000000000000000000000000000000..aa125f0138a5056705aa852d3f11ab8fe6fa9746 --- /dev/null +++ b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.inc @@ -0,0 +1,6 @@ +<?php + +$x = array(new Foo, array()); +$y = new Foo; +$z = new $class; +$bar = new $foo[$x + 1]; diff --git a/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c9aebcadc9d94ca7bd2bb3d2aacd6bd15105d266 --- /dev/null +++ b/coder_sniffer/Drupal/Test/Classes/ClassCreateInstanceUnitTest.php @@ -0,0 +1,42 @@ +<?php + +class Drupal_Sniffs_Classes_ClassCreateInstanceUnitTest extends CoderSniffUnitTest +{ + + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @return array(int => int) + */ + public function getErrorList($testFile) + { + return array( + 3 => 1, + 4 => 1, + 5 => 1, + 6 => 1, + ); + + }//end getErrorList() + + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array(int => int) + */ + public function getWarningList($testFile) + { + return array(); + + }//end getWarningList() + + +}//end class diff --git a/coder_sniffer/Drupal/Test/bad/BadUnitTest.php b/coder_sniffer/Drupal/Test/bad/BadUnitTest.php index e1cf8f55cd692a48c2d2dc49f313f4e380a61b1d..c6a77fc6d2c11f73659597c700825e8c37137343 100644 --- a/coder_sniffer/Drupal/Test/bad/BadUnitTest.php +++ b/coder_sniffer/Drupal/Test/bad/BadUnitTest.php @@ -253,7 +253,7 @@ class Drupal_BadUnitTest extends CoderSniffUnitTest 379 => 1, 383 => 1, 384 => 1, - 385 => 2, + 385 => 1, 386 => 1, 387 => 1, 389 => 1, diff --git a/composer.lock b/composer.lock index 598a2b38a665bbcd9d17ea8db739a80e900ca009..7aeec95e4d7cbbf8c1fc896a1ae01d0ef87867e0 100644 --- a/composer.lock +++ b/composer.lock @@ -335,16 +335,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.3.0", + "version": "1.4.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "f8d5d08c56de5cfd592b3340424a81733259a876" + "reference": "db32c18eba00b121c145575fcbcd4d4d24e6db74" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/f8d5d08c56de5cfd592b3340424a81733259a876", - "reference": "f8d5d08c56de5cfd592b3340424a81733259a876", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/db32c18eba00b121c145575fcbcd4d4d24e6db74", + "reference": "db32c18eba00b121c145575fcbcd4d4d24e6db74", "shasum": "" }, "require": { @@ -357,7 +357,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3-dev" + "dev-master": "1.4-dev" } }, "autoload": { @@ -380,20 +380,20 @@ "keywords": [ "tokenizer" ], - "time": "2014-08-31 06:12:13" + "time": "2015-01-17 09:51:32" }, { "name": "phpunit/phpunit", - "version": "4.4.1", + "version": "4.4.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "6a5e49a86ce5e33b8d0657abe145057fc513543a" + "reference": "e90575c2bb86290d57a262862dab1da125431576" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6a5e49a86ce5e33b8d0657abe145057fc513543a", - "reference": "6a5e49a86ce5e33b8d0657abe145057fc513543a", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e90575c2bb86290d57a262862dab1da125431576", + "reference": "e90575c2bb86290d57a262862dab1da125431576", "shasum": "" }, "require": { @@ -451,7 +451,7 @@ "testing", "xunit" ], - "time": "2014-12-28 07:57:05" + "time": "2015-01-17 11:24:41" }, { "name": "phpunit/phpunit-mock-objects", @@ -827,17 +827,17 @@ }, { "name": "symfony/yaml", - "version": "v2.6.1", + "version": "v2.6.3", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "3346fc090a3eb6b53d408db2903b241af51dcb20" + "reference": "82462a90848a52c2533aa6b598b107d68076b018" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/3346fc090a3eb6b53d408db2903b241af51dcb20", - "reference": "3346fc090a3eb6b53d408db2903b241af51dcb20", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/82462a90848a52c2533aa6b598b107d68076b018", + "reference": "82462a90848a52c2533aa6b598b107d68076b018", "shasum": "" }, "require": { @@ -870,7 +870,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-12-02 20:19:20" + "time": "2015-01-03 15:33:07" } ], "aliases": [],