8000 bug #52964 [ExpressionLanguage] Fix null coalescing propagation (fanc… · symfony/symfony@1c87e55 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c87e55

Browse files
committed
bug #52964 [ExpressionLanguage] Fix null coalescing propagation (fancyweb)
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
2 parents 96421d3 + 6c5ca23 commit 1c87e55

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/ExpressionLanguage/Node/NullCoalesceNode.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function compile(Compiler $compiler): void
3939
public function evaluate(array $functions, array $values): mixed
4040
{
4141
if ($this->nodes['expr1'] instanceof GetAttrNode) {
42-
$this->nodes['expr1']->attributes['is_null_coalesce'] = true;
42+
$this->addNullCoalesceAttributeToGetAttrNodes($this->nodes['expr1']);
4343
}
4444

4545
return $this->nodes['expr1']->evaluate($functions, $values) ?? $this->nodes['expr2']->evaluate($functions, $values);
@@ -49,4 +49,17 @@ public function toArray(): array
4949
{
5050
return ['(', $this->nodes['expr1'], ') ?? (', $this->nodes['expr2'], ')'];
5151
}
52+
53+
private function addNullCoalesceAttributeToGetAttrNodes(Node $node): void
54+
{
55+
if (!$node instanceof GetAttrNode) {
56+
return;
57+
}
58+
59+
$node->attributes['is_null_coalesce'] = true;
60+
61+
foreach ($node->nodes as $node) {
62+
$this->addNullCoalesceAttributeToGetAttrNodes($node);
63+
}
64+
}
5265
}

src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ public function bar()
424424
yield ['foo["bar"]["baz"] ?? "default"', ['bar' => null]];
425425
yield ['foo["bar"].baz ?? "default"', ['bar' => null]];
426426
yield ['foo.bar().baz ?? "default"', $foo];
427+
yield ['foo.bar.baz.bam ?? "default"', (object) ['bar' => null]];
428+
yield ['foo?.bar?.baz?.qux ?? "default"', (object) ['bar' => null]];
429+
yield ['foo[123][456][789] ?? "default"', [123 => []]];
427430
}
428431

429432
/**

0 commit comments

Comments
 (0)
0