-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
javiereguiluz
merged 1 commit into
symfony:6.1
from
mytuny:feature_expressionlanguage_nullsafe
Apr 4, 2022
Merged
[ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods #16630
javiereguiluz
merged 1 commit into
symfony:6.1
from
mytuny:feature_expressionlanguage_nullsafe
Apr 4, 2022
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Actually, the nullsafe operator only guards against null. It does not guard against undefined properties/keys. |
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
…ject's properties and methods.
cbca2b4
to
23bf15d
Compare
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! |
Thanks @javiereguiluz! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[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 ofNullsafe operator
under the paragraphWorking with Objects
.The actual work related to this Doc PR available as Symfony PR #45795.