]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Write a linter rule for unexpected `return` values
authorJoshua Spence <josh@freelancer.com>
Thu, 19 Nov 2015 08:34:32 +0000 (19:34 +1100)
committerJoshua Spence <josh@freelancer.com>
Thu, 19 Nov 2015 08:34:32 +0000 (19:34 +1100)
Summary:
Although it is technically possible to return a value from a PHP constructor, it is rather odd and should be avoided. See some discussion on [[http://stackoverflow.com/q/11904255 | StackOverflow]]. Consider the following example:

```lang=php
class SomeClass {
  public function __construct() {
    return 'quack';
  }
}
```

This doesn't work:

```lang=php, counterexample
echo new SomeClas();
```

This, strangely, does work:

```lang=php
echo id(new SomeClass())->__construct();
```

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

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

src/__phutil_library_map__.php
src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/construct.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/destruct.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/empty-return.lint-test [new file with mode: 0644]

index a9d0a26ef59ffbc3f2f0b2a3226bc1bbcf5cf091..7f3ec25de89e548bfe13872ae42dc096b2464886 100644 (file)
@@ -333,6 +333,8 @@ phutil_register_library_map(array(
     'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRuleTestCase.php',
     'ArcanistUndeclaredVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUndeclaredVariableXHPASTLinterRule.php',
     'ArcanistUndeclaredVariableXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUndeclaredVariableXHPASTLinterRuleTestCase.php',
+    'ArcanistUnexpectedReturnValueXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php',
+    'ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase.php',
     'ArcanistUnitConsoleRenderer' => 'unit/renderer/ArcanistUnitConsoleRenderer.php',
     'ArcanistUnitRenderer' => 'unit/renderer/ArcanistUnitRenderer.php',
     'ArcanistUnitTestEngine' => 'unit/engine/ArcanistUnitTestEngine.php',
@@ -707,6 +709,8 @@ phutil_register_library_map(array(
     'ArcanistUnaryPrefixExpressionSpacingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistUndeclaredVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistUndeclaredVariableXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+    'ArcanistUnexpectedReturnValueXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
     'ArcanistUnitConsoleRenderer' => 'ArcanistUnitRenderer',
     'ArcanistUnitRenderer' => 'Phobject',
     'ArcanistUnitTestEngine' => 'Phobject',
diff --git a/src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUnexpectedReturnValueXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..b73ea0a
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+final class ArcanistUnexpectedReturnValueXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 92;
+
+  public function getLintName() {
+    return pht('Unexpected `%s` Value', 'return');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_WARNING;
+  }
+
+  public function process(XHPASTNode $root) {
+    $methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
+
+    foreach ($methods as $method) {
+      $method_name = $method
+        ->getChildOfType(2, 'n_STRING')
+        ->getConcreteString();
+
+      switch (strtolower($method_name)) {
+        case '__construct':
+        case '__destruct':
+          $returns = $method->selectDescendantsOfType('n_RETURN');
+
+          foreach ($returns as $return) {
+            $return_value = $return->getChildByIndex(0);
+
+            if ($return_value->getTypeName() == 'n_EMPTY') {
+              continue;
+            }
+
+            $this->raiseLintAtNode(
+              $return,
+              pht(
+                'Unexpected `%s` value in `%s` method.',
+                'return',
+                $method_name));
+          }
+          break;
+      }
+    }
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..462857c
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistUnexpectedReturnValueXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(
+      dirname(__FILE__).'/unexpected-return-value/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/construct.lint-test b/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/construct.lint-test
new file mode 100644 (file)
index 0000000..894c3f6
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+class SomeClass {
+  public function __construct() {
+    return null;
+  }
+}
+~~~~~~~~~~
+warning:4:5
diff --git a/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/destruct.lint-test b/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/destruct.lint-test
new file mode 100644 (file)
index 0000000..5b9d7bf
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+class SomeClass {
+  public function __destruct() {
+    return null;
+  }
+}
+~~~~~~~~~~
+warning:4:5
diff --git a/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/empty-return.lint-test b/src/lint/linter/xhpast/rules/__tests__/unexpected-return-value/empty-return.lint-test
new file mode 100644 (file)
index 0000000..c18a5a5
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+class SomeClass {
+  public function __construct() {
+    return;
+  }
+}
+~~~~~~~~~~