]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule to ensure that `namespace` is the first statement in a file
authorJoshua Spence <josh@freelancer.com>
Wed, 25 Nov 2015 20:32:01 +0000 (07:32 +1100)
committerJoshua Spence <josh@freelancer.com>
Wed, 25 Nov 2015 20:34:27 +0000 (07:34 +1100)
Summary: If a `namespace` is used within a PHP script, it must be the first statement.

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D14526

src/__phutil_library_map__.php
src/lint/linter/xhpast/rules/ArcanistNamespaceFirstStatementXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/class-before-namespace.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/incorrect.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/multiple-namespaces.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/no-namespace.lint-test [new file with mode: 0644]

index 9c914e1b08168c9b61c46b1d4d98f5b6d7fefa7c..6d6b2f282a4bf7549d072bbc60cd5077e44a08c8 100644 (file)
@@ -234,6 +234,8 @@ phutil_register_library_map(array(
     'ArcanistMissingLinterException' => 'lint/linter/exception/ArcanistMissingLinterException.php',
     'ArcanistModifierOrderingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistModifierOrderingXHPASTLinterRule.php',
     'ArcanistModifierOrderingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistModifierOrderingXHPASTLinterRuleTestCase.php',
+    'ArcanistNamespaceFirstStatementXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNamespaceFirstStatementXHPASTLinterRule.php',
+    'ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase.php',
     'ArcanistNamingConventionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNamingConventionsXHPASTLinterRule.php',
     'ArcanistNamingConventionsXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNamingConventionsXHPASTLinterRuleTestCase.php',
     'ArcanistNestedNamespacesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php',
@@ -626,6 +628,8 @@ phutil_register_library_map(array(
     'ArcanistMissingLinterException' => 'Exception',
     'ArcanistModifierOrderingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistModifierOrderingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+    'ArcanistNamespaceFirstStatementXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistNamingConventionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistNamingConventionsXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistNestedNamespacesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
diff --git a/src/lint/linter/xhpast/rules/ArcanistNamespaceFirstStatementXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistNamespaceFirstStatementXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..729c0de
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+final class ArcanistNamespaceFirstStatementXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 98;
+
+  public function getLintName() {
+    return pht('`%s` Statement Must Be The First Statement', 'namespace');
+  }
+
+  public function process(XHPASTNode $root) {
+    $namespaces = $root->selectDescendantsOfType('n_NAMESPACE');
+
+    if (!count($namespaces)) {
+      return;
+    }
+
+    $statements = $root->getChildOfType(0, 'n_STATEMENT_LIST');
+
+    // Ignore the first statement, which should be `n_OPEN_TAG`.
+    $second_statement = $statements->getChildByIndex(1)->getChildByIndex(0);
+
+    if ($second_statement->getTypeName() != 'n_NAMESPACE') {
+      $this->raiseLintAtNode(
+        $second_statement,
+        pht(
+          'A script which contains a `%s` statement expects the very first '.
+          'statement to be a `%s` statement. Otherwise, a PHP fatal error '.
+          'will occur. %s',
+          'namespace',
+          'namespace', $second_statement->getTypeName()));
+    }
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..514c0ba
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistNamespaceFirstStatementXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(
+      dirname(__FILE__).'/namespace-first-statement/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/class-before-namespace.lint-test b/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/class-before-namespace.lint-test
new file mode 100644 (file)
index 0000000..7af115c
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+class X {}
+namespace X;
+~~~~~~~~~~
+error:2:1
diff --git a/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/incorrect.lint-test b/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/incorrect.lint-test
new file mode 100644 (file)
index 0000000..f2fd0e3
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * This is actually perfectly valid, for some unknown reason.
+ *
+ * TODO: Maybe fix the handling of this edge case.
+ */
+
+1;
+
+namespace X {
+  class C { }
+  echo 2;
+}
+~~~~~~~~~~
+error:9:1
diff --git a/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/multiple-namespaces.lint-test b/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/multiple-namespaces.lint-test
new file mode 100644 (file)
index 0000000..cf5fec7
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+namespace X;
+class X {}
+
+namespace Y;
+class Y {}
+~~~~~~~~~~
diff --git a/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/no-namespace.lint-test b/src/lint/linter/xhpast/rules/__tests__/namespace-first-statement/no-namespace.lint-test
new file mode 100644 (file)
index 0000000..161f738
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+class X {}
+~~~~~~~~~~