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

Return the object property that is actually accessible. #3371

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

ErikRoelofs
Copy link

Since there is a check for two types of property, we should also return the one we found.

Without this, cases where a property is accessed that exists in the cast of the object, but not directly, will not be available and will throw notices and end up with NULL.

We noticed this in our project because of using a DateTimeInterface's date property (which I wouldn't recommend, but still). The if check will respond true because if you cast a DateTimeImmutable to an array it has a 'date' key, but trying to access it directly does not actually work (at least not in PHP 7.4. It apparently worked before as the code had been running fine before we upgraded.)

My proposed change should work in all situations.

Since there is a check for two types of property, we should also return the one we found.
@ErikRoelofs
Copy link
Author

Hm, the nightly build has failed. Is that something my code did? It looks more like an issue with the nightly build or some other part of Twig?

Copy link
Contributor

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Can you add some tests that would fail before your changes?

if (isset($object->$item)) {
return $object->$item;
}
return ((array) $object)[(string) $item];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ((array) $object)[(string) $item];
return (isset($object->$item) ? $object->$item : ((array) $object)[(string) $item];

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

Successfully merging this pull request may close these issues.

3 participants