Skip to content

Commit 48819d3

Browse files
committed
don't use the twig_test_empty() function (it's deprecated since Twig 3.9)
1 parent 52839be commit 48819d3

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

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

Lines 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

Lines 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('(FormExtension::testEmpty($_label_ = ');
101102
$compiler->subcompile($label);
102103
$compiler->raw(') ? [] : ["label" => $_label_])');
103104
}

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

Lines 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\', (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"] + (FormExtension::testEmpty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))',
267267
$this->getVariableGetter('form')
268268
),
269269
trim($compiler->compile($node)->getSource())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy