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

Advice: handling dynamically created instance var values #1753

Closed
far-blue opened this issue Jun 5, 2018 · 7 comments
Closed

Advice: handling dynamically created instance var values #1753

far-blue opened this issue Jun 5, 2018 · 7 comments
Labels

Comments

@far-blue
Copy link

far-blue commented Jun 5, 2018

I'm updating some existing code to allow it to be checked with Phan and I have a template engine which allows you to set (using magic methods) values on, effectively, dynamically created instance vars. e.g.

$view = new View();
$view->foo = 1;
$view->baa = 'hello';

Where foo and baa are then used in the template and are not pre-defined anywhere.

Is there a way to effectively tell Phan about this kind of behaviour on a class?

Thanks!

@TysonAndre
Copy link
Member

#805 would need to be done first.

After it was completed, it might be possible to write a plugin to type check the inner body of the template.

@far-blue
Copy link
Author

far-blue commented Jun 5, 2018

I'm not really bothered about the type safety of the values passed to the template engine because the templates themselves aren't php, I'd just prefer not to have (lots of!) warnings about references to undeclared properties on this one particular class which is the 'view' class.

@TysonAndre
Copy link
Member

// @phan-file-suppress PhanUndeclaredProperty might work.

You can also write your own plugin to just suppress undeclared property issues for the given issues.

Resources:

An alternative would be to add a way to specify that user-defined classes have arbitrary dynamic properties and to give up on warning about them (Phan does this if __get or __set are defined, but that probably doesn't help you)

@far-blue
Copy link
Author

far-blue commented Jun 6, 2018

Interestingly, my view class inherits from a parent that does have __set defined. I guess Phan doesn't notice this inheritance maybe?

@TysonAndre
Copy link
Member

It's been a while since I've looked at the magic property code.

It turns out that \Phan\Language\Element\Clazz->getPropertyByNameInContext() only checks for __get() (unconditionally). There was a TODO for that.

  • Defining only __set and not __get is a fairly rare case. It should work properly if __set is defined.

Checking if the parent node was an assignment (to determine if __get()/__set() should be checked for) would be the proper way to do this check

@far-blue
Copy link
Author

far-blue commented Jun 8, 2018

thanks for your reply :) Adding a __get method did the job :)

@TysonAndre
Copy link
Member

Closing this and opening #1756 to track the code fixes I mentioned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants