From 149e7895fbd5e35f804e6f0b77d7788a988bc2f9 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Thu, 19 Nov 2015 19:31:45 +1100 Subject: [PATCH] Add a linter rule for nested namespaces Summary: Namespace declarations cannot be nested, see https://3v4l.org/kRUNj. Test Plan: Added unit tests Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: Korvin Differential Revision: https://secure.phabricator.com/D14513 --- src/__phutil_library_map__.php | 4 +++ ...canistNestedNamespacesXHPASTLinterRule.php | 29 +++++++++++++++++++ ...stedNamespacesXHPASTLinterRuleTestCase.php | 10 +++++++ .../nested-namespaces/nested.lint-test | 9 ++++++ 4 files changed, 52 insertions(+) create mode 100644 src/lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php create mode 100644 src/lint/linter/xhpast/rules/__tests__/ArcanistNestedNamespacesXHPASTLinterRuleTestCase.php create mode 100644 src/lint/linter/xhpast/rules/__tests__/nested-namespaces/nested.lint-test diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 283f3cf7..a9d0a26e 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -226,6 +226,8 @@ phutil_register_library_map(array( 'ArcanistModifierOrderingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistModifierOrderingXHPASTLinterRuleTestCase.php', 'ArcanistNamingConventionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNamingConventionsXHPASTLinterRule.php', 'ArcanistNamingConventionsXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNamingConventionsXHPASTLinterRuleTestCase.php', + 'ArcanistNestedNamespacesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php', + 'ArcanistNestedNamespacesXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNestedNamespacesXHPASTLinterRuleTestCase.php', 'ArcanistNewlineAfterOpenTagXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNewlineAfterOpenTagXHPASTLinterRule.php', 'ArcanistNewlineAfterOpenTagXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNewlineAfterOpenTagXHPASTLinterRuleTestCase.php', 'ArcanistNoEffectException' => 'exception/usage/ArcanistNoEffectException.php', @@ -598,6 +600,8 @@ phutil_register_library_map(array( 'ArcanistModifierOrderingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistNamingConventionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistNamingConventionsXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', + 'ArcanistNestedNamespacesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', + 'ArcanistNestedNamespacesXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistNewlineAfterOpenTagXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistNewlineAfterOpenTagXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistNoEffectException' => 'ArcanistUsageException', diff --git a/src/lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php new file mode 100644 index 00000000..ec42064b --- /dev/null +++ b/src/lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php @@ -0,0 +1,29 @@ +selectDescendantsOfType('n_NAMESPACE'); + + foreach ($namespaces as $namespace) { + $nested_namespaces = $namespace->selectDescendantsOfType('n_NAMESPACE'); + + foreach ($nested_namespaces as $nested_namespace) { + $this->raiseLintAtNode( + $nested_namespace, + pht( + '`%s` declarations cannot be nested. '. + 'This construct will cause a PHP fatal error.', + 'namespace')); + } + } + } + +} diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistNestedNamespacesXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistNestedNamespacesXHPASTLinterRuleTestCase.php new file mode 100644 index 00000000..ba48ce3b --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistNestedNamespacesXHPASTLinterRuleTestCase.php @@ -0,0 +1,10 @@ +executeTestsInDirectory(dirname(__FILE__).'/nested-namespaces/'); + } + +} diff --git a/src/lint/linter/xhpast/rules/__tests__/nested-namespaces/nested.lint-test b/src/lint/linter/xhpast/rules/__tests__/nested-namespaces/nested.lint-test new file mode 100644 index 00000000..ef3d5757 --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/nested-namespaces/nested.lint-test @@ -0,0 +1,9 @@ +