Technical article
PHPStan and Larastan in Laravel: find errors earlier
Use static analysis with a realistic level, explicit types and framework knowledge to find errors before runtime.
- Laravel & PHP
- PHPStan
- Larastan
- Laravel
- find
- errors

In this article
PHPStan can find invalid method calls and unsafe handling of missing values before they fail in a user flow. Larastan adds Laravel knowledge. Use analysis alongside tests: types can be correct while a business decision is wrong.
Choose an achievable analysis contract
includes:
- vendor/larastan/larastan/extension.neon
parameters:
level: 8
paths:
- app
- routes
treatPhpDocTypesAsCertain: falseThis sample analyses app and routes at level 8, which checks access to nullable types among other rules. That is an illustrative choice, not a compulsory starting level. Keep treatPhpDocTypesAsCertain: false only when deliberately accounting for potentially unreliable PHPDoc types.
Illustration: a user may be missing
User::find($id) can return no user. Calling a method afterwards without a check can fail. Choose an explicit failure path or findOrFail when a missing user should return 404. An invented @var annotation or cast does not change the runtime outcome.
Compatibility and bootstrapping
Check composer.lock and the supported PHP, Laravel, PHPStan and Larastan combination. Larastan boots the application container to understand Laravel types. Provide an appropriate analysis environment without production credentials or external actions during service-provider boot.
php -d memory_limit=2G vendor/bin/phpstan analyse --no-progressA baseline records bounded debt
A reviewed baseline can record existing findings so new errors remain visible. Do not automatically add new findings to it. Record why exclusions exist and reduce old exceptions when changing the relevant code.
From finding to evidence
- Reproduce the finding with locked tool versions.
- Correct the actual type or missing behaviour.
- Run analysis again without a broad ignore rule.
- Test runtime behaviour for both found and missing records.
Successful analysis does not prove authorisation, mail delivery or database performance. Those questions retain their own checks.
Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.
Technical deep dive: use static analysis to make uncertain data explicit.
Illustrative scenario: a method expects a model or value that can actually be nullable. The normal flow appears to work, but a missing relationship or configuration value can later cause a runtime failure.
Technical decision
Fix the underlying type contract instead of bypassing the analyser. Decide whether null is valid, validate the system boundary and pass a concrete type to the rest of the code afterwards.
Checks
- Decide whether null is a valid domain state or an error condition.
- Normalise mixed data at request, configuration, JSON and external API boundaries.
- Avoid ignores and artificial casts that only hide the uncertainty.
- Add a behaviour test when the corrected type assumption affects a business-critical flow.
A static-analysis warning is a signal. The correct fix depends on the real domain contract.
Last updated:


