TomasVotruba / bladestan

PHPStan analysis for Blade templates
https://tomasvotruba.com/blog/introducing-bladestan-phpstan-analysis-of-blade-templates/
MIT License
290 stars 14 forks source link

Fix $loop in nested @foreach #53

Closed AJenbo closed 1 year ago

AJenbo commented 1 year ago

Previously the code would generate an unset() for $loop each time a loop ended. This however means that $loop was not available after a nested loop, which is not how Blade works.

foreach ($items as $item) {
    $loop = new Loop();
    foreach ($item->names as $name) {
        $loop = new Loop();
        echo $name;
        unset($loop); // <-- outloop nolonger has a context
    }
    echo $loop->odd ? 'odd-button.png' : 'even-button.png';
    unset($loop);
}

PR:

foreach ($items as $item) {
    $loop = new Loop();
    foreach ($item->names as $name) {
        echo $name;
    }
    echo $loop->odd ? 'odd-button.png' : 'even-button.png';
    unset($loop);
}

Additionally I replaced the hack I did for creating the loop (using a comment) with the proper way of doing so now that I know how PhpParser works.

This indeed did solve the last of the errors I was getting for the two projects I work on for a living.

TomasVotruba commented 1 year ago

Let's shipt it, tahnk you :+1: