]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Raise a lint error when code uses a PHP 5.3 feature in Phabricator
authorepriestley <git@epriestley.com>
Fri, 9 Mar 2012 21:51:02 +0000 (13:51 -0800)
committerepriestley <git@epriestley.com>
Fri, 9 Mar 2012 21:51:02 +0000 (13:51 -0800)
Summary:
  - Add a lint check for PHP 5.3 features (namespaces, anonymous functions).
  - Add unit tests.
  - Disable it by default.
  - Enable it for us.

Test Plan: Ran unit tests.

Reviewers: btrahan, edward

Reviewed By: edward

CC: aran, epriestley
Maniphest Tasks: T962

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

src/lint/engine/phutil/PhutilLintEngine.php
src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/naming-conventions.lint-test
src/lint/linter/xhpast/__tests__/data/php53.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test

index 42d811849d1d528b0a682e8d1d647323ec9ed5fa..881ec99fc6d4dc232f8c5c899f6331ace58b213a 100644 (file)
@@ -94,6 +94,8 @@ class PhutilLintEngine extends ArcanistLintEngine {
       array(
         ArcanistXHPASTLinter::LINT_RAGGED_CLASSTREE_EDGE
           => ArcanistLintSeverity::SEVERITY_WARNING,
+        ArcanistXHPASTLinter::LINT_PHP_53_FEATURES
+          => ArcanistLintSeverity::SEVERITY_ERROR,
       ));
     $license_linter = new ArcanistApacheLicenseLinter();
     $linters[] = $xhpast_linter;
index dd5861bfccc82a996fde6a3ec6195a9b6ddb16d2..75191f5e286040dc8f01966b19c0df0e97f6dd6f 100644 (file)
@@ -54,6 +54,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
   const LINT_ARRAY_INDEX_SPACING       = 28;
   const LINT_RAGGED_CLASSTREE_EDGE     = 29;
   const LINT_IMPLICIT_FALLTHROUGH      = 30;
+  const LINT_PHP_53_FEATURES           = 31;
 
 
   public function getLintNameMap() {
@@ -87,6 +88,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_ARRAY_INDEX_SPACING       => 'Spacing Before Array Index',
       self::LINT_RAGGED_CLASSTREE_EDGE     => 'Class Not abstract Or final',
       self::LINT_IMPLICIT_FALLTHROUGH      => 'Implicit Fallthrough',
+      self::LINT_PHP_53_FEATURES           => 'Use Of PHP 5.3 Features',
     );
   }
 
@@ -120,6 +122,11 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
       // which isn't necessary in the general case.
       self::LINT_RAGGED_CLASSTREE_EDGE
         => ArcanistLintSeverity::SEVERITY_DISABLED,
+
+      // This is disabled by default because projects don't necessarily target
+      // a specific minimum version.
+      self::LINT_PHP_53_FEATURES
+        => ArcanistLintSeverity::SEVERITY_DISABLED,
     );
   }
 
@@ -190,6 +197,56 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintBraceFormatting($root);
     $this->lintRaggedClasstreeEdges($root);
     $this->lintImplicitFallthrough($root);
+    $this->lintPHP53Features($root);
+  }
+
+  public function lintPHP53Features($root) {
+
+    $functions = $root->selectTokensOfType('T_FUNCTION');
+    foreach ($functions as $function) {
+
+      $next = $function->getNextToken();
+      while ($next) {
+        if ($next->isSemantic()) {
+          break;
+        }
+        $next = $next->getNextToken();
+      }
+
+      if ($next) {
+        if ($next->getTypeName() == '(') {
+          $this->raiseLintAtToken(
+            $function,
+            self::LINT_PHP_53_FEATURES,
+            'This codebase targets PHP 5.2, but anonymous functions were '.
+            'not introduced until PHP 5.3.');
+        }
+      }
+    }
+
+    $namespaces = $root->selectTokensOfType('T_NAMESPACE');
+    foreach ($namespaces as $namespace) {
+      $this->raiseLintAtToken(
+        $namespace,
+        self::LINT_PHP_53_FEATURES,
+        'This codebase targets PHP 5.2, but namespaces were not introduced '.
+        'until PHP 5.3.');
+    }
+
+    // NOTE: This is only "use x;", in anonymous functions the node type is
+    // n_LEXICAL_VARIABLE_LIST even though both tokens are T_USE.
+
+    // TODO: We parse n_USE in a slightly crazy way right now; that would be
+    // a better selector once it's fixed.
+
+    $uses = $root->selectDescendantsOfType('n_USE_LIST');
+    foreach ($uses as $use) {
+      $this->raiseLintAtNode(
+        $use,
+        self::LINT_PHP_53_FEATURES,
+        'This codebase targets PHP 5.2, but namespaces were not introduced '.
+        'until PHP 5.3.');
+    }
   }
 
   private function lintImplicitFallthrough($root) {
index 78e16a51d9a1ba2dd8b7003f80c360bb7c91d89c..65c3e5ab42f292b2e18dbf5620de488f9c0bb79e 100644 (file)
@@ -61,6 +61,7 @@ warning:5:25
 warning:8:11
 warning:12:10
 warning:12:13
+disabled:24:1
 warning:26:13
 warning:30:3
 warning:31:3
diff --git a/src/lint/linter/xhpast/__tests__/data/php53.lint-test b/src/lint/linter/xhpast/__tests__/data/php53.lint-test
new file mode 100644 (file)
index 0000000..8622b9a
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace a;
+use b, c;
+f(function() {});
+g(function() use ($c) {});
+h(function /* ! */ () use ($c) {});
+~~~~~~~~~~
+disabled:3:1
+disabled:4:5
+disabled:5:3
+disabled:6:3
+disabled:7:3
index 18f432675f12cfd30d9d7a1f5ebb3daa323e3322..7aa359c563d3e583e48831ae983f3099133ed678 100644 (file)
@@ -161,6 +161,7 @@ function arrow($o, $x) {
 }
 
 ~~~~~~~~~~
+disabled:3:1
 error:30:3
 error:32:3
 error:38:3