Static Analysis for Laravel Blade Templates
Published on by Paul Redmond
Have you ever released a Blade template to production that was accidentally missing a variable or a method that had a typo? Have you wanted to avoid finding templating bugs in production? Well, check out Bladestan, by the author of rector, which adds static analysis in PHPStan for Blade templates.
Can @phpstan statically analyse @laravelphp templates?
— Tomas Votruba (@VotrubaT) March 24, 2023
Hell yeah 😎🥳️https://t.co/m6txBPzwWh
This package finds all view calls and looks if rendered variables have valid behavior in the provided template. For example, it will check the template file path and ensure it exists, inspect the variables, render the template, and review for any violation using your PHPStan setup.
Here's an example from the author's blog post Introducing Bladestan:
@foreach($posts as $post) {{ $post->getContet() }}@endforeach
In the above example, PHPStan would return the following report:
blog.blade.php:17rendered in: app/Http/Controllers/BlogController.php:20-------------------------------------------------------------- - '#Call to an undefined method App\\Entity\\Post\:\:getContet\(\)#'
Bladestan takes care of everything you need to check your blade templates statically and uses your existing PHPStan setup, including Larastan.
To start with Bladestan, you can get the installation and setup instructions from the GitHub repo.