-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
Symfony function polyfills get put into the wrong namespace #657
Comments
Great summary of this bug. I'm also having the exact same issue. This path does not have a namespace originally, and PHP-Scoper adds one:
Original: if (!function_exists('str_starts_with')) {
function str_starts_with(?string $haystack, ?string $needle): bool { return p\Php80::str_starts_with($haystack ?? '', $needle ?? ''); }
} In Phar: namespace _HumbugBox416619033617;
if (!\function_exists('str_ends_with')) {
function str_ends_with(?string $haystack, ?string $needle) : bool
{
return p\Php80::str_ends_with($haystack ?? '', $needle ?? '');
}
} This PR should have solved it, but for some reason it's still prefixing it for me: https://github.com/box-project/box/pull/579/files It seems this applies only to the Box.phar, not the Phars that it generates. Box version (Phar): |
There are a lot of libraries with this issue out there. The way that Box itself handles is by skip scoping the polyfills. Essentially, use this
It seems to me that this opens a breach in the encapsulation of the Phar, but it's the only solution that I could find. |
To expand on @Luc45 good explanation, there is this doc entry https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md#polyfills. Of course could be missing in which case contributions are welcomed. I'm also aware this is a PITA, no one is happy about it, but maybe I found a way to get rid of that in the next version. Also thanks for providing a reproducer still @allejo :) If there is any other problem please feel free to open another issue |
I'm removing my code example in favor of the snippet that @theofidry shared, which is more complete. |
Bug report
feature/box-cleanup
When my
box.json.dist
uses the"KevinGH\\Box\\Compactor\\PhpScoper"
compactor, any function that Symfony polyfills in the global namespace is written into the wrong namespace. Kinda hard to explain, so let me try my best.In my project's
vendor
directory, theInputOption.php
file makes use ofstr_starts_with
, which exists in PHP 8.0+ but Symfony makes use ofsymfony/polyfills-php80
to polyfill this function, allowing it to work as intended.This works because
symfony/polyfills-php80
has a bootstrap file that has this logic in the global namespace:After running Box with PhpScoper, the
InputOption.php
file ends up with code that looks like,And
symfony/polyfill-php80
's bootstrap file has been updated to,This leads to the
str_starts_with()
not being found sinceInputOption.php
is referencing the one in the global namespace and the polyfill is being added to the scoped namespace.box.json.dist
Output
The text was updated successfully, but these errors were encountered: