]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Detect use of "+" on a string literal in PHP.
authorepriestley <epriestley@phabricator.com>
Thu, 10 Mar 2011 23:11:41 +0000 (15:11 -0800)
committerepriestley <epriestley@phabricator.com>
Thu, 10 Mar 2011 23:15:45 +0000 (15:15 -0800)
Summary:
This is realistically always wrong and the author means "."

Test Plan:
lint / unit

Reviewed By: crackerjack
Reviewers: crackerjack, aran
CC: crackerjack
Differential Revision: 68

src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test [new file with mode: 0644]

index fd2f1c8ff00f6f3281ec96f3155e11b625c9eaa2..d6aafab743ee25a6a630e9ba9c39fabb0bbcfb84 100644 (file)
@@ -45,6 +45,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
   const LINT_COMMENT_STYLE            = 18;
   const LINT_CLASS_FILENAME_MISMATCH  = 19;
   const LINT_TAUTOLOGICAL_EXPRESSION  = 20;
+  const LINT_PLUS_OPERATOR_ON_STRINGS = 21;
 
 
   public function getLintNameMap() {
@@ -69,6 +70,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
       self::LINT_COMMENT_STYLE            => 'Comment Style',
       self::LINT_CLASS_FILENAME_MISMATCH  => 'Class-Filename Mismatch',
       self::LINT_TAUTOLOGICAL_EXPRESSION  => 'Tautological Expression',
+      self::LINT_PLUS_OPERATOR_ON_STRINGS => 'Not String Concatenation',
     );
   }
 
@@ -146,6 +148,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintHashComments($root);
     $this->lintPrimaryDeclarationFilenameMatch($root);
     $this->lintTautologicalExpressions($root);
+    $this->lintPlusOperatorOnStrings($root);
   }
 
   private function lintTautologicalExpressions($root) {
@@ -977,6 +980,27 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
     );
   }
 
+  private function lintPlusOperatorOnStrings($root) {
+    $binops = $root->selectDescendantsOfType('n_BINARY_EXPRESSION');
+    foreach ($binops as $binop) {
+      $op = $binop->getChildByIndex(1);
+      if ($op->getConcreteString() != '+') {
+        continue;
+      }
+
+      $left = $binop->getChildByIndex(0);
+      $right = $binop->getChildByIndex(2);
+      if (($left->getTypeName() == 'n_STRING_SCALAR') ||
+          ($right->getTypeName() == 'n_STRING_SCALAR')) {
+        $this->raiseLintAtNode(
+          $binop,
+          self::LINT_PLUS_OPERATOR_ON_STRINGS,
+          "In PHP, '.' is the string concatenation operator, not '+'. This ".
+          "expression uses '+' with a string literal as an operand.");
+      }
+    }
+  }
+
   protected function raiseLintAtToken(
     XHPASTToken $token,
     $code,
diff --git a/src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test b/src/lint/linter/xhpast/__tests__/data/wrong-concat-operator.lint-test
new file mode 100644 (file)
index 0000000..aaf0de4
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+"a"."b";
+"a" + "b";
+"a" + $x;
+$x + $y + $z + "q" + 0;
+~~~~~~~~~~
+error:3:1
+error:4:1
+error:5:1