rector icon indicating copy to clipboard operation
rector copied to clipboard

Symfony polyfill not found

Open mpesari opened this issue 1 year ago • 6 comments

Bug Report

Subject Details
Rector version 1.1.1

mb_ucfirst() from symfony/polyfill-php84 is not found by rector.

I tried some ways to work around the issue, without success:

  • adding it to explicit autoload in rector config
  • using with the new withPhpPolyfill()
  • using withSets([LevelSetList::UP_TO_PHP_83]) instead of withPhpSets()

Minimal PHP Code Causing Issue

$ composer require symfony/polyfill-php84 v1.30.0

src/test.php:

<?php declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

# Test that the polyfill is correctly loaded

print mb_ucfirst('hello') . PHP_EOL;

rector.php:

<?php declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/src'])
    ->withPhpSets(php83: true);

What's happening?

$ php src/test.php
Hello
$ vendor/bin/rector

 [ERROR] Could not process "src/test.php" file, due to:
         "Call to undefined method Symfony\Polyfill\Mbstring\Mbstring::mb_ucfirst()". On line: 207

Expected Behaviour

I'm not sure which rule is in effect here? I guess nothing should happen...

mpesari avatar Jun 26 '24 10:06 mpesari

The error seems from phpstan:

➜  test-php84 vendor/bin/rector process src -vvv

                                                                                                                        
 [ERROR] Could not process "/Users/samsonasik/test-php84/src/test.php" file, due to:                                    
         "System error: "Call to undefined method Symfony\Polyfill\Mbstring\Mbstring::mb_ucfirst()"                     
                                                                                                                        
         Stack trace:                                                                                                   
         #0 phar://vendor/phpstan/phpstan/phpstan.phar/src/Type/Php/StrCaseFunctionsReturnTypeExtension.php(79):        
         mb_ucfirst('hello')

it seems some autoload is overlapped

samsonasik avatar Jun 28 '24 03:06 samsonasik

The error seems still happen even mb_ucfirst is not called:

Screenshot 2024-06-28 at 10 39 18

samsonasik avatar Jun 28 '24 03:06 samsonasik

Oh, I edit the wrong file, when mb_ucfirst not called, it working ok

➜  test-php84 vendor/bin/rector process src/test.php --dry-run
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

                                                                                                                        
 [OK] Rector is done!                                                                                                                                                                                                                      

samsonasik avatar Jun 28 '24 03:06 samsonasik

I guess the patch needs to be in phpstan-src itself, as it got error on phpstan.org

https://phpstan.org/r/4aa0a8d2-809b-472b-a406-6d085b4781aa

and stack trace shown on phpstan:

 [ERROR] Could not process "src/test.php" file, due to:                                                                 
         "System error: "Call to undefined method Symfony\Polyfill\Mbstring\Mbstring::mb_ucfirst()"                     
                                                                                                                        
         Stack trace:                                                                                                   
         #0 phar://vendor/phpstan/phpstan/phpstan.phar/src/Type/Php/StrCaseFunctionsReturnTypeExtension.php(79):        
         mb_ucfirst('hello')                                                                                            
         #1 phar://vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1638):                            
         PHPStan\Type\Php\StrCaseFunctionsReturnTypeExtension->getTypeFromFunctionCall(Object(PHPStan\Reflection\Php\Ph 
         pFunctionReflection), Object(PhpParser\Node\Expr\FuncCall), Object(PHPStan\Analyser\MutatingScope))

samsonasik avatar Jun 28 '24 05:06 samsonasik

Yeah, you need the symfony/polyfill-php84 package to be installed first, with it, phpstan does find the symbols, but rector doesn't

mpesari avatar Jun 28 '24 10:06 mpesari

Same errors, A quick fix that worked for me

-"symfony/polyfill-mbstring": "1.30",
+"symfony/polyfill-mbstring": "1.29",

DylanDelobel avatar Jun 28 '24 16:06 DylanDelobel

Fixed by upgrading phpstan from 1.11.5 to 1.11.6 ... kind of.

Now phpstan doesn't report error if polyfill is actually missing, and undefined function mb_ucfirst() is not found.

mpesari avatar Jul 16 '24 12:07 mpesari