Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15317659
D12854.id30924.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D12854.id30924.diff
View Options
diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php
--- a/src/lint/linter/ArcanistXHPASTLinter.php
+++ b/src/lint/linter/ArcanistXHPASTLinter.php
@@ -64,6 +64,7 @@
const LINT_CLASS_NAME_LITERAL = 62;
const LINT_USELESS_OVERRIDING_METHOD = 63;
const LINT_NO_PARENT_SCOPE = 64;
+ const LINT_TOSTRING_EXCEPTION = 65;
private $blacklistedFunctions = array();
private $naminghook;
@@ -200,6 +201,8 @@
=> pht('Useless Overriding Method'),
self::LINT_NO_PARENT_SCOPE
=> pht('No Parent Scope'),
+ self::LINT_TOSTRING_EXCEPTION
+ => pht('Throwing Exception in %s Method', '__toString'),
);
}
@@ -318,7 +321,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '26';
+ return '27';
}
protected function resolveFuture($path, Future $future) {
@@ -409,6 +412,7 @@
'lintClassNameLiteral' => self::LINT_CLASS_NAME_LITERAL,
'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD,
'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE,
+ 'lintThrowExceptionInToStringMethod' => self::LINT_TOSTRING_EXCEPTION,
);
foreach ($method_codes as $method => $codes) {
@@ -3887,6 +3891,33 @@
}
}
+ 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
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/tostring-exception.lint-test
@@ -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
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Mar 7, 7:44 PM (2 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7346165
Default Alt Text
D12854.id30924.diff (2 KB)
Attached To
Mode
D12854: Add a linter rule to prevent exceptions from being thrown in a toString method
Attached
Detach File
Event Timeline
Log In to Comment