Description
Symfony version(s) affected
6.2-dev
Description
I've recently upgrade to expression-language 6.1 (and consequently php 8.1) to use the null safe operator, having confused it with the null coalescing operator.
In that version, expressions like content?.preview?.image
compile, but fail to evaluate as soon as one of those properties is not defined. Expressions like xx ?? yy
fail to compile as expected.
Today, I upgraded to expression-language 6.2 (dev channel) and the expressions with null coalescing operator started compiling fine, but it still fails to evaluate properly:
│ Rendering 10 (draft) stories...
│ ├ /allgemein/
│ │ 0. Failed to evaluate expression "storyItem.content?.preview_image?.filename ?? null" of attribute "v-if".
│ │ Reason: Undefined property: stdClass::$preview_image
│ │ Element: <img .......
I noticed this seems to happen from the 2nd undefined property level onward.
How to reproduce
composer require symfony/expression-language:^6.2@dev
require_once __DIR__ . '/vendor/autoload.php';
$el = new \Symfony\Component\ExpressionLanguage\ExpressionLanguage();
var_dump($el->evaluate('o.a.b.c ?? "na"', ['o' => (object)['a' => (object)[]]]));
results in:
PHP Warning: Undefined property: stdClass::$b in .../vendor/symfony/expression-language/Node/GetAttrNode.php on line 92
Warning: Undefined property: stdClass::$b in .../vendor/symfony/expression-language/Node/GetAttrNode.php on line 92
string(2) "na"
By comparison, this is how it looks like in plain PHP (and the expected result):
https://3v4l.org/qpWWe
Possible Solution
No response
Additional Context
From my understanding, the feature has been implemented here: #46142
A bit of a side note: will this feature be added to 6.1 actually? 'Coz the PR adds an entry in the changelog under 6.1 heading.