Skip to content
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

Make function existence deterministic in Twig AppExtension #3562

Closed
ThomasLandauer opened this issue Sep 17, 2021 · 3 comments
Closed

Make function existence deterministic in Twig AppExtension #3562

ThomasLandauer opened this issue Sep 17, 2021 · 3 comments

Comments

@ThomasLandauer
Copy link
Contributor

'm just forwarding what @ondrejmirtes told me at phpstan/phpstan#5640 (comment):

Calling twig_date_format_filter() in App\Twig\AppExtension sometimes leads to phpstan reporting

Function twig_date_format_filter not found

... and sometimes it doesn't ;-)

@ondrejmirtes at symfony/symfony#43067 (comment):

This can usually be solved by separating functions into their own files and putting those files into Composer's autoload.files section.

@stof
Copy link
Member

stof commented Dec 14, 2021

I don't think twig_date_format_filter is meant to be called directly in your own code.

In the past, functions implementing the built-in filters have been changed between Twig versions (for instance adding the environment as first argument and using needs_environment in their config).
Twig provides BC for the signature of the Twig functions, but not for the signature of the PHP callable used to implement them.

@fabpot should we tag all those functions as @internal ?

@ThomasLandauer
Copy link
Contributor Author

Well, I'm only using it to "fix" the date filter for null values:

public function dateFilter(Environment $environment, $date, string $format = 'F j, Y H:i:m'): string
{
    if (null === $date) {
        return '';
    }
    return twig_date_format_filter($environment, $date, $format);
}

Is there a better to do this (other than checking for is empty on every usage)?

@fabpot
Copy link
Contributor

fabpot commented Dec 15, 2023

Fixed in Twig 4 where we don't have these functions anymore and their replacements are all internal methods.

@fabpot fabpot closed this as completed Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants