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

Retrieve local variables for method? #222

Open
dantleech opened this issue Nov 25, 2016 · 3 comments
Open

Retrieve local variables for method? #222

dantleech opened this issue Nov 25, 2016 · 3 comments

Comments

@dantleech
Copy link
Contributor

dantleech commented Nov 25, 2016

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 dantleech changed the title Retrieve in-scope variables for method? Retrieve local variables for method? Nov 25, 2016
@asgrim
Copy link
Member

asgrim commented Nov 25, 2016

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

@Ocramius
Copy link
Member

Ocramius commented Nov 25, 2016

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)

@dantleech
Copy link
Contributor Author

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 dantleech mentioned this issue Nov 27, 2016
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants