BetterReflection icon indicating copy to clipboard operation
BetterReflection copied to clipboard

Retrieve local variables for method?

Open dantleech opened this issue 9 years ago • 3 comments

Would it be reasonable to retrieve the local variables which are available in a reflection method?

e.g.

class Foobar
{
    public function barfoo(NumberOne $one, NumberTwo $two)
    {
         $numberThree = 3;
    }
}
$vars = $reflection->getMethod('barfoo')->getLocalVariables();

// [
//     'one' => Variable(type=NumberOne, declaredAt=2)
//     'two  => Variable(type=NumberTwo, declaredAt=2)
//     'numberThree' => Variable(type=int, declaredAt=4
// ];

dantleech avatar Nov 25 '16 10:11 dantleech

I love this idea! We can definitely consider this one :)

asgrim avatar Nov 25 '16 10:11 asgrim

Oh hell yeah, this would be awesome!

Could detect things like:

  • local variable definition
  • accessed instance properties
  • accessed static properties
  • accessed globals (global keyword)
  • accessed super-globals (unsure about this one - probably sufficient to do a diff between locally defined variables and accessed variables)

Ocramius avatar Nov 25 '16 15:11 Ocramius

My motivation here is writing an auto-complete engine, as such the main problem is inferring the types of the local variables - we would need to resolve types from ASSIGN nodes the best we can (e.g. gettype($bar = $boo->baz->bad()) == 'string').

The only technical problem, I think, is how we determine the types of class properties so that we can resolve the expression chain:

  1. Use @var annotations to infer member property types.
  2. Fallback to guessing from the $this assignations in the __construct.

accessed instance properties accessed static properties accessed globals (global keyword) accessed super-globals (unsure about this one - probably sufficient to do a diff between locally defined variables and accessed variables)

Makes sense - I guess these could be collected in the same visitor/thing.

dantleech avatar Nov 27 '16 09:11 dantleech