Page MenuHomePhabricator

D11504.diff
No OneTemporary

D11504.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
@@ -122,6 +122,7 @@
'ArcanistImplicitConstructorXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistImplicitConstructorXHPASTLinterRule.php',
'ArcanistImplicitFallthroughXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistImplicitFallthroughXHPASTLinterRule.php',
'ArcanistImplicitVisibilityXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistImplicitVisibilityXHPASTLinterRule.php',
+ 'ArcanistIndentationXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistIndentationXHPASTLinterRule.php',
'ArcanistInnerFunctionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistInnerFunctionXHPASTLinterRule.php',
'ArcanistInstallCertificateWorkflow' => 'workflow/ArcanistInstallCertificateWorkflow.php',
'ArcanistInstanceOfOperatorXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistInstanceOfOperatorXHPASTLinterRule.php',
@@ -379,6 +380,7 @@
'ArcanistImplicitConstructorXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistImplicitFallthroughXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistImplicitVisibilityXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistIndentationXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistInnerFunctionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistInstallCertificateWorkflow' => 'ArcanistWorkflow',
'ArcanistInstanceOfOperatorXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
diff --git a/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test b/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test
--- a/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test
+++ b/src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test
@@ -1,8 +1,8 @@
<?php
class MyClass {
- public function myfunc($var) {
- echo $var;
- }
+ public function myfunc($var) {
+ echo $var;
+ }
}
$myvar = true;
diff --git a/src/lint/linter/__tests__/xhpast/indentation.lint-test b/src/lint/linter/__tests__/xhpast/indentation.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/indentation.lint-test
@@ -0,0 +1,26 @@
+<?php
+
+final class SomeClass {
+public function someMethod() {
+return null;
+}
+}
+
+if (function_exists('json_last_error_msg'))
+json_last_error_msg();
+~~~~~~~~~~
+error:3:13
+advice:4:1
+advice:5:1
+advice:10:1
+~~~~~~~~~~
+<?php
+
+final class SomeClass {
+ public function someMethod() {
+ return null;
+ }
+}
+
+if (function_exists('json_last_error_msg'))
+ json_last_error_msg();
diff --git a/src/lint/linter/__tests__/xhpast/php54-features.lint-test b/src/lint/linter/__tests__/xhpast/php54-features.lint-test
--- a/src/lint/linter/__tests__/xhpast/php54-features.lint-test
+++ b/src/lint/linter/__tests__/xhpast/php54-features.lint-test
@@ -1,14 +1,14 @@
<?php
f()[0];
- m()[0];
+m()[0];
// The check above should be this, see T4334.
// $o->m()[0];
~~~~~~~~~~
error:3:5
-error:4:9
+error:4:5
~~~~~~~~~~
~~~~~~~~~~
{
diff --git a/src/lint/linter/xhpast/rules/ArcanistIndentationXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistIndentationXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistIndentationXHPASTLinterRule.php
@@ -0,0 +1,84 @@
+<?php
+
+final class ArcanistIndentationXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ private $indentation = ' ';
+
+ const ID = 73;
+
+ public function getLintName() {
+ return pht('Indentation');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_ADVICE;
+ }
+
+ public function getLinterConfigurationOptions() {
+ return parent::getLinterConfigurationOptions() + array(
+ 'xhpast.indentation' => array(
+ 'type' => 'optional string',
+ 'help' => pht('String to be used for indentation.'),
+ ),
+ );
+ }
+
+ public function setLinterConfigurationValue($key, $value) {
+ switch ($key) {
+ case 'xhpast.indentation':
+ $this->indentation = $value;
+ return;
+
+ default:
+ return parent::getLinterConfigurationOptions();
+ }
+ }
+
+ public function process(XHPASTNode $root) {
+ $nodes = $root->selectDescendantsOfType('n_STATEMENT');
+
+ foreach ($nodes as $node) {
+ $indent = $node->getIndentation();
+ $expected = $this->getExpectedIndentation($node);
+
+ if ($expected === null) {
+ continue;
+ }
+
+ if ($indent != $expected) {
+ $this->raiseLintAtOffset(
+ $node->getOffset() - strlen($indent),
+ pht('Incorrect indentation, expected "%s"', $expected),
+ $indent,
+ $expected);
+ }
+ }
+ }
+
+ private function getExpectedIndentation(XHPASTNode $node) {
+ $level = 0;
+
+ for ($prev = null; $node !== null; $prev = $node, $node = $node->getParentNode()) {
+ switch ($node->getTypeName()) {
+ case 'n_STATEMENT_LIST':
+ if ($node->getLineNumber() == $node->getEndLineNumber()) {
+ return null;
+ }
+
+ $level++;
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ if ($level <= 0) {
+ return '';
+ }
+
+ return str_repeat($this->indentation, $level - 1);
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 15, 2:13 AM (15 h, 22 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6712951
Default Alt Text
D11504.diff (5 KB)

Event Timeline