Page MenuHomePhabricator

D14603.id35568.diff
No OneTemporary

D14603.id35568.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
@@ -96,6 +96,8 @@
'ArcanistCppcheckLinterTestCase' => 'lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php',
'ArcanistCpplintLinter' => 'lint/linter/ArcanistCpplintLinter.php',
'ArcanistCpplintLinterTestCase' => 'lint/linter/__tests__/ArcanistCpplintLinterTestCase.php',
+ 'ArcanistCurlyBraceArrayIndexXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistCurlyBraceArrayIndexXHPASTLinterRule.php',
+ 'ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase.php',
'ArcanistDeclarationParenthesesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDeclarationParenthesesXHPASTLinterRule.php',
'ArcanistDeclarationParenthesesXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistDeclarationParenthesesXHPASTLinterRuleTestCase.php',
'ArcanistDefaultParametersXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDefaultParametersXHPASTLinterRule.php',
@@ -502,6 +504,8 @@
'ArcanistCppcheckLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistCpplintLinter' => 'ArcanistExternalLinter',
'ArcanistCpplintLinterTestCase' => 'ArcanistExternalLinterTestCase',
+ 'ArcanistCurlyBraceArrayIndexXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistDeclarationParenthesesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDeclarationParenthesesXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistDefaultParametersXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
diff --git a/src/lint/linter/xhpast/rules/ArcanistCurlyBraceArrayIndexXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistCurlyBraceArrayIndexXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistCurlyBraceArrayIndexXHPASTLinterRule.php
@@ -0,0 +1,52 @@
+<?php
+
+final class ArcanistCurlyBraceArrayIndexXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 119;
+
+ public function getLintName() {
+ return pht('Curly Brace Array Index');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_WARNING;
+ }
+
+ public function process(XHPASTNode $root) {
+ $index_accesses = $root->selectDescendantsOfType('n_INDEX_ACCESS');
+
+ foreach ($index_accesses as $index_access) {
+ $tokens = $index_access->getChildByIndex(1)->getTokens();
+
+ $left_brace = head($tokens)->getPrevToken();
+ while (!$left_brace->isSemantic()) {
+ $left_brace = $left_brace->getPrevToken();
+ }
+
+ $right_brace = last($tokens)->getNextToken();
+ while (!$right_brace->isSemantic()) {
+ $right_brace = $right_brace->getNextToken();
+ }
+
+ if ($left_brace->getValue() == '{' || $right_brace->getValue() == '}') {
+ $replacement = null;
+ foreach ($index_access->getTokens() as $token) {
+ if ($token === $left_brace) {
+ $replacement .= '[';
+ } else if ($token === $right_brace) {
+ $replacement .= ']';
+ } else {
+ $replacement .= $token->getValue();
+ }
+ }
+
+ $this->raiseLintAtNode(
+ $index_access,
+ pht('Use `%s` instead of `%s`.', "\$x['key']", "\$x{'key'}"),
+ $replacement);
+ }
+ }
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase.php
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistCurlyBraceArrayIndexXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/curly-brace-array-index/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/array.lint-test b/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/array.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/array.lint-test
@@ -0,0 +1,9 @@
+<?php
+$x['key'];
+$y{'key'};
+~~~~~~~~~~
+warning:3:1
+~~~~~~~~~~
+<?php
+$x['key'];
+$y['key'];
diff --git a/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/nested.lint-test b/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/nested.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/nested.lint-test
@@ -0,0 +1,7 @@
+<?php
+$x[$y{'key'}];
+~~~~~~~~~~
+warning:2:4
+~~~~~~~~~~
+<?php
+$x[$y['key']];
diff --git a/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/whitespace.lint-test b/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/whitespace.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/curly-brace-array-index/whitespace.lint-test
@@ -0,0 +1,7 @@
+<?php
+$x { 'key' /* comment */ };
+~~~~~~~~~~
+warning:2:1
+~~~~~~~~~~
+<?php
+$x [ 'key' /* comment */ ];

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 11, 8:01 AM (1 d, 18 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6728500
Default Alt Text
D14603.id35568.diff (5 KB)

Event Timeline