Retrieve local variables for method?
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
// ];
I love this idea! We can definitely consider this one :)
Oh hell yeah, this would be awesome!
Could detect things like:
- local variable definition
- accessed instance properties
- accessed static properties
- accessed globals (
globalkeyword) - accessed super-globals (unsure about this one - probably sufficient to do a diff between locally defined variables and accessed variables)
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:
- Use
@varannotations to infer member property types. - Fallback to guessing from the
$thisassignations 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.