]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule to prevent exceptions from being thrown in a toString method
authorJoshua Spence <josh@joshuaspence.com>
Sun, 17 May 2015 22:17:32 +0000 (08:17 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Sun, 17 May 2015 22:18:34 +0000 (08:18 +1000)
Summary: Fixes T8207. It is not possible to throw an exception from within the `__toString()` method. Add a linter rule to prevent this from happening.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Maniphest Tasks: T8207

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

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

index 6ee850a15d0c2d281d6a8e62368e07de016bd14c..d08364fdbfb49b8e2f1a54225a215776c4fb5078 100644 (file)
@@ -66,6 +66,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   const LINT_NO_PARENT_SCOPE            = 64;
   const LINT_ALIAS_FUNCTION             = 65;
   const LINT_CAST_SPACING               = 66;
+  const LINT_TOSTRING_EXCEPTION         = 67;
 
   private $blacklistedFunctions = array();
   private $naminghook;
@@ -206,6 +207,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
         => pht('Alias Functions'),
       self::LINT_CAST_SPACING
         => pht('Cast Spacing'),
+      self::LINT_TOSTRING_EXCEPTION
+        => pht('Throwing Exception in %s Method', '__toString'),
     );
   }
 
@@ -326,7 +329,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   public function getVersion() {
     // The version number should be incremented whenever a new rule is added.
-    return '28';
+    return '29';
   }
 
   protected function resolveFuture($path, Future $future) {
@@ -419,6 +422,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
       'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE,
       'lintAliasFunctions' => self::LINT_ALIAS_FUNCTION,
       'lintCastSpacing' => self::LINT_CAST_SPACING,
+      'lintThrowExceptionInToStringMethod' => self::LINT_TOSTRING_EXCEPTION,
     );
 
     foreach ($method_codes as $method => $codes) {
@@ -4124,6 +4128,34 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     }
   }
 
+  private function lintThrowExceptionInToStringMethod(XHPASTNode $root) {
+    $methods = $root->selectDescendantsOfType('n_METHOD_DECLARATION');
+
+    foreach ($methods as $method) {
+      $name = $method
+        ->getChildOfType(2, 'n_STRING')
+        ->getConcreteString();
+
+      if ($name != '__toString') {
+        continue;
+      }
+
+      $throws = $method
+        ->getChildOfType(5, 'n_STATEMENT_LIST')
+        ->selectDescendantsOfType('n_THROW');
+
+      foreach ($throws as $throw) {
+        $this->raiseLintAtNode(
+          $throw,
+          self::LINT_TOSTRING_EXCEPTION,
+          pht(
+            'It is not possible to throw an %s from within the %s method.',
+            'Exception',
+            '__toString'));
+      }
+    }
+  }
+
 
   /**
    * Retrieve all calls to some specified function(s).
diff --git a/src/lint/linter/__tests__/xhpast/tostring-exception.lint-test b/src/lint/linter/__tests__/xhpast/tostring-exception.lint-test
new file mode 100644 (file)
index 0000000..2788fbd
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+class MyClass {
+  public function __toString() {
+    if (some_function()) {
+      throw new Exception('Oops');
+    }
+
+    return __CLASS__;
+  }
+}
+
+class MyOtherClass {
+  public function __toString() {
+    return 'Success';
+  }
+}
+~~~~~~~~~~
+error:5:7