PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

follow value of variable in php script

Open subabrain opened this issue 6 years ago • 13 comments

Hi, i just want to ask if its possible to follow a variable through a php script?

For example:

$val = "test"; $val2 = $val; echo $val2;

I just found the traverse - but how can i do that exactly?

I could do this with regex - but i want to know if its working with the php parser too?

Thanks and Nice Time!

subabrain avatar Dec 22 '18 21:12 subabrain

I use this approach to get property uses in code:

https://github.com/rectorphp/rector/blob/9db613e92ee8c2f5f390937d856a25116b9b1071/packages/Php/src/Rector/Property/CompleteVarDocTypePropertyRector.php#L142-L153

$propertyAssignNodes = $this->betterNodeFinder->find([$classNode], function (Node $node) use (
    $propertyName
): bool {
    if ($node instanceof Assign) {
        if ($node->var instanceof PropertyFetch) {
            // is property match
            return $this->isName($node->var, $propertyName);
        }
    }
    return false;
});

Basically use native NodeFinder and find all variable with that name looking for assigs.

What exactly is your goal? Similar analysis does PHPStan and Rector builds on top of that.

TomasVotruba avatar Jan 03 '19 09:01 TomasVotruba

Hello and Thanks!

sorry but i didnt get it to work ... maybe you can give an example - thanks a lot!

subabrain avatar Jan 26 '19 09:01 subabrain

What is your goal actually? There might be a better solution.

TomasVotruba avatar Jan 26 '19 09:01 TomasVotruba

ok - here an example:

<?php

$var1 = "some_value"; //First assigned Value
$var2 = $var1; //The first variable ($var1) is assigned to $var2

//...

//Now i will echo the last Variable is assigned to the first one
echo $var2;

//Now i want to get out the value of the first assigned variable
//I could do this with regex but if i have the following Construct:

class test {

    public $var1 = "first";
	public $var2;
	
	public function test() {
	
		$this->var2 = $this->var1;
		
	}
	
	public function echo_it() {
	
		echo $this->var2;
	
	}

}

$inst = new test();
$inst->test();
$inst->echo_it();

?>

subabrain avatar Jan 26 '19 10:01 subabrain

I see. So your goal is to get value of echo $var2;, resp. echo $this->var2;?

TomasVotruba avatar Jan 26 '19 10:01 TomasVotruba

yes thats it 👍

subabrain avatar Jan 26 '19 10:01 subabrain

I tried Rector + PHPStan to resolve the value, but unfortunatelly PHPStan is not capable to analyze the value.

Here is code I used: https://github.com/rectorphp/rector/compare/analasis-example?expand=1#diff-3fd2b223ed0f173609588cfd28904e34

In that way, you'd have to write own NodeScopeResolver - https://github.com/phpstan/phpstan/blob/master/src/Analyser/NodeScopeResolver.php, or extend it.

TomasVotruba avatar Jan 26 '19 12:01 TomasVotruba

What is your ultimate goal to do with the value?

TomasVotruba avatar Jan 26 '19 12:01 TomasVotruba

hey thanks a lot - but how to ge a node from the code? Thanks!

subabrain avatar Jan 26 '19 16:01 subabrain

I started with Documentation: https://github.com/nikic/PHP-Parser/blob/master/doc/2_Usage_of_basic_components.markdown#node-traversation

I go through one-change example in this post: How to change PHP code with Abstract Syntax Tree

TomasVotruba avatar Jan 26 '19 16:01 TomasVotruba

okay - i see - but i get the following error:

Fatal error: Uncaught Error: Call to a member function getNodeStaticType() on null in E:\xampp\htdocs\rector\src\Rector\TypeAnalyzerTrait.php:76 Stack trace: #0 E:\xampp\htdocs\rector\utils\showcase\src\GetStringValueRector.php(29): Rector\Rector\AbstractRector->getStaticType(Object(PhpParser\Node\Stmt\Expression)) #1 E:\xampp\htdocs\rector\utils\showcase\src\GetStringValueRector.php(52): Rector\Showcase\GetStringValueRector->refactor(Object(PhpParser\Node\Stmt\Expression)) #2 {main} thrown in E:\xampp\htdocs\rector\src\Rector\TypeAnalyzerTrait.php on line 76

maybe you can help me a last time - thx a lot!

subabrain avatar Jan 26 '19 18:01 subabrain

Open issue at Rector with full code, so we don't spam here

TomasVotruba avatar Jan 26 '19 18:01 TomasVotruba

ok - sorry ill do that ;)

subabrain avatar Jan 26 '19 18:01 subabrain

Closing this as out of scope for this library. Other libraries provide support for this on top of this one, e.g. various static analyzers. https://github.com/ircmaxell/php-cfg is also worth mentioning, which tracks variable assignments in SSA form.

nikic avatar Sep 03 '22 13:09 nikic