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

Implemented fixer for constructor calls without parenthesis.

parent ad743d1a
Branches
Tags
No related merge requests found
......@@ -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
?>
<?php
$x = array(new Foo, array());
$y = new Foo;
$z = new $class;
$bar = new $foo[$x + 1];
<?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
......@@ -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,
......
......@@ -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": [],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment