laravel / prompts

Beautiful and user-friendly forms for your command-line PHP applications.
https://laravel.com/docs/prompts
MIT License
532 stars 94 forks source link

fix(helper): ensure helpers can't be redeclared #146

Closed NickSdot closed 6 months ago

NickSdot commented 6 months ago

To make packages like symfony/var-dumper available globally you prepend the global Composer autoload.php like so:

// php.ini

auto_prepend_file = ${HOME}/.composer/vendor/autoload.php
image

From: https://symfony.com/doc/current/components/var_dumper.html

To reproduce, at least these must be installed.

// composer

laravel/installer (global)
symfony/var-dumper (global)
laravel/framework (project)
phpstan/phpstan (project)

Then run PhpStan in a project.

// cli

$ vendor/bin/phpstan analyse Common

Result.

Fatal error: Cannot redeclare Laravel\Prompts\text() (previously declared in ~/.composer/vendor/laravel/prompts/src/helpers.php:11) in XXX/vendor/laravel/prompts/src/helpers.php on line 13

Call Stack:
0.0325    1260304   1. {main}() XXX/vendor/bin/phpstan:0
0.0325    1261384   2. include('XXX/vendor/phpstan/phpstan/phpstan') XXX/vendor/bin/phpstan:119
0.0754    4122912   3. require('phar://XXX/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan') XXX/vendor/phpstan/phpstan/phpstan:8
0.0754    4123296   4. _PHPStan_7961f7ae1\{closure:phar://XXX/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan:13-125}() phar://XXX/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan:125
0.1766   36372104   5. require_once('XXX/vendor/autoload.php') phar://XXX/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan:66
0.1766   36378600   6. ComposerAutoloaderInit9c491b8531eec05ba41a11d9276a5749::getLoader() XXX/vendor/autoload.php:25
0.1838   39023408   7. {closure:XXX/vendor/composer/autoload_real.php:37-43}($fileIdentifier = '47e1160838b5e5a10346ac4084b58c23', $file = 'XXX/vendor/composer/../laravel/prompts/src/helpers.php') XXX/vendor/composer/autoload_real.php:45

Notes


Amazing work here btw, Jess. Thank you! 🫡❤️

jessarcher commented 6 months ago

I think it's caused by the Laravel installer being installed globally. It depends on Prompts, which effectively makes it installed globally as well. With the auto_prepend_file setting in place, there are effectively two copies of Prompts installed.

Prompts didn't previously have the function_exists guard like other Laravel helpers because they are namespaced functions rather than global, but I don't see any issues adding it.