]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Fix PHT linter
authorvrana <jakubv@fb.com>
Sat, 19 Jan 2013 18:26:24 +0000 (10:26 -0800)
committervrana <jakubv@fb.com>
Sat, 19 Jan 2013 19:48:17 +0000 (11:48 -0800)
Summary: Variables in strings and concatenation lists.

Test Plan:
New unit tests.

  preg_match('/^((?>[^$\\\\]*)|\\\\.)*$/s', str_repeat('a', 1e6));

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin
Differential Revision: https://secure.phabricator.com/D4540

src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/xhpast/pht.lint-test

index b845f55ed01b62a7f28cef0bd1f0392879a3531d..ce5b3062ae6248c23001e2df731fb697ad27d3c0 100644 (file)
@@ -156,7 +156,7 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
   }
 
   public function getCacheVersion() {
-    return '1-'.md5_file(xhpast_get_binary_path());
+    return '2-'.md5_file(xhpast_get_binary_path());
   }
 
   public function lintPath($path) {
@@ -298,25 +298,11 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
         continue;
       }
 
-      // TODO: Desceptively, n_STRING_SCALAR may include variables, mostly
-      // because I was lazy when implementing the parser. We should perform more
-      // strict checks here, and/or enhance the parser.
-
       $identifier = $parameters->getChildByIndex(0);
-      if ($identifier->getTypeName() == 'n_STRING_SCALAR' ||
-          $identifier->getTypeName() == 'n_HEREDOC') {
+      if ($this->isConstantString($identifier)) {
         continue;
       }
 
-      if ($identifier->getTypeName() == 'n_CONCATENATION_LIST') {
-        foreach ($identifier->getChildren() as $child) {
-          if ($child->getTypeName() == 'n_STRING_SCALAR' ||
-              $child->getTypeName() == 'n_OPERATOR') {
-            continue 2;
-          }
-        }
-      }
-
       $this->raiseLintAtNode(
         $call,
         self::LINT_PHT_WITH_DYNAMIC_STRING,
@@ -325,6 +311,42 @@ final class ArcanistXHPASTLinter extends ArcanistLinter {
     }
   }
 
+  private function isConstantString(XHPASTNode $node) {
+    $value = $node->getConcreteString();
+
+    switch ($node->getTypeName()) {
+      case 'n_HEREDOC':
+        if ($value[3] == "'") { // Nowdoc: <<<'EOT'
+          return true;
+        }
+        $value = preg_replace('/^.+\n|\n.*$/', '', $value);
+        break;
+
+      case 'n_STRING_SCALAR':
+        if ($value[0] == "'") {
+          return true;
+        }
+        $value = substr($value, 1, -1);
+        break;
+
+      case 'n_CONCATENATION_LIST':
+        foreach ($node->getChildren() as $child) {
+          if ($child->getTypeName() == 'n_OPERATOR') {
+            continue;
+          }
+          if (!$this->isConstantString($child)) {
+            return false;
+          }
+        }
+        return true;
+
+      default:
+        return false;
+    }
+
+    return preg_match('/^((?>[^$\\\\]*)|\\\\.)*$/s', $value);
+  }
+
   public function lintPHP53Features($root) {
 
     $functions = $root->selectTokensOfType('T_FUNCTION');
index b9b1c711eb10793bd5122a1703786c9b558ff887..5b99f06fdaa55007308ff525edde0a6b01d9c8b5 100644 (file)
@@ -5,7 +5,23 @@ pht('a'.'b');
 pht(f());
 pht();
 pht($a);
+pht('a'.$a);
+pht('$a');
+pht("$a");
 pht('%s', $a);
+
+pht(<<<EOT
+a
+EOT
+);
+
+pht(<<<EOT
+$a
+EOT
+);
 ~~~~~~~~~~
 disabled:5:1
 disabled:7:1
+disabled:8:1
+disabled:10:1
+disabled:18:1