-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add magic getter method support to Entities #5309
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
Conversation
When using Symfony Forms, it permits the implementation of magic getter methods in those Entities by using the `__call` magic method. It basically allows one to do the following in an Entity: public function __call($method, $arguments) { if (preg_match('/^(get|is|has)([A-Z].*)$/', $method, $matches)) { return $this->{lcfirst($matches[2])}; } throw new \Exception(sprintf( 'Method does not exist for class "%s".', get_class($this) )); }
All three travisbot machines seems to have failed due to |
Please add some tests (for each cases of your |
$result[self::VALUE] = $objectOrArray->$hasser(); | ||
} else { | ||
throw new InvalidPropertyException(sprintf('Neither property "%s" nor method "%s()" nor method "%s()" exists in class "%s"', $property, $getter, $isser, $reflClass->name)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception message is wrong (property).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind explaining? I don't understand.
Edit: Actually, I think I get what you mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just meant, that the exception message mentions the "property %s" but __call is not for properties but methods.
Btw, why do you need the is_callable
test when you already have tested for __call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_callable calls are needed to be able to tell if those magic "methods" were actually implemented or not on that class. You should see the latest commit, it's probably a bit clearer in that :).
Edit: For clarification,method_exists
or .hasMethod
can't tell whether or not those methods exist.
Edit2: Thinking about it, that was a bit of a brain fart since Is_callable will always return true due to the __call method.
Added some tests. A note: even after running composer install I was unable to run the tests since PHP would fail because it would try to load something from Doctrine/orm/lib which isn't listed as a dependency in composer.json. In the end, I added it as a dependecy and everything ran smoothly. Am I missing something? |
@Bogdanp to run the testsuite, you need to ask composer to install the dev requirements too by adding the |
This allows Entities to have __call methods that don't necessarily "implement" any of the magic getter methods.
Alright, got it :). Thanks. Is there some place where this stuff is documented? I had checked the getting started contributing page but it said nothing about the --dev option. |
then it should be added in the doc. Btw, a good way to know what is required to setup the testsuite when a project uses travis is to look at the |
Good to know! |
Please also test the case of an unsupported method (which will not behave properly IMO) and fix the coding standards |
You are right, it won't. I hadn't considered that is_callable will always pass because of the __call method (I actually had a "d'oh" moment when I realized that). I'll have to come up with a better solution after I get home since I'm at the office right now. |
the point is, as soon as |
Yes, that is what I was saying in my previous comment. The fix should be in the last two commits, simply checking for the methods themselves should be fine and moving them to the bottom of the condition stack should prevent any exceptions or errors generated by |
Please add a test for a class which does not support the magic getter (throwing an exception in it) but support the magic isser. I can assure you it will fail by throwing you the exception |
in fact, you test does not allow testing all cases: you use the same class supporting all 3 magic methods. But this does not allow you to know which case was called. You need 3 classes, one for each case |
You are correct, there is no proper way to determine which method should be called or if they are callable and in the case that the author of the Entity has written a |
@stof I don't think we should support a magic isser when is magic getter already throwed an exception. The only solution that would support this, is catching exceptions as @Bogdanp suggested and assuming __call would raise an exception in this case. I'd suggest to simply invoke _call with the plain property value (no getter or isser variation) when there is a __call defined. |
@Tobion It makes no sense to call it with the plain property value. If you use magic methods to implement getters, we cannot assume you implemented it by calling the method the same way than the property (which would not be consistent with what we do for other cases btw). The point is, we cannot support |
My question is, should I implement it the way I described above? I think that in order to avoid confusion we can either keep the Exception that was thrown by Regarding the |
This implementation unfortunately is obsolete since the PropertyAccess component was introduced. Please feel welcome to reimplement it for that component. Simply open a new PR and we will check it again. :) See also #7263. |
This PR was merged into the master branch. Discussion ---------- [PropertyAccess] add support for magic call Hi, I add support for magic call with the `PropertyAccess` the is basic implementation, if no `getter`, `isser`, or `hasser` or `_get` is found and there is `__call` then the PropertyAccess call the getter the same for setter. This functionality is disable by default | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | seems OK (failure/errors are the same on master) | Fixed tickets | #4683, #6413, #5309 | License | MIT | Doc PR | symfony/symfony-docs#2472 - [x] submit changes to the documentation @bschussek is this ok ? Commits ------- a785baa [PropertyAccess] add support for magic call, related to #4683
This PR was merged into the 7.1 branch. Discussion ---------- [Notifier] Add Unifonic notifier bridge Add docs for symfony/symfony#5309 Fixes #19301 Commits ------- 9e47f5f [Notifier] Add Unifonic notifier bridge
When using Symfony Forms, it permits the implementation of magic getter methods in those Entities by using the
__call
magic method.It basically allows one to do the following in an Entity: