typhoon icon indicating copy to clipboard operation
typhoon copied to clipboard

Resolve a variable type

Open xepozz opened this issue 10 months ago • 1 comments

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?

xepozz avatar Apr 14 '24 21:04 xepozz

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

vudaltsov avatar Apr 19 '24 09:04 vudaltsov