Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15424433
D12854.id31001.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.id31001.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
@@ -66,6 +66,7 @@
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 @@
=> 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 @@
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 @@
'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 @@
}
}
+ 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
Sun, Mar 23, 10:24 PM (2 d, 12 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7223939
Default Alt Text
D12854.id31001.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