laravel / tinker

Powerful REPL for the Laravel framework.
https://laravel.com/docs/artisan#tinker
MIT License
7.32k stars 130 forks source link

Closures not working when using the --execute argument. #148

Closed dmason30 closed 2 years ago

dmason30 commented 2 years ago

Steps To Reproduce:

Running any one of these commands:

php artisan tinker --execute="optional('foo', fn($v) => dump($v));"
php artisan tinker --execute="transform('foo', fn($v) => dump($v));"
php artisan tinker --execute="tap('foo', fn($v) => dump($v));"
php artisan tinker --execute="with('foo', fn($v) => dump($v));"

php artisan tinker --execute="optional('foo', function ($v) {  dump($v); });"
php artisan tinker --execute="transform('foo', function ($v) {  dump($v); });"
php artisan tinker --execute="tap('foo', function ($v) {  dump($v); });"
php artisan tinker --execute="with('foo', function ($v) {  dump($v); });"

Produces this error

TypeError: Too few arguments to function dump(), 0 passed in /Users/danmason/PhpstormProjects/xxxeval()'d code on line 2 and exactly 1 expected

Running the same functions in PsySh works as expected

$ php artisan tinker
Psy Shell v0.11.2 (PHP 8.1.4 — cli) by Justin Hileman
>>> optional('foo', fn($v) => dump($v));
"foo"
=> "foo"
>>> transform('foo', fn($v) => dump($v));
"foo"
=> "foo"
>>> tap('foo', fn($v) => dump($v));
"foo"
=> "foo"
>>> with('foo', fn($v) => dump($v))
"foo"
=> "foo"
driesvints commented 2 years ago

You need to escape the $ sign when running this in the CLI:

$ php artisan tinker --execute="optional('foo', fn(\$v) => dump(\$v));" 
"foo"