Page MenuHomePhabricator

D14514.diff
No OneTemporary

D14514.diff

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

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)

Event Timeline