-
-
Notifications
You must be signed in to change notification settings - Fork 193
ClosureExpressionVisitor > Don't duplicate the accessor when the field already starts with it #131
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
Is there anything I can do to bring this PR forward? I'm currently using this fork in my projects, would be great to rely on a stable tag again :D 🙏 |
@@ -49,7 +49,12 @@ public static function getObjectFieldValue($object, $field) | |||
$accessors = ['get', 'is']; | |||
|
|||
foreach ($accessors as $accessor) { | |||
$accessor .= $field; | |||
// Don't duplicate the accessor when the field already starts with it | |||
if (0 === strpos($field, $accessor)) { |
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'm afraid this check on its own will lead to false positives: issue
, island
, isolation
. Better heuristic would be to look for is
followed by a capital letter
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.
But then it can be worse performace-wise, not sure if it's worth the hassle
I added another approach, using a nice regex, in 7fa9e35. I think this is the winner. What do you think? I will cleanup/squash the commits once its ok. |
@malarzm what does it take to get this merged? Thanks :) |
b0e0ac0
to
ed85eda
Compare
Rebased the PR, squashed it and applied Doctrine CS. |
@Ocramius do you think this can be merged? If not, what is needed to get it to a mergable state? |
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 would like to have this PR merged to prevent keeping it longer on hold for a second year. Except my question on the Regex, there's nothing to add from my side.
On a side note: method_exists
also returns true when the method is private (https://3v4l.org/4pBgc). This part of the library was always prone to bugs, but maybe we can handle this differently in another PR, if it is desired.
|
||
return $object->$accessor(); | ||
if (preg_match('/^is[A-Z]+/', $field) === 1 && method_exists($object, $field)) { |
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.
@Ocramius Do we need this Regex check? Having only the method_exists()
, we could even use accessor-methods that haven't a is
or get
prefix.
I agree, let's get this merged. TBH, I would keep the logic as-is and rather refactor this in a separate pull request. @ruudk could you rebase one last time? Thanks! |
ed85eda
to
1695209
Compare
Done. Should I squash all the commits into 1 or do you do this on merge? |
If you don't mind I'd appreciate a squash before merging - less work for me 😉 |
1695209
to
5752bc9
Compare
Done :) |
You can fix this by running |
When your property is called `private $isActive = true` with `function isActive()` as the getter, it should be possible to query it with `isActive = true` without getting errors.
5752bc9
to
f43cf44
Compare
Fixed it manually... PHPCS doesn't work really nice, see:
Nothing works. |
Thanks for the heads up, and sorry you had to fix it manually. Let me take another look at that after taking care of your PR 👍 |
FWIW, build completed successfully but build result wasn’t reported back correctly: https://travis-ci.org/doctrine/collections/builds/502138875. Thanks @ruudk! |
Wow, thanks for merging it 🙌🥳 |
Don’t celebrate too soon, need to dig into the changes before releasing a new version. I hope to be able to do it shortly. |
This PR tries to fix an issue I am facing in my application that uses Doctrine2 ORM.
I have the following setup:
I'm trying to get all PaymentMethods from the Platform that are active:
When I call
$platform->getActivePaymentMethods()
I get:I can fix this of course by changing my Criteria to
Criteria::expr()->eq('active', true)
but then I will get in troubles when it tries to run the expression on the PersistentCollection instead of the ArrayCollection.This fix makes the getter a bit smarter. When you prefix the field with
is*
it doesn't re-applyis
as an accessor. Instead ofisIsActive
it will just doisActive
.