Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

follow value of variable in php script #558

Closed
subabrain opened this issue Dec 22, 2018 · 14 comments
Closed

follow value of variable in php script #558

subabrain opened this issue Dec 22, 2018 · 14 comments

Comments

@subabrain
Copy link

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!

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Jan 3, 2019

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.

@subabrain
Copy link
Author

Hello and Thanks!

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

@TomasVotruba
Copy link
Contributor

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

@subabrain
Copy link
Author

subabrain commented Jan 26, 2019

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();

?>

@TomasVotruba
Copy link
Contributor

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

@subabrain
Copy link
Author

yes thats it 👍

@TomasVotruba
Copy link
Contributor

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
Copy link
Contributor

What is your ultimate goal to do with the value?

@subabrain
Copy link
Author

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

@TomasVotruba
Copy link
Contributor

@subabrain
Copy link
Author

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!

@TomasVotruba
Copy link
Contributor

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

@subabrain
Copy link
Author

ok - sorry ill do that ;)

@nikic
Copy link
Owner

nikic commented Sep 3, 2022

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 nikic closed this as completed Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants