ComposerRequireChecker icon indicating copy to clipboard operation
ComposerRequireChecker copied to clipboard

unknown symbol mb_trim

Open 8ctopus opened this issue 6 months ago • 10 comments

Thank you for a great tool, noticed this minor issue while using it.

mb_trim is missing

8ctopus avatar Jun 16 '25 06:06 8ctopus

Sounds about right: would be detected if you were on PHP 8.4

Ocramius avatar Jun 16 '25 07:06 Ocramius

Actually you're right, I assumed incorrectly that mb_trim was already present in php 8.3. But it still gets interesting, here's the story:

I was running the check on php 8.3 and incorrectly assumed that the composer require checker was missing this new function since the code using mb_trim works just fine. By digging a bit further, I found out that I have symfony/polyfill-php84 installed as a soft dependency by another package.

Could composer require checker detect that mb_trim is php 8.4 only and therefore it uses a polyfill as a soft dependency?

8ctopus avatar Jun 16 '25 15:06 8ctopus

Could composer require checker detect that mb_trim is php 8.4 only and therefore it uses a polyfill as a soft dependency?

PHP functions are discovered via reflection, and not kept in a "known list" somewhere (maintaining it would also be a pain).

and therefore it uses a polyfill as a soft dependency?

Adding the polyfill is something that libraries should never do: polyfills are only for end-user applications.

It's also likely that the tool can't inspect the polyfill, since the polyfill wraps all declared functions in conditionals.

Ocramius avatar Jun 16 '25 15:06 Ocramius

@Ocramius Thank you for all the explanations, much appreciated. I found the package that requires the polyfill, it's one I made xD. I will fix that. I guess there's nothing to do then from composer require checker's side, if affirmative, please close the issue.

8ctopus avatar Jun 17 '25 16:06 8ctopus

I don't agree with @Ocramius on this: If a library wants to use mb_trim, it must require PHP 8.4, making it impossible to install if the end-user application runs on PHP 8.3 or lower. If the library wants to support earlier PHP versions by utilizing a polyfill, it might do so. The only alternative is that the library cannot use mb_trim.

Just as a real-world example: doctrine/collections requires the same polyfill for their array-functions, and Symfony itself also uses polyfill-packages (just not the one for PHP 8.4) in their components.

Still it is true that polyfills cannot be easily detected by this tool, so you have to weigh the pros and cons.

SvenRtbg avatar Jun 17 '25 17:06 SvenRtbg

If a library wants to use mb_trim, it must require PHP 8.4, making it impossible to install if the end-user application runs on PHP 8.3 or lower.

And that is fine, BTW: upgrading is everyone's job, and staying with older library versions is also OK (as long as there are no security issues).

I don't expect this tool to be able to detect polyfills either, BTW: it's hacks upon hacks, whereas it is easier for libraries to provide their own namespaced copy of a function, avoiding shadowing of core functions, which is messy at best.

From an application PoV, this tool correctly tells you that you need PHP 8.4, BTW: the error message isn't very nice though.

Ocramius avatar Jun 17 '25 17:06 Ocramius

@Ocramius How do you feel about adding detection that the symfony polyfills are being used as soft dependencies to composer require checker ?

8ctopus avatar Jun 18 '25 04:06 8ctopus

@8ctopus this package was especially designed to prevent the concept of "soft dependencies": it is the main reason why it was built, since soft dependencies have the tendency of being absent when it most matters.

Ocramius avatar Jun 18 '25 15:06 Ocramius

@Ocramius so polyfill use detection is aligned with the objective

8ctopus avatar Jun 19 '25 13:06 8ctopus

Yes 👍

Ocramius avatar Jun 19 '25 14:06 Ocramius