]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve the "unexpected return value" linter rule
authorJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 22:49:43 +0000 (09:49 +1100)
committerJoshua Spence <josh@freelancer.com>
Mon, 23 Nov 2015 23:01:35 +0000 (10:01 +1100)
Summary: Return values within constructors are acceptable if they are within a closure or anonymous function.

Test Plan: Added a test case.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php
src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/closure.lint-test [new file with mode: 0644]

index b73ea0a5dc9b8a9801931e5ee3a519bb2a81da46..10ffda44ad79b4a5e48856aa09661cedd27ed8a3 100644 (file)
@@ -33,6 +33,10 @@ final class ArcanistUnexpectedReturnValueXHPASTLinterRule
               continue;
             }
 
+            if ($this->isInAnonymousFunction($return)) {
+              continue;
+            }
+
             $this->raiseLintAtNode(
               $return,
               pht(
@@ -45,4 +49,15 @@ final class ArcanistUnexpectedReturnValueXHPASTLinterRule
     }
   }
 
+  private function isInAnonymousFunction(XHPASTNode $node) {
+    while ($node) {
+      if ($node->getTypeName() == 'n_FUNCTION_DECLARATION' &&
+          $node->getChildByIndex(2)->getTypeName() == 'n_EMPTY') {
+        return true;
+      }
+
+      $node = $node->getParentNode();
+    }
+  }
+
 }
diff --git a/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/closure.lint-test b/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/closure.lint-test
new file mode 100644 (file)
index 0000000..65536bf
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+class SomeClass {
+  private $closure;
+
+  public function __construct() {
+    $this->closure = function() {
+        return null;
+    };
+  }
+}
+~~~~~~~~~~