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

Concerns about passing null to some polyfill arguments #506

Open
pilif opened this issue Oct 22, 2024 · 5 comments
Open

Concerns about passing null to some polyfill arguments #506

pilif opened this issue Oct 22, 2024 · 5 comments

Comments

@pilif
Copy link

pilif commented Oct 22, 2024

For years our code-base (like presumably many others) had an existing mb_trim() polyfill of our own and as it pre-dates even php 7, it had a slight quirk in that it accepted a null as the first argument.

PHP 8.4 will provide its own mb_trim() implementation which has its first argument typed as string, but which also accepts a null argument, albeit with a deprecation warning.

The symfony polyfill on the other hand does not accept a null argument and thus, it being an userland function will throw a TypeError instead.

In the case of our code-base, this caused an issue where a completely non-related composer update now turned code which run perfectly well (and would have changed to raising a deprecation notice in 8.4) into a TypeError.

I know that this is probably a slight edge-case, but I'm still coming here to raise the issue because it might affect other users.

IMHO, to be a proper polyfill for PHP 8.4 mb_trim, your implementation should accept nulls too and raise an E_USER_DEPRECATED error if a null is passed.

And the same is probably true for many other functions as well.

@Ayesh
Copy link
Contributor

Ayesh commented Oct 22, 2024

I'd rather not deviate the polyfill from the actual function signature TBH.

Would it not be easier to add a (string) cast to all of the callsites in the application?

- mb_trim($value);
+ mb_trim((string) $value);

@nicolas-grekas
Copy link
Member

Duplicate of #499, which already has #501, isn't it?

@pilif
Copy link
Author

pilif commented Oct 22, 2024

yes. if course. That's (more or less) what I have done.

But still. It feels incorrect for a polyfill to not actually polyfill the function it's supposed to polyfill but to be more opinionated about its arguments.

Think from a user's perspective. Even with all the semver rules in place, updating from 1.30 to 1.31 (or in my case updating a patch release of a dependency) caused a TypeError to be thrown where there previously wasn't.

@pilif
Copy link
Author

pilif commented Oct 22, 2024

I'm not sure this is a duplicate of #499.

This is about throwing a TypeError where the polyfilled original function is only throwing a deprecation notice.

@stof
Copy link
Member

stof commented Oct 23, 2024

Accepting null in the polyfill should indeed be the right behavior (as done in other polyfills), to match the behavior of the native function. This is what we do for most polyfilled functions, but it looks like this has been missed for the more recent additions to the mbstring polyfill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants