Skip to content

[ExpressionLanguage] Fix coalescing operator deep nested properties #47446

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

Closed
wants to merge 1 commit into from

Conversation

mytuny
Copy link
Contributor

@mytuny mytuny commented Sep 1, 2022

Q A
Branch? 6.2
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #47192
License MIT

This is a fix for issue #47192 which consist of dealing with nested object's properties evalution problem occuring for levels 3 and above.

@carsonbot
Copy link

Hey!

I think @mytuny has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@@ -69,6 +69,10 @@ public function evaluate(array $functions, array $values)
{
switch ($this->attributes['type']) {
case self::PROPERTY_CALL:
if ($this->nodes['node'] instanceof GetAttrNode) {
$this->nodes['node']->attributes['is_null_coalesce'] = $this->attributes['is_null_coalesce'];
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be $this->nodes['node']->attributes['is_null_coalesce'] = $this->nodes['node']->attributes['is_null_coalesce'] || $this->attributes['is_null_coalesce']; ?

Copy link
Contributor Author

@mytuny mytuny Sep 12, 2022

Choose a reason for hiding this comment

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

shouldn't this be $this->nodes['node']->attributes['is_null_coalesce'] = $this->nodes['node']->attributes['is_null_coalesce'] || $this->attributes['is_null_coalesce']; ?

@stof This will introduce a regression when we have a mixed chaining between different types of nodes (not only GetAttrNode).

Choose a reason for hiding this comment

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

Suggested change
$this->nodes['node']->attributes['is_null_coalesce'] = $this->attributes['is_null_coalesce'];
$this->nodes['node']->attributes['is_null_coalesce'] = $this->attributes['is_null_coalesce'] ?? false;

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@fancyweb fancyweb modified the milestones: 6.4, 6.3 Nov 16, 2023
@fancyweb
Copy link
Contributor

I looked at this a bit.
For the record, there's the same problem with an array: a[123][456] ?? "na".
The proposed patch doesn't look right to me. Shouldn't we just set the is_null_coalesce attribute to true recursively in NullCoalesceNode instead?

diff --git a/src/Symfony/Component/ExpressionLanguage/Node/NullCoalesceNode.php b/src/Symfony/Component/ExpressionLanguage/Node/NullCoalesceNode.php
index 1cc5eb058e..c9c3038bb7 100644
--- a/src/Symfony/Component/ExpressionLanguage/Node/NullCoalesceNode.php
+++ b/src/Symfony/Component/ExpressionLanguage/Node/NullCoalesceNode.php
@@ -38,9 +38,7 @@ class NullCoalesceNode extends Node
 
     public function evaluate(array $functions, array $values): mixed
     {
-        if ($this->nodes['expr1'] instanceof GetAttrNode) {
-            $this->nodes['expr1']->attributes['is_null_coalesce'] = true;
-        }
+        $this->addNullCoalesceAttributeToGetAttrNodes($this->nodes['expr1']);
 
         return $this->nodes['expr1']->evaluate($functions, $values) ?? $this->nodes['expr2']->evaluate($functions, $values);
     }
@@ -49,4 +47,17 @@ class NullCoalesceNode extends Node
     {
         return ['(', $this->nodes['expr1'], ') ?? (', $this->nodes['expr2'], ')'];
     }
+
+    private function addNullCoalesceAttributeToGetAttrNodes(Node $node): void
+    {
+        if (!$node instanceof GetAttrNode) {
+            return;
+        }
+
+        $node->attributes['is_null_coalesce'] = true;
+
+        foreach ($node->nodes as $node) {
+            $this->addNullCoalesceAttributeToGetAttrNodes($node);
+        }
+    }
 }

@shadowhand
Copy link

@fancyweb seems like it might work. Have you checked it against the test case added here, as well as the one in #49230?

@fabpot
Copy link
Member

fabpot commented Dec 9, 2023

Closing in favor of #52964

@fabpot fabpot closed this Dec 9, 2023
fabpot added a commit that referenced this pull request Dec 9, 2023
…yweb)

This PR was merged into the 6.3 branch.

Discussion
----------

[ExpressionLanguage] Fix null coalescing propagation

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | #47192
| License       | MIT

This PR replaces #47446 since the author didn't reply.

I think my patch suggestion is better.

Commits
-------

6c5ca23 [ExpressionLanguage] Fix null coalescing propagation
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.

Null safe operator in EL does not seem to work as expected
8 participants