Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dbaaa3d

Browse filesBrowse files
committed
don't use the twig_test_empty() function (it's deprecated since Twig 3.9)
1 parent 52839be commit dbaaa3d
Copy full SHA for dbaaa3d

File tree

3 files changed

+25
-4
lines changed
Filter options

3 files changed

+25
-4
lines changed

‎src/Symfony/Bridge/Twig/Extension/FormExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/FormExtension.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ public function getFieldChoices(FormView $view): iterable
158158
yield from $this->createFieldChoicesList($view->vars['choices'], $view->vars['choice_translation_domain']);
159159
}
160160

161+
/**
162+
* @internal
163+
*/
164+
public static function testEmpty($value)
165+
{
166+
if ($value instanceof \Countable) {
167+
return 0 === \count($value);
168+
}
169+
170+
if ($value instanceof \Traversable) {
171+
return !iterator_count($value);
172+
}
173+
174+
if (\is_object($value) && method_exists($value, '__toString')) {
175+
return '' === (string) $value;
176+
}
177+
178+
return '' === $value || false === $value || null === $value || [] === $value;
179+
}
180+
161181
private function createFieldChoicesList(iterable $choices, $translationDomain): iterable
162182
{
163183
foreach ($choices as $choice) {

‎src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Twig\Node;
1313

14+
use Symfony\Bridge\Twig\Extension\FormExtension;
1415
use Twig\Compiler;
1516
use Twig\Node\Expression\ArrayExpression;
1617
use Twig\Node\Expression\ConstantExpression;
@@ -50,7 +51,7 @@ public function compile(Compiler $compiler): void
5051
$labelIsExpression = false;
5152

5253
// Only insert the label into the array if it is not empty
53-
if (!twig_test_empty($label->getAttribute('value'))) {
54+
if (!FormExtension::testEmpty($label->getAttribute('value'))) {
5455
$originalVariables = $variables;
5556
$variables = new ArrayExpression([], $lineno);
5657
$labelKey = new ConstantExpression('label', $lineno);
@@ -97,7 +98,7 @@ public function compile(Compiler $compiler): void
9798

9899
// Check at runtime whether the label is empty.
99100
// If not, add it to the array at runtime.
100-
$compiler->raw('(twig_test_empty($_label_ = ');
101+
$compiler->raw('(Symfony\Bridge\Twig\Extension\FormExtension::testEmpty($_label_ = ');
101102
$compiler->subcompile($label);
102103
$compiler->raw(') ? [] : ["label" => $_label_])');
103104
}

‎src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNull()
226226
// https://github.com/symfony/symfony/issues/5029
227227
$this->assertEquals(
228228
sprintf(
229-
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
229+
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (Symfony\Bridge\Twig\Extension\FormExtension::testEmpty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
230230
$this->getVariableGetter('form')
231231
),
232232
trim($compiler->compile($node)->getSource())
@@ -263,7 +263,7 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
263263
// https://github.com/symfony/symfony/issues/5029
264264
$this->assertEquals(
265265
sprintf(
266-
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
266+
'$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (Symfony\Bridge\Twig\Extension\FormExtension::testEmpty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
267267
$this->getVariableGetter('form')
268268
),
269269
trim($compiler->compile($node)->getSource())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.