From bf88f4616cdfc8a391012f768e5e226aa0fb9a52 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Fri, 14 Aug 2015 07:37:48 +1000 Subject: [PATCH] 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 --- src/__phutil_library_map__.php | 2 ++ .../xhpast/global-variables.lint-test | 8 ++++++ .../xhpast/naming-conventions.lint-test | 1 + .../xhpast/undeclared-variables.lint-test | 2 ++ ...ArcanistGlobalVariableXHPASTLinterRule.php | 28 +++++++++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 src/lint/linter/__tests__/xhpast/global-variables.lint-test create mode 100644 src/lint/linter/xhpast/rules/ArcanistGlobalVariableXHPASTLinterRule.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index c4b3d8b1..70f87173 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -110,6 +110,7 @@ phutil_register_library_map(array( 'ArcanistGeneratedLinterTestCase' => 'lint/linter/__tests__/ArcanistGeneratedLinterTestCase.php', 'ArcanistGetConfigWorkflow' => 'workflow/ArcanistGetConfigWorkflow.php', 'ArcanistGitAPI' => 'repository/api/ArcanistGitAPI.php', + 'ArcanistGlobalVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistGlobalVariableXHPASTLinterRule.php', 'ArcanistGoLintLinter' => 'lint/linter/ArcanistGoLintLinter.php', 'ArcanistGoLintLinterTestCase' => 'lint/linter/__tests__/ArcanistGoLintLinterTestCase.php', 'ArcanistGoTestResultParser' => 'unit/parser/ArcanistGoTestResultParser.php', @@ -390,6 +391,7 @@ phutil_register_library_map(array( 'ArcanistGeneratedLinterTestCase' => 'ArcanistLinterTestCase', 'ArcanistGetConfigWorkflow' => 'ArcanistWorkflow', 'ArcanistGitAPI' => 'ArcanistRepositoryAPI', + 'ArcanistGlobalVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistGoLintLinter' => 'ArcanistExternalLinter', 'ArcanistGoLintLinterTestCase' => 'ArcanistExternalLinterTestCase', 'ArcanistGoTestResultParser' => 'ArcanistTestResultParser', diff --git a/src/lint/linter/__tests__/xhpast/global-variables.lint-test b/src/lint/linter/__tests__/xhpast/global-variables.lint-test new file mode 100644 index 00000000..863cf5fd --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/global-variables.lint-test @@ -0,0 +1,8 @@ +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.')); + } + } + +}