Skip to content

[ExpressionLanguage] Support non-existent names when followed by null coalescing #54757

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
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add support for null-coalescing unknown variables

7.1
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ExpressionLanguage\Node;

use Symfony\Component\ExpressionLanguage\Compiler;

/**
* @author Adam Kiss <hello@adamkiss.com>
*
* @internal
*/
class NullCoalescedNameNode extends Node
{
public function __construct(string $name)
{
parent::__construct(
[],
['name' => $name]
);
}

public function compile(Compiler $compiler): void
{
$compiler->raw('$'.$this->attributes['name'].' ?? null');
}

public function evaluate(array $functions, array $values): null
{
return null;
}

public function toArray(): array
{
return [$this->attributes['name'].' ?? null'];
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ public function parsePrimaryExpression(): Node\Node
} else {
if (!($this->flags & self::IGNORE_UNKNOWN_VARIABLES)) {
if (!\in_array($token->value, $this->names, true)) {
if ($this->stream->current->test(Token::PUNCTUATION_TYPE, '??')) {
return new Node\NullCoalescedNameNode($token->value);
}

throw new SyntaxError(sprintf('Variable "%s" is not valid.', $token->value), $token->cursor, $this->stream->getExpression(), $token->value, $this->names);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ public function bar()
}
};

yield ['bar ?? "default"', null];
yield ['foo.bar ?? "default"', null];
yield ['foo.bar.baz ?? "default"', (object) ['bar' => null]];
yield ['foo.bar ?? foo.baz ?? "default"', null];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\NullCoalescedNameNode;

class NullCoalescedNameNodeTest extends AbstractNodeTestCase
{
public static function getEvaluateData(): array
{
return [
[null, new NullCoalescedNameNode('foo'), []],
];
}

public static function getCompileData(): array
{
return [
['$foo ?? null', new NullCoalescedNameNode('foo')],
];
}

public static function getDumpData(): array
{
return [
['foo ?? null', new NullCoalescedNameNode('foo')],
];
}
}
Loading