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

Resolve a variable type #37

Open
xepozz opened this issue Apr 14, 2024 · 1 comment
Open

Resolve a variable type #37

xepozz opened this issue Apr 14, 2024 · 1 comment

Comments

@xepozz
Copy link

xepozz commented Apr 14, 2024

I'm looking for tools which may help me to reach my goal. Hope typhoon can do it and I can help it do to it.

Goal

Goal is the easiest and hardest at the same time.

I need to find a variable type in some context.

Example

Let's imagine the following code:

index.php:

$var = 1;

The ways it could go

So there are two ways to detect the variable type:

  1. Run the code and call gettype($var)
  2. Run a static analyzer

The first way is quite simple, but I'd say impossible because of a lot of restrictions: environment to run, an entrypoint to start a process and so on
The second way is more about building kind of static analyzer. If it not matches with the project philosophy please stop me.

Typhoon's turn to decide

So having the second way to get a variable type we can start from the bottom and try to detect a type of $var1 and $var2 in the following code.

function a(bool $param) {
    $var1 = 1; // int
    $var2 = $param ? 'string' : ['array']; // union of string and array
}

It would be so boring to build one more static analyzer and even think about it. I don't know about the ways it go, but I'd start from a specific visitor for the PHP-Parser.

If you're willing to ask me why so, I tell you that's just my case to use it. That's why I'm here asking help to cover my only case.

So, PHP-Parser is available, also is available a result from the debug_backtrace() function / Exception::getTrace() with possible caught arguments like from the following snippet:

<?php

declare(strict_types=1);

function test($var1 = 1)
{
    $var2 = 'string';
    throw new \Exception('Hello!');
}

try {
    test(5);
} catch (\Throwable $e) {
    var_dump(
        $e->getTrace()
    );
}

If it helps I can get the job finding the right way to the destination (a variable from the execution plan) to my hands.

Solution

I thought a bit about the solution and see it like a class that:

  • Is PHP-Parser's visitor
  • Tracks the vars (arguments, parameters) and theirs types inside the nodes
  • Look after the real variables and makes decision based on the values (args from the debug_backtrace() function)

After attaching the visitor it may be able to call a method find the variable "var1" type with the context {context} where {context} is something special that contains all the necessary information to help the method to do it's work.


I'm open to help to develop these reflection.
Any thought?

@vudaltsov
Copy link
Member

Hi, @xepozz. Thank you for the interest in the Typhoon project !

Right now we don't have plans to analyze procedural code, because it is out of scope of reflection which only provides function and class signatures. That would basically be a step to writing a static analyzer. It's a non-trivial task, that requires type comparator, variable context, loop analysis and many other things. Right now the main priorities are:

  1. Finish 0.4.0 and prepare for 1.0.0.
  2. Add tools based on Reflection: hydrator, assertions, etc.
  3. Then we might consider moving towards more complicated things ;)

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

2 participants