Skip to content

[TwigBridge] don't use deprecated and internal Twig functions #52990

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
Dec 15, 2023

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Dec 11, 2023

Q A
Branch? 5.4
Bug fix? no
New feature? no
Deprecations? no
Issues Fix #52987
License MIT

twig_test_empty() and twig_escape_filter() are deprecated since Twig 3.9

@@ -158,6 +158,26 @@ public function getFieldChoices(FormView $view): iterable
yield from $this->createFieldChoicesList($view->vars['choices'], $view->vars['choice_translation_domain']);
}

/**
* @internal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can put a comment that this function has been copied from twig_test_empty, to help track changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we use CoreExtension::testEmpty() if it exists?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CoreExtension::testEmpty() is internal, its signature could change, or it could be moved or renamed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we intend to migrate Twig into a Symfony component, I think it's fine to access @internal functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in Symfony components, it is not fine to use @internal APIs of a different component, as components relies on semver ranges in their inter-component requirements.

@OskarStark OskarStark changed the title [TwigBridge] don't use the twig_test_empty() function (it's deprecated since Twig 3.9) [TwigBridge] don't use the twig_test_empty() function (it's deprecated since Twig 3.9) Dec 11, 2023
@OskarStark
Copy link
Contributor

OskarStark commented Dec 11, 2023

The same seems to be needed for twig_escape_filter

11x: Since twig/twig 3.9.0: Using the internal "twig_escape_filter" function is deprecated.
11x in WebProfilerExtensionTest::testDumpHeaderIsDisplayed from Symfony\Bundle\WebProfilerBundle\Tests\Twig

@OskarStark OskarStark changed the title [TwigBridge] don't use the twig_test_empty() function (it's deprecated since Twig 3.9) [TwigBridge] Don't use the twig_test_empty() function anymore Dec 11, 2023
@xabbuh xabbuh changed the title [TwigBridge] Don't use the twig_test_empty() function anymore [TwigBridge] don't use deprecated and internal Twig functions Dec 11, 2023
/**
* @see \Twig\Extension\CoreExtension::testEmpty()
*/
private static function isEmpty($value): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we rely on the internal twig function for the compiled template, we can do the same for the compilation step and avoid duplicating the code.

But I'm still not sure this is a sane solution to rely on the internal function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, let's use the Twig method instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these methods shouldn't be @internal then?

/**
* @see \Twig\Extension\CoreExtension::testEmpty()
*/
private static function isEmpty($value): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, let's use the Twig method instead.

// Only insert the label into the array if it is not empty
if (!twig_test_empty($label->getAttribute('value'))) {
if (!$isEmpty) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use $label !== null && $label !== '' as condition instead ? We don't need to handle cases about checking emptiness for other kind of values here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@fabpot
Copy link
Member

fabpot commented Dec 15, 2023

Thank you @xabbuh.

@fabpot fabpot merged commit 85b3a05 into symfony:5.4 Dec 15, 2023
@xabbuh xabbuh deleted the issue-52987 branch December 15, 2023 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants