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

Check data types in default CoreExtension #3609

Closed
shyim opened this issue Dec 23, 2021 · 2 comments
Closed

Check data types in default CoreExtension #3609

shyim opened this issue Dec 23, 2021 · 2 comments

Comments

@shyim
Copy link

shyim commented Dec 23, 2021

I am currently upgrading a project for PHP 8.1. I get lot of mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in vendor/twig/twig/src/Extension/CoreExtension.php on line 1098

It's pretty hard to find which template/variable is triggern this. Maybe it makes sense to check the type in the CoreExtension and throw an error?

@spackmat
Copy link

spackmat commented Dec 29, 2021

I have that with the trim filter.

Would it be against a best practice to allow and handle null for the several string filters? In PHP 8.1 I get a deprecation notice for trim:

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated

That is correct, as PHP's trim() only wants to work on strings, but from my point of view the corresponding Twig filter should simply return null or an empty string on a null input to avoid verbose workarounds for nullable strings.

I can provide a PR implementing this small change, maybe also for the other affected string related filters as mentioned in the first post. Returning an empty string in that cases would reflect the current behavior, but in an explicit way: trim(null) returns an empty string and raises the deprecation notice mentioned above.

Another and maybe better solution would be a ?| construct just like PHPs ?-> null safe operator to handle those type issues. But I don't know how complicated this would be to implement. Maybe that is another issue.

Edit: That nullsafe operator stuff already is another issue 😉: #3260

@fabpot
Copy link
Contributor

fabpot commented Jan 2, 2022

See #3617

@fabpot fabpot closed this as completed Jan 2, 2022
fabpot added a commit that referenced this issue Jan 2, 2022
This PR was merged into the 2.x branch.

Discussion
----------

Allow null when Twig expects a string

To ease the transition to PHP 8.1, Twig now explicitly accepts `null` in addition to strings in filters that expect strings.

Closes #3615
Closes #3557
Closes #3610
Closes #3609

Commits
-------

92bc110 Allow null when Twig expects a string
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