Static Analysis for Laravel Blade Templates

Packages

March 28th, 2023

Static Analysis for Laravel Blade Templates

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.

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:17
rendered 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.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.