1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-26 08:42:40 +01:00

Add a linter rule for global variables

Summary: Ref T7409. Global variables are gross and should be avoided like the plague.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Maniphest Tasks: T7409

Differential Revision: https://secure.phabricator.com/D13882
This commit is contained in:
Joshua Spence 2015-08-14 07:37:48 +10:00
parent f4c322cb72
commit bf88f4616c
5 changed files with 41 additions and 0 deletions

View file

@ -110,6 +110,7 @@ phutil_register_library_map(array(
'ArcanistGeneratedLinterTestCase' => 'lint/linter/__tests__/ArcanistGeneratedLinterTestCase.php', 'ArcanistGeneratedLinterTestCase' => 'lint/linter/__tests__/ArcanistGeneratedLinterTestCase.php',
'ArcanistGetConfigWorkflow' => 'workflow/ArcanistGetConfigWorkflow.php', 'ArcanistGetConfigWorkflow' => 'workflow/ArcanistGetConfigWorkflow.php',
'ArcanistGitAPI' => 'repository/api/ArcanistGitAPI.php', 'ArcanistGitAPI' => 'repository/api/ArcanistGitAPI.php',
'ArcanistGlobalVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistGlobalVariableXHPASTLinterRule.php',
'ArcanistGoLintLinter' => 'lint/linter/ArcanistGoLintLinter.php', 'ArcanistGoLintLinter' => 'lint/linter/ArcanistGoLintLinter.php',
'ArcanistGoLintLinterTestCase' => 'lint/linter/__tests__/ArcanistGoLintLinterTestCase.php', 'ArcanistGoLintLinterTestCase' => 'lint/linter/__tests__/ArcanistGoLintLinterTestCase.php',
'ArcanistGoTestResultParser' => 'unit/parser/ArcanistGoTestResultParser.php', 'ArcanistGoTestResultParser' => 'unit/parser/ArcanistGoTestResultParser.php',
@ -390,6 +391,7 @@ phutil_register_library_map(array(
'ArcanistGeneratedLinterTestCase' => 'ArcanistLinterTestCase', 'ArcanistGeneratedLinterTestCase' => 'ArcanistLinterTestCase',
'ArcanistGetConfigWorkflow' => 'ArcanistWorkflow', 'ArcanistGetConfigWorkflow' => 'ArcanistWorkflow',
'ArcanistGitAPI' => 'ArcanistRepositoryAPI', 'ArcanistGitAPI' => 'ArcanistRepositoryAPI',
'ArcanistGlobalVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistGoLintLinter' => 'ArcanistExternalLinter', 'ArcanistGoLintLinter' => 'ArcanistExternalLinter',
'ArcanistGoLintLinterTestCase' => 'ArcanistExternalLinterTestCase', 'ArcanistGoLintLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistGoTestResultParser' => 'ArcanistTestResultParser', 'ArcanistGoTestResultParser' => 'ArcanistTestResultParser',

View file

@ -0,0 +1,8 @@
<?php
global $BASE_URL;
function foo() {
global $x, $y;
}
~~~~~~~~~~
warning:2:1
warning:4:3

View file

@ -72,4 +72,5 @@ warning:29:3
warning:30:3 warning:30:3
warning:31:3 warning:31:3
warning:33:3 warning:33:3
warning:37:3
warning:55:3 warning:55:3

View file

@ -180,6 +180,7 @@ function some_func($x, $y) {
}; };
} }
~~~~~~~~~~ ~~~~~~~~~~
warning:9:3
error:28:3 error:28:3
error:30:3 error:30:3
error:36:3 error:36:3
@ -188,6 +189,7 @@ error:43:7
error:44:5 error:44:5
error:45:5 error:45:5
error:46:10 error:46:10
warning:51:3
error:51:10 worst ever error:51:10 worst ever
warning:61:3 warning:61:3
error:87:3 This stuff is basically testing the lexer/parser for function decls. error:87:3 This stuff is basically testing the lexer/parser for function decls.

View file

@ -0,0 +1,28 @@
<?php
final class ArcanistGlobalVariableXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 79;
public function getLintName() {
return pht('Global Variables');
}
public function getLintSeverity() {
return ArcanistLintSeverity::SEVERITY_WARNING;
}
public function process(XHPASTNode $root) {
$nodes = $root->selectDescendantsOfType('n_GLOBAL_DECLARATION_LIST');
foreach ($nodes as $node) {
$this->raiseLintAtNode(
$node,
pht(
'Limit the use of global variables. Global variables are '.
'generally a bad idea and should be avoided when possible.'));
}
}
}