laravel / tinker

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

Loop only returns the last value if the curly brackets start on a new line #161

Closed victorelec14 closed 1 year ago

victorelec14 commented 1 year ago

Description:

Tinker displays only the last value of a loop if the curly backet starts in new line

Steps To Reproduce:

$data = [
    "1111",
    "2222",
    "3333",
    "4444",
];
foreach ($data as $pos)
{
    echo $pos . "\n";
}

Result:

4444

Now with the curly backet in the same line of the foreach

$data = [
    "1111",
    "2222",
    "3333",
    "4444",
];
foreach ($data as $pos){
    echo $pos . "\n";
}`

Result :

1111
2222
3333
4444

Thanks

driesvints commented 1 year ago

I don't think this is a tinker issue but rather a psysh one. Please try their repo or a support channel, thanks.

bobthecow commented 1 year ago

This is expected! (Though I can totally see why you wouldn't expect it).

As a REPL, PsySH evals your code as soon as it can. It doesn't wait for input to be "done". It also doesn't require semicolons if the input is otherwise complete. So when you type:

foreach ($data as $pos)
{
    echo $pos . "\n";
}

… you're really saying:

foreach ($data as $pos);
{
    echo $pos . "\n";
}

The difference is subtle :)

The reason it outputs the final $pos is that the loop iteration variable leaks out of the loop scope and the final value is available after the loop completes. To top it all off, your echo is surrounded by brackets but you can just … put brackets wherever you want in PHP. In this case they do nothing interesting at all. So your whole thing is equivalent to:

$data = [
    "1111",
    "2222",
    "3333",
    "4444",
];
$pos = end($data);
echo $pos . "\n";

To avoid this, you can:

  1. Change your style, and always put opening brackets on the same line while using the REPL. This is my personal preference, as it avoids other, similar sorts of issues.
  2. Force line continuation by adding a \ at the end of the line (much like in Bash or other shells, which also execute as soon as they have valid input).
  3. Or disable automatic semicolon insertion, via the requireSemicolons config option. This means PsySH will never execute anything until you've added all the semicolons, which can be a bit annoying, but it also prevents your issue here completely.
bobthecow commented 1 year ago

Note that this behavior only applies to line-by-line input. Inside another scope that already keeps the input buffer open (e.g. inside a function or class definition, or a namespace block) it will act like you expected, because treating the foreach on its own line as a complete statement and executing it won't work inside another block.

It also doesn't apply to pasted input, input from STDIN, or input from the edit command.

driesvints commented 1 year ago

@bobthecow thanks a lot for that thorough explanation 👍

bobthecow commented 1 year ago

Oooh! I just thought of a fourth way!

If you only temporarily want to get the requireSemicolons type experience, you can open a block by starting with a { on its own line. Then all input will be buffered until you get to the closing }.

Screenshot 2023-03-23 at 8 52 02 AM

This would work great for method chaining interfaces.