From: epriestley Date: Thu, 12 May 2011 01:51:25 +0000 (-0700) Subject: Stop undeclared variable warning for $obj->{$prop} syntax X-Git-Tag: conduit-4~341 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=3edd525f836e35601996db2f5c91255d110ca1a6;p=arcanist.git Stop undeclared variable warning for $obj->{$prop} syntax 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 --- diff --git a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php index d6aafab7..38e21513 100644 --- a/src/lint/linter/xhpast/ArcanistXHPASTLinter.php +++ b/src/lint/linter/xhpast/ArcanistXHPASTLinter.php @@ -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) { diff --git a/src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test b/src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test index 4e2f723e..0add1437 100644 --- a/src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test +++ b/src/lint/linter/xhpast/__tests__/data/undeclared-variables.lint-test @@ -155,7 +155,10 @@ class A { return 2; } } - + +function arrow($o, $x) { + echo $o->{$x->{$x->{$x.$x->{$x}}.$x}}; +} ~~~~~~~~~~ error:30:3