Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14095758
D14514.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D14514.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -319,6 +319,8 @@
'ArcanistTestXHPASTLintSwitchHook' => 'lint/linter/__tests__/ArcanistTestXHPASTLintSwitchHook.php',
'ArcanistTextLinter' => 'lint/linter/ArcanistTextLinter.php',
'ArcanistTextLinterTestCase' => 'lint/linter/__tests__/ArcanistTextLinterTestCase.php',
+ 'ArcanistThisReassignmentXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistThisReassignmentXHPASTLinterRule.php',
+ 'ArcanistThisReassignmentXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistThisReassignmentXHPASTLinterRuleTestCase.php',
'ArcanistTimeWorkflow' => 'workflow/ArcanistTimeWorkflow.php',
'ArcanistToStringExceptionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistToStringExceptionXHPASTLinterRule.php',
'ArcanistToStringExceptionXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistToStringExceptionXHPASTLinterRuleTestCase.php',
@@ -695,6 +697,8 @@
'ArcanistTestXHPASTLintSwitchHook' => 'ArcanistXHPASTLintSwitchHook',
'ArcanistTextLinter' => 'ArcanistLinter',
'ArcanistTextLinterTestCase' => 'ArcanistLinterTestCase',
+ 'ArcanistThisReassignmentXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistThisReassignmentXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistTimeWorkflow' => 'ArcanistPhrequentWorkflow',
'ArcanistToStringExceptionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistToStringExceptionXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
diff --git a/src/lint/linter/xhpast/rules/ArcanistThisReassignmentXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistThisReassignmentXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistThisReassignmentXHPASTLinterRule.php
@@ -0,0 +1,39 @@
+<?php
+
+final class ArcanistThisReassignmentXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 91;
+
+ public function getLintName() {
+ return pht('`%s` Reassignment', '$this');
+ }
+
+ public function process(XHPASTNode $root) {
+ $binary_expressions = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
+
+ foreach ($binary_expressions as $binary_expression) {
+ $operator = $binary_expression->getChildOfType(1, 'n_OPERATOR');
+
+ if ($operator->getConcreteString() != '=') {
+ continue;
+ }
+
+ $variable = $binary_expression->getChildByIndex(0);
+
+ if ($variable->getTypeName() != 'n_VARIABLE') {
+ continue;
+ }
+
+ if ($variable->getConcreteString() == '$this') {
+ $this->raiseLintAtNode(
+ $binary_expression,
+ pht(
+ '`%s` cannot be re-assigned. '.
+ 'This construct will cause a PHP fatal error.',
+ '$this'));
+ }
+ }
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistThisReassignmentXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistThisReassignmentXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistThisReassignmentXHPASTLinterRuleTestCase.php
@@ -0,0 +1,10 @@
+<?php
+
+final class ArcanistThisReassignmentXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(dirname(__FILE__).'/this-reassignment/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/this-reassignment/class.lint-test b/src/lint/linter/xhpast/rules/__tests__/this-reassignment/class.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/this-reassignment/class.lint-test
@@ -0,0 +1,8 @@
+<?php
+class SomeClass {
+ public function __construct() {
+ $this = null;
+ }
+}
+~~~~~~~~~~
+error:4:5
diff --git a/src/lint/linter/xhpast/rules/__tests__/this-reassignment/this.lint-test b/src/lint/linter/xhpast/rules/__tests__/this-reassignment/this.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/this-reassignment/this.lint-test
@@ -0,0 +1,4 @@
+<?php
+$this = null;
+~~~~~~~~~~
+error:2:1
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 26, 11:35 PM (21 h, 50 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6791201
Default Alt Text
D14514.diff (4 KB)
Attached To
Mode
D14514: Write a linter rule for `$this` reassignment
Attached
Detach File
Event Timeline
Log In to Comment