From: Joshua Spence Date: Sun, 17 May 2015 22:17:32 +0000 (+1000) Subject: Add a linter rule to prevent exceptions from being thrown in a toString method X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=1e3073a4f88e92159f797e34c716fd09ad17d7fa;p=arcanist.git Add a linter rule to prevent exceptions from being thrown in a toString method 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 --- diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 6ee850a1..d08364fd 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -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 index 00000000..2788fbdc --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/tostring-exception.lint-test @@ -0,0 +1,18 @@ +