]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for uppercase constant definitions
authorJoshua Spence <josh@joshuaspence.com>
Sun, 12 Apr 2015 21:21:39 +0000 (07:21 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Sun, 12 Apr 2015 21:23:02 +0000 (07:23 +1000)
Summary: Ref T7409. Adds a linter rule to ensure that constant are defined as being uppercase. Based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php | Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff]].

Test Plan: Added test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T7409

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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/constant-case.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/xhpast/dynamic-define.lint-test

index 7e7ac94a23d6f189b3b20233a4a6e42290b46a04..2f5fb63d39e48e4dcc4fc9d505dfb42d1fe16601 100644 (file)
@@ -236,7 +236,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   public function getVersion() {
     // The version number should be incremented whenever a new rule is added.
-    return '18';
+    return '19';
   }
 
   protected function resolveFuture($path, Future $future) {
@@ -316,6 +316,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintFormattedString' => self::LINT_FORMATTED_STRING,
       'lintUnnecessaryFinalModifier' => self::LINT_UNNECESSARY_FINAL_MODIFIER,
       'lintUnnecessarySemicolons' => self::LINT_UNNECESSARY_SEMICOLON,
+      'lintConstantDefinitions' => self::LINT_NAMING_CONVENTIONS,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -3263,6 +3264,83 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  private function lintConstantDefinitions(XHPASTNode $root) {
+    $defines = $this
+      ->getFunctionCalls($root, array('define'))
+      ->add($root->selectDescendantsOfTypes(array(
+        'n_CLASS_CONSTANT_DECLARATION',
+        'n_CONSTANT_DECLARATION',
+      )));
+
+    foreach ($defines as $define) {
+      switch ($define->getTypeName()) {
+        case 'n_CLASS_CONSTANT_DECLARATION':
+        case 'n_CONSTANT_DECLARATION':
+          $constant = $define->getChildByIndex(0);
+
+          if ($constant->getTypeName() !== 'n_STRING') {
+            $constant = null;
+          }
+
+          break;
+
+        case 'n_FUNCTION_CALL':
+          $constant = $define
+            ->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
+            ->getChildByIndex(0);
+
+          if ($constant->getTypeName() !== 'n_STRING_SCALAR') {
+            $constant = null;
+          }
+
+          break;
+
+        default:
+          $constant = null;
+          break;
+      }
+
+      if (!$constant) {
+        continue;
+      }
+      $constant_name = $constant->getConcreteString();
+
+      if ($constant_name !== strtoupper($constant_name)) {
+        $this->raiseLintAtNode(
+          $constant,
+          self::LINT_NAMING_CONVENTIONS,
+          pht('Constants should be uppercase.'));
+      }
+    }
+  }
+
+
+  /**
+   * Retrieve all calls to some specified function(s).
+   *
+   * Returns all descendant nodes which represent a function call to one of the
+   * specified functions.
+   *
+   * @param  XHPASTNode    Root node.
+   * @param  list<string>  Function names.
+   * @return AASTNodeList
+   */
+  protected function getFunctionCalls(XHPASTNode $root, array $function_names) {
+    $calls = $root->selectDescendantsOfType('n_FUNCTION_CALL');
+    $nodes = array();
+
+    foreach ($calls as $call) {
+      $node = $call->getChildByIndex(0);
+      $name = strtolower($node->getConcreteString());
+
+      if (in_array($name, $function_names)) {
+        $nodes[] = $call;
+      }
+    }
+
+    return AASTNodeList::newFromTreeAndNodes($root->getTree(), $nodes);
+  }
+
   public function getSuperGlobalNames() {
     return array(
       '$GLOBALS',
diff --git a/src/lint/linter/__tests__/xhpast/constant-case.lint-test b/src/lint/linter/__tests__/xhpast/constant-case.lint-test
new file mode 100644 (file)
index 0000000..5933b66
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+define('foo', 'bar');
+const bar = 'baz';
+
+class Foo {
+  const bar = 'baz';
+}
+~~~~~~~~~~
+warning:2:8
+warning:3:7
+error:5:7
+warning:6:9
index 897c3ece1738546a066fcde916598b7c378f52a7..a7a995479c7ccaf90412240c7d4d930034ecd750 100644 (file)
@@ -1,7 +1,7 @@
 <?php
-define('pony', 'cute');
+define('PONY', 'cute');
 define($pony, 'cute');
-define('pony', $cute);
+define('PONY', $cute);
 define($pony, $cute);
 ~~~~~~~~~~
 error:3:8 dynamic define