1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 14:52:40 +01:00

Add a LINT_CONSTRUCTOR_PARENTHESES rule to ArcanistXHPASTLinter

Summary: Adds an `ArcanistXHPASTLinter::LINT_CONSTRUCTOR_PARENTHESES` rule which ensures that (possibly empty) parentheses are used when calling a constructor.

Test Plan: Added test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D10597
This commit is contained in:
Joshua Spence 2014-10-07 23:59:03 +11:00
parent b1112e73c4
commit e54725ec89
2 changed files with 34 additions and 1 deletions

View file

@ -48,6 +48,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
const LINT_LANGUAGE_CONSTRUCT_PAREN = 46;
const LINT_EMPTY_STATEMENT = 47;
const LINT_ARRAY_SEPARATOR = 48;
const LINT_CONSTRUCTOR_PARENTHESES = 49;
private $naminghook;
private $switchhook;
@ -107,6 +108,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
self::LINT_LANGUAGE_CONSTRUCT_PAREN => 'Language Construct Parentheses',
self::LINT_EMPTY_STATEMENT => 'Empty Block Statement',
self::LINT_ARRAY_SEPARATOR => 'Array Separator',
self::LINT_CONSTRUCTOR_PARENTHESES => 'Constructor Parentheses',
);
}
@ -147,6 +149,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
self::LINT_LANGUAGE_CONSTRUCT_PAREN => $warning,
self::LINT_EMPTY_STATEMENT => $advice,
self::LINT_ARRAY_SEPARATOR => $advice,
self::LINT_CONSTRUCTOR_PARENTHESES => $advice,
);
}
@ -196,7 +199,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
return '9';
return '10';
}
protected function resolveFuture($path, Future $future) {
@ -267,6 +270,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN,
'lintEmptyBlockStatements' => self::LINT_EMPTY_STATEMENT,
'lintArraySeparator' => self::LINT_ARRAY_SEPARATOR,
'lintConstructorParentheses' => self::LINT_CONSTRUCTOR_PARENTHESES,
);
foreach ($method_codes as $method => $codes) {
@ -2792,6 +2796,23 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
}
}
private function lintConstructorParentheses(XHPASTNode $root) {
$nodes = $root->selectDescendantsOfType('n_NEW');
foreach ($nodes as $node) {
$class = $node->getChildByIndex(0);
$params = $node->getChildByIndex(1);
if ($params->getTypeName() == 'n_EMPTY') {
$this->raiseLintAtNode(
$class,
self::LINT_CONSTRUCTOR_PARENTHESES,
pht('Use parentheses when invoking a constructor.'),
$class->getConcreteString().'()');
}
}
}
public function getSuperGlobalNames() {
return array(
'$GLOBALS',

View file

@ -0,0 +1,12 @@
<?php
new Foo;
new Bar();
new Foo\Bar;
~~~~~~~~~~
advice:2:5
advice:4:5
~~~~~~~~~~
<?php
new Foo();
new Bar();
new Foo\Bar();