- What is Larastan and how does it work with Laravel?
- Larastan is a PHPStan extension built for Laravel. It boots your Laravel application container so it can resolve runtime-aware types such as facades, container bindings, and Eloquent magic, then reports bugs without running your code. It adds static typing to Laravel projects and helps catch errors before you write tests.
- How do I install Larastan in a Laravel project?
- Install it as a development dependency with composer require --dev larastan/larastan. Then add the Larastan extension to your phpstan.neon configuration and set the paths and analysis level you want to check. You run it with vendor/bin/phpstan analyse. For Larastan 3.x, make sure PHPStan 2.3.0 or newer within the 2.x series is installed.
- Which Laravel and PHP versions does Larastan support?
- Larastan 3.x supports Laravel 11.16+ and requires PHP 8.2+. Larastan 2.x covers Laravel 9.0 through 11.16, while Larastan 1.x is for older Laravel versions below 9 and legacy PHP. Check the compatibility matrix before upgrading because Larastan, PHPStan, and Laravel versions are coupled.
- What PHPStan version does Larastan 3.13.0 require?
- Larastan 3.13.0 requires PHPStan 2.3.0 or newer within the PHPStan 2.x series. Pinning Larastan without updating PHPStan is the most likely upgrade breakage path, because older PHPStan versions are not a supported combination. Upgrade Larastan and PHPStan together and run analysis to see the new findings.
- How do I configure Larastan for a Laravel application?
- Create or update phpstan.neon to include vendor/larastan/larastan/extension.neon, then define the paths to analyse and a level from 0 to 9. Start at a lower level for existing codebases, fix high-value errors first, and raise the level over time. You can exclude generated or legacy directories and add baseline entries for known issues while you clean them up.
- Can Larastan analyze Eloquent relationships and eager loading?
- Yes. Larastan 3.13.0 adds relation-of<Model, Relationship> annotations to enforce concrete relationship types and infers actual relationship types inside with(), withOnly(), load(), and loadMissing() calls. It also flags invalid eager-loading column selections such as with(['posts:id' => $callback]), a pattern that can silently break model hydration at runtime. These checks are especially useful in services and repositories that type-hint relation results.
- Why does Larastan report new errors after upgrading to 3.13.0?
- The new errors are usually true positives from more accurate inference, not regressions. Larastan 3.13.0 corrects collection key types for concat(), merge(), shift(), and splice(), and it flags invalid eager-loading column selections. Expect a true-positive wave in existing pipelines and plan time to fix or baseline those findings rather than suppressing them broadly.
- How do I run Larastan in CI for a Laravel project?
- Add vendor/bin/phpstan analyse to your CI pipeline after Composer install, using the same phpstan.neon configuration as local development. Cache the PHPStan result cache between runs where your CI supports it to speed up repeated analysis. Pin Larastan and PHPStan versions in composer.lock so CI does not drift into unsupported version combinations.
- Does Larastan work with Pest, PHPUnit, and Laravel Testbench?
- Larastan analyses application code and is independent of the test runner, so it can sit alongside Pest or PHPUnit. Testbench-based package projects may need extra configuration so Larastan can bootstrap the package application correctly. Custom packages with incomplete PHPDoc coverage may still produce some false positives.
- How do I fix memory or performance issues when running Larastan on a large Laravel codebase?
- Larastan inherits PHPStan's memory and CPU profile, so very large codebases can need more memory or longer CI time. Increase PHPStan's memory limit, analyse in parallel where possible, and exclude generated, vendor, and legacy paths that do not need checks. Keep the analysis level realistic and use a baseline for existing errors so new issues stay visible.