TomasVotruba / bladestan

PHPStan analysis for Blade templates

Home Page:https://tomasvotruba.com/blog/introducing-bladestan-phpstan-analysis-of-blade-templates/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Product of @foreach not vissible to @include

AJenbo opened this issue · comments

                @foreach($products as $product)
                    @include('product-list._row', ['product => $product])
                @endforeach

Undefined variable: $product (template: product-list.blade.php, line: 15)

P.s. is there some way where I can inspect the generated PHP? I'm having a few strange cases that I'm having a hard time groging.

Hi, please send failing test case to the phpstan rule. We'll check it 👍

This may well be a duplicate of #26, if cut down to the minimum and no other extension I get Syntax error, unexpected EOF on line 6. So at least for now I'll not add a test case for it.

Hm turns out this is a separate issue. There is also an issue with $loop not being known inside loops.

I'm really missing a way to have phpstan print out the generated/analyzed PHP code. Is there anyway to do so?

By hacking up phpblade a bit I can see that the compiled-php is:

<?php
/** file: foreach-include.blade.php, line: 1 */ $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop();
/** file: foreach-include.blade.php, line: 2 */ echo $__env->make('bar', ['bar' => $product], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render();
/** file: foreach-include.blade.php, line: 3 */ endforeach; $__env->popLoop(); $loop = $__env->getLastLoop();

But after transforming the includes we end up with this where it's using $products insted of $product

<?php
/** file: foreach-include.blade.php, line: 1 */ $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop();
/** file: foreach-include.blade.php, line: 2 */ (function () use($products) {
$bar = $product;
/** file: bar.blade.php, line: 2 */ echo e($foo + 'bar');