Skip to content

[ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods #16630

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

Merged

Conversation

mytuny
Copy link
Contributor

@mytuny mytuny commented Mar 20, 2022

[ExpressionLanguage] [NEW FEATURE]

This PR introduces the support for nullsafe operator in expressions that works with accessing object's properties and methods.

The proposed change here, includes a paragraph under the sub-page /expression_language/syntax to describe the new feature usage. The sub-paragraph has a title of Nullsafe operator under the paragraph Working with Objects.

The actual work related to this Doc PR available as Symfony PR #45795.

@nicolas-grekas
Copy link
Member

Actually, the nullsafe operator only guards against null. It does not guard against undefined properties/keys.
This PR should be updated accordingly.

nicolas-grekas added a commit to symfony/symfony that referenced this pull request Apr 4, 2022
…r (mytuny)

This PR was merged into the 6.1 branch.

Discussion
----------

[ExpressionLanguage] Add support for null-safe operator

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #45411, #21691
| License       | MIT
| Doc PR        | symfony/symfony-docs#16630

This is a long-time-lasting feature for the `ExpressionLanguage` component. I've been waiting for the support of `Nullsafe operator` in expressions dealing with mutable objects, until I finally decided to work on it once for all 👍

The lack of [nullsafety feature](https://wiki.php.net/rfc/nullsafe_operator) has been repeatedly reported as a BUG several time  (e.g [#45411](#45411) & [#21691](#21691)) when it is actually a missing feature.

Currently, expressions like `foo.bar` assumes that the property `bar` "always" exists on the object `foo` and if doesn't the parser throws a `RuntimeException`. Although, sometimes, that's exactly the behavior we need, some other times we may work with mutable objects with uncontrolled structure, thus, such assumption is error-prone and will force adding extra checks making the expression uglier and less readable.

The proposed work, introduces the support for the `?.` syntax alongside with the usual `.` syntax to help working with objects with dynamic structure. The two notations works identically in all normal cases. The difference occurs when trying to access non-existant properties and/or methods where the `.` notation will throw a `RuntimeException` as usual and the `?.` notation will return `null` instead and no errors nor exceptions will be thrown. Hence the name "Null-Safe".

PS: This work account ONLY for accessing **object's** properties and methods. It does not account for non-existant **array** items which is a seperate problem that can be addressed by introducing the [null coalescing](https://wiki.php.net/rfc/isset_ternary) operator. Another feature that I'm currently working on as well 💯

Commits
-------

946c59f [ExpressionLanguage] Add support for null-safe operator
@nicolas-grekas nicolas-grekas changed the title [ExpressionLanguage] Add support for Nullsafe syntax for accessing ob… [ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods Apr 4, 2022
@javiereguiluz javiereguiluz removed the Waiting Code Merge Docs for features pending to be merged label Apr 4, 2022
@javiereguiluz javiereguiluz modified the milestones: next, 6.1 Apr 4, 2022
@javiereguiluz javiereguiluz changed the base branch from 5.4 to 6.1 April 4, 2022 15:32
@javiereguiluz javiereguiluz force-pushed the feature_expressionlanguage_nullsafe branch from cbca2b4 to 23bf15d Compare April 4, 2022 15:32
@javiereguiluz javiereguiluz merged commit e92c704 into symfony:6.1 Apr 4, 2022
@javiereguiluz
Copy link
Member

Sofien thanks for providing these docs ... and congrats on your first Symfony Docs contribution 🎉

Nicolas, I reworded the explanation as you said while merging. Thanks!

@mytuny
Copy link
Contributor Author

mytuny commented Apr 4, 2022

Thanks @javiereguiluz!
Glad to support my favorite project 😀

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

Successfully merging this pull request may close these issues.

5 participants