Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14017238
D11504.id27758.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
D11504.id27758.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
@@ -53,8 +53,10 @@
const LINT_BLACKLISTED_FUNCTION = 51;
const LINT_IMPLICIT_VISIBILITY = 52;
const LINT_CALL_TIME_PASS_BY_REF = 53;
+ const LINT_INDENTATION = 54;
private $blacklistedFunctions = array();
+ private $indentation = 2;
private $naminghook;
private $switchhook;
private $version;
@@ -118,6 +120,7 @@
self::LINT_BLACKLISTED_FUNCTION => 'Use of Blacklisted Function',
self::LINT_IMPLICIT_VISIBILITY => 'Implicit Method Visibility',
self::LINT_CALL_TIME_PASS_BY_REF => 'Call-Time Pass-By-Reference',
+ self::LINT_INDENTATION => 'Indentation',
);
}
@@ -160,6 +163,7 @@
self::LINT_ARRAY_SEPARATOR => $advice,
self::LINT_CONSTRUCTOR_PARENTHESES => $advice,
self::LINT_IMPLICIT_VISIBILITY => $advice,
+ self::LINT_INDENTATION => $advice,
);
}
@@ -169,6 +173,11 @@
'type' => 'optional map<string, string>',
'help' => pht('Blacklisted functions which should not be used.'),
),
+ 'xhpast.indentation' => array(
+ 'type' => 'optional int',
+ 'help' => pht(
+ 'Number of whitespace characters to be used for indentation.'),
+ ),
'xhpast.naminghook' => array(
'type' => 'optional string',
'help' => pht(
@@ -197,6 +206,9 @@
case 'xhpast.blacklisted.function':
$this->blacklistedFunctions = $value;
return;
+ case 'xhpast.indentation':
+ $this->indentation = $value;
+ return;
case 'xhpast.naminghook':
$this->naminghook = $value;
return;
@@ -216,7 +228,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '15';
+ return '16';
}
protected function resolveFuture($path, Future $future) {
@@ -293,6 +305,7 @@
'lintMethodModifier' => self::LINT_IMPLICIT_VISIBILITY,
'lintPropertyModifier' => self::LINT_IMPLICIT_VISIBILITY,
'lintCallTimePassByReference' => self::LINT_CALL_TIME_PASS_BY_REF,
+ 'lintIndentation' => self::LINT_INDENTATION,
);
foreach ($method_codes as $method => $codes) {
@@ -3101,6 +3114,99 @@
}
}
+ private function lintIndentation(XHPASTNode $root) {
+ $statement_lists = $root->selectDescendantsOfType('n_STATEMENT_LIST');
+
+ foreach ($statement_lists as $statement_list) {
+ $statements = $statement_list->getChildrenOfType('n_STATEMENT');
+
+ foreach ($statements as $statement) {
+ list($before, $after) = $statement->getSurroundingNonsemanticTokens();
+
+ $indent = '';
+ foreach ($before as $token) {
+ $indent .= preg_replace("/^\s*\n/", '', $token->getValue());
+ }
+
+ if ($indent != $this->getLeadingIndentation($statement)) {
+ $this->raiseLintAtToken(
+ head($before),
+ self::LINT_INDENTATION,
+ pht('Incorrect indentation B.'),
+ preg_replace(
+ "/(?<=\n)\s*/",
+ '',
+ $token->getValue()).$this->getLeadingIndentation($statement));
+ }
+
+ $indent = '';
+ foreach ($after as $token) {
+ $indent .= preg_replace("/^\s*\n/", '', $token->getValue());
+ }
+
+ $next = head($after);
+
+ if (!$next->getNextToken()) {
+ continue;
+ }
+
+ if ($next->getNextToken()->getTypeName() !== '}') {
+ continue;
+ }
+
+ if ($indent != $this->getTrailingIndentation($statement)) {
+ $this->raiseLintAtToken(
+ head($after),
+ self::LINT_INDENTATION,
+ pht('Incorrect indentation A.'),
+ preg_replace(
+ "/(?<=\n)\s*/",
+ '',
+ $token->getValue()).$this->getTrailingIndentation($statement));
+ }
+ }
+ }
+ }
+
+ private function getLeadingIndentation(XHPASTNode $node) {
+ $level = -1;
+
+ for (; $node != null; $node = $node->getParentNode()) {
+ switch ($node->getTypeName()) {
+ case 'n_STATEMENT_LIST':
+ $level++;
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ return str_repeat(' ', $this->indentation * $level);
+ }
+
+ private function getTrailingIndentation(XHPASTNode $node) {
+ $level = -2;
+
+ for (; $node != null; $node = $node->getParentNode()) {
+ switch ($node->getTypeName()) {
+ case 'n_STATEMENT_LIST':
+ $level++;
+ break;
+
+ case 'n_CASE':
+ case 'n_DEFAULT':
+ $level--;
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ return str_repeat(' ', $this->indentation * $level);
+ }
+
public function getSuperGlobalNames() {
return array(
'$GLOBALS',
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 5, 2:47 PM (1 w, 3 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6759476
Default Alt Text
D11504.id27758.diff (4 KB)
Attached To
Mode
D11504: Add a linter rule for PHP indentation
Attached
Detach File
Event Timeline
Log In to Comment