Page MenuHomePhabricator

D12376.diff
No OneTemporary

D12376.diff

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
@@ -57,6 +57,7 @@
const LINT_UNNECESSARY_FINAL_MODIFIER = 55;
const LINT_UNNECESSARY_SEMICOLON = 56;
const LINT_SELF_MEMBER_REFERENCE = 57;
+ const LINT_LOGICAL_OPERATORS = 58;
private $blacklistedFunctions = array();
private $naminghook;
@@ -127,6 +128,7 @@
self::LINT_UNNECESSARY_FINAL_MODIFIER => 'Unnecessary Final Modifier',
self::LINT_UNNECESSARY_SEMICOLON => 'Unnecessary Semicolon',
self::LINT_SELF_MEMBER_REFERENCE => 'Self Member Reference',
+ self::LINT_LOGICAL_OPERATORS => 'Logical Operators',
);
}
@@ -172,6 +174,7 @@
self::LINT_UNNECESSARY_FINAL_MODIFIER => $advice,
self::LINT_UNNECESSARY_SEMICOLON => $advice,
self::LINT_SELF_MEMBER_REFERENCE => $advice,
+ self::LINT_LOGICAL_OPERATORS => $advice,
);
}
@@ -239,7 +242,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '20';
+ return '21';
}
protected function resolveFuture($path, Future $future) {
@@ -321,6 +324,7 @@
'lintUnnecessarySemicolons' => self::LINT_UNNECESSARY_SEMICOLON,
'lintConstantDefinitions' => self::LINT_NAMING_CONVENTIONS,
'lintSelfMemberReference' => self::LINT_SELF_MEMBER_REFERENCE,
+ 'lintLogicalOperators' => self::LINT_LOGICAL_OPERATORS,
);
foreach ($method_codes as $method => $codes) {
@@ -3456,6 +3460,27 @@
}
}
+ private function lintLogicalOperators(XHPASTNode $root) {
+ $logical_ands = $root->selectTokensOfType('T_LOGICAL_AND');
+ $logical_ors = $root->selectTokensOfType('T_LOGICAL_OR');
+
+ foreach ($logical_ands as $logical_and) {
+ $this->raiseLintAtToken(
+ $logical_and,
+ self::LINT_LOGICAL_OPERATORS,
+ pht('Use `%s` instead of `%s`.', '&&', 'and'),
+ '&&');
+ }
+
+ foreach ($logical_ors as $logical_or) {
+ $this->raiseLintAtToken(
+ $logical_or,
+ self::LINT_LOGICAL_OPERATORS,
+ pht('Use `%s` instead of `%s`.', '||', 'or'),
+ '||');
+ }
+ }
+
/**
* Retrieve all calls to some specified function(s).
*
diff --git a/src/lint/linter/__tests__/xhpast/logical-operators.lint-test b/src/lint/linter/__tests__/xhpast/logical-operators.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/logical-operators.lint-test
@@ -0,0 +1,22 @@
+<?php
+
+$x = false;
+$y = true;
+
+$x and $y;
+$x && $y;
+$x or $y;
+$x || $y;
+~~~~~~~~~~
+advice:6:4
+advice:8:4
+~~~~~~~~~~
+<?php
+
+$x = false;
+$y = true;
+
+$x && $y;
+$x && $y;
+$x || $y;
+$x || $y;

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 20, 6:33 AM (18 h, 27 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7705282
Default Alt Text
D12376.diff (2 KB)

Event Timeline