phpsymfonycomposer-phppolyfills

Composer installs unexpected polyfills. How to avoid installing them if they are unnecessary?


I'm using composer, and I have just updated guzzlehttp/guzzle. I'm astonished for the packages that are installed:

>composer update  guzzlehttp/guzzle
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 1 update, 0 removals
  - Installing symfony/polyfill-php72 (v1.17.0): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.17.0): Downloading (100%)
  - Installing symfony/polyfill-intl-idn (v1.17.0): Downloading (100%)
  - Updating guzzlehttp/guzzle (6.5.2 => 6.5.4): Downloading (100%)

I'm using php 7.4 with mbstring installed, and there is no reason these polyfills are installed. Composer is aware of the PHP version I'm using:

>composer show --platform
(...)
ext-mbstring        7.4.2    The mbstring PHP extension
(...)
php                 7.4.3    The PHP interpreter

I suspec that I have some misconfiguration somewhere, it doesn't make sense to me that these polyfills are installed.


Solution

  • The polyfills are installed in case the package is installed in a server that does not match the requirements. This way maximum portability and compatibility is achieved.

    The way to avoid these packages from being installed if you know your project already depends on a specific platform version and or extensions, and thus you can are certain they are always going to be available on every deployment is to declare them on the replace section:

    "replace": {  
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-mbstring": "*"
    }
    

    This will prevent these packages being installed at all. But this will not make sure these capabilities are present when installing the application. For completeness sake, you should add the corresponding entries in the require section:

    "require": {  
       "php": "^7.2",  
       "ext-mbstring": "*"
    }
    

    This is glossed over in the symfony/polyfill readme, but it only mentions polyfills for PHP versions, although the same logic applies for any other polyfill your project is certain to have enough capabilities as to not require, and that you could avoid installing altogether.

    Still, if you do not do this, the performance hit is minimal, and should not be a cause for concern.