]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Stop undeclared variable warning for $obj->{$prop} syntax
authorepriestley <git@epriestley.com>
Thu, 12 May 2011 01:51:25 +0000 (18:51 -0700)
committerepriestley <git@epriestley.com>
Thu, 12 May 2011 05:06:03 +0000 (22:06 -0700)
Summary:
This isn't necessarily a root-cause solution but this syntax is really really
bad. If we want to fix the root cause, I'd recommend making its use a lint
error?

Test Plan:
Unit tests failed before patch, passed afterward.

Reviewed By: aran
Reviewers: jungejason, tuomaspelkonen, aran
CC: aran
Differential Revision: 270

src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test

index d6aafab743ee25a6a630e9ba9c39fabb0bbcfb84..38e215133894f823c0129610c82ac587543f9df8 100644 (file)
@@ -405,7 +405,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
       // ($declarations).
 
       foreach ($vars as $var) {
-        $concrete = $var->getConcreteString();
+        $concrete = $this->getConcreteVariableString($var);
         $declarations[$concrete] = min(
           idx($declarations, $concrete, PHP_INT_MAX),
           $var->getOffset());
@@ -445,7 +445,7 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
           // figure out.
           continue;
         }
-        $concrete = $var->getConcreteString();
+        $concrete = $this->getConcreteVariableString($var);
         if ($var->getOffset() >= idx($declarations, $concrete, PHP_INT_MAX)) {
           // The use appears after the variable is declared, so it's fine.
           continue;
@@ -466,6 +466,13 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
     }
   }
 
+  private function getConcreteVariableString($var) {
+    $concrete = $var->getConcreteString();
+    // Strip off curly braces as in $obj->{$property}.
+    $concrete = trim($concrete, '{}');
+    return $concrete;
+  }
+
   protected function lintPHPTagUse($root) {
     $tokens = $root->getTokens();
     foreach ($tokens as $token) {
index 4e2f723e8243de416c63c995eb2967630b2d16ec..0add1437fbf1043c4814693eb56fee209e74681f 100644 (file)
@@ -155,7 +155,10 @@ class A {
     return 2;
   }
 }
-  
+
+function arrow($o, $x) {
+  echo $o->{$x->{$x->{$x.$x->{$x}}.$x}};
+}
 
 ~~~~~~~~~~
 error:30:3