]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for declaring nested functions
authorJoshua Spence <josh@joshuaspence.com>
Mon, 13 Apr 2015 20:29:44 +0000 (06:29 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 13 Apr 2015 20:29:44 +0000 (06:29 +1000)
Summary: Ref T7409. Adds `ArcanistXHPASTLinter::LINT_INNER_FUNCTION` to advise against declaring PHP functions within an outer function. Based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Squiz/Sniffs/PHP/InnerFunctionsSniff.php | Squiz_Sniffs_PHP_InnerFunctionsSniff]].

Test Plan: Added unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T7409

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

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/inner-function.lint-test [new file with mode: 0644]

index 2ff9805fb95c2045ee4e1a11d299165c8feecfd7..405530fff02545078c6c545c9a2fd31b7ba00b98 100644 (file)
@@ -58,6 +58,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   const LINT_UNNECESSARY_SEMICOLON      = 56;
   const LINT_SELF_MEMBER_REFERENCE      = 57;
   const LINT_LOGICAL_OPERATORS          = 58;
+  const LINT_INNER_FUNCTION             = 59;
 
   private $blacklistedFunctions = array();
   private $naminghook;
@@ -129,6 +130,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_UNNECESSARY_SEMICOLON      => 'Unnecessary Semicolon',
       self::LINT_SELF_MEMBER_REFERENCE      => 'Self Member Reference',
       self::LINT_LOGICAL_OPERATORS          => 'Logical Operators',
+      self::LINT_INNER_FUNCTION             => 'Inner Functions',
     );
   }
 
@@ -175,6 +177,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       self::LINT_UNNECESSARY_SEMICOLON      => $advice,
       self::LINT_SELF_MEMBER_REFERENCE      => $advice,
       self::LINT_LOGICAL_OPERATORS          => $advice,
+      self::LINT_INNER_FUNCTION             => $warning,
     );
   }
 
@@ -242,7 +245,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   public function getVersion() {
     // The version number should be incremented whenever a new rule is added.
-    return '21';
+    return '22';
   }
 
   protected function resolveFuture($path, Future $future) {
@@ -325,6 +328,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintConstantDefinitions' => self::LINT_NAMING_CONVENTIONS,
       'lintSelfMemberReference' => self::LINT_SELF_MEMBER_REFERENCE,
       'lintLogicalOperators' => self::LINT_LOGICAL_OPERATORS,
+      'lintInnerFunctions' => self::LINT_INNER_FUNCTION,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -3486,6 +3490,27 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  private function lintInnerFunctions(XHPASTNode $root) {
+    $function_decls = $root->selectDescendantsOfType('n_FUNCTION_DECLARATION');
+
+    foreach ($function_decls as $function_declaration) {
+      $inner_functions = $function_declaration
+        ->selectDescendantsOfType('n_FUNCTION_DECLARATION');
+
+      foreach ($inner_functions as $inner_function) {
+        if ($inner_function->getChildByIndex(2)->getTypeName() == 'n_EMPTY') {
+          // Anonymous closure.
+          continue;
+        }
+
+        $this->raiseLintAtNode(
+          $inner_function,
+          self::LINT_INNER_FUNCTION,
+          pht('Avoid the use of inner functions.'));
+      }
+    }
+  }
+
   /**
    * Retrieve all calls to some specified function(s).
    *
diff --git a/src/lint/linter/__tests__/xhpast/inner-function.lint-test b/src/lint/linter/__tests__/xhpast/inner-function.lint-test
new file mode 100644 (file)
index 0000000..38d0ca1
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+function outer() {
+  if (!function_exists('inner')) {
+    function inner() {}
+  }
+}
+
+// Closures are allowed.
+function my_func($foo) {
+  function() {};
+}
+~~~~~~~~~~
+warning:5:5