PavelZanek.com
build Tools

Larastan

I use Larastan as a way to bring static analysis into a Laravel project without ignoring Eloquent, facades, and the dynamic parts of the framework. It does not replace tests or review, but it can catch type errors, incorrect return values, and small problems that would otherwise appear only when the application is running.

Larastan

Larastan is one of those tools that does not look exciting at first, but can do a lot for the long-term health of a project. It will not make an admin screen faster, it will not add a visible feature, and a client will probably never notice it directly. Still, it can change how confidently a developer can move through a Laravel application.

Laravel is a dynamic framework. Eloquent models, facades, collections, the service container, and magic methods are convenient, but ordinary static analysis can struggle with them. Larastan builds on PHPStan and adds Laravel-specific knowledge. That allows it to find problems that plain PHPStan might miss or report too generally.

Why static analysis matters

In a small application, tests and manual review may feel sufficient. But the longer a project lives, the more small inconsistencies appear. A method returns something different than expected. A collection contains a different type of object. A relationship is named differently than a developer remembers. A nullable value is passed into a place that never expected null.

Larastan helps catch these issues earlier. It does not wait for the bug to happen in production or for a test to accidentally cover the exact path. It scans the code and points to places where types, return values, or method calls do not make sense. It is not a replacement for tests. It is another layer of feedback that covers a different kind of risk.

Laravel without false confidence

The biggest value of Larastan is that it does not ask developers to ignore how Laravel actually works. Generic PHP analysis can be either too weak or too noisy in frameworks with dynamic behavior. Larastan understands models, facades, the service container, and other Laravel concepts better, so its feedback is usually more useful.

That does not mean installing it solves everything. In an older application, the first run may report a lot of issues. Some will be real bugs. Others will be places where the code simply lacks precise types or documentation. The goal is not necessarily to reach the strictest level on day one. The first benefit is seeing where the codebase is unclear.

Increasing strictness gradually

I would introduce Larastan gradually. In an existing project, turning on the strictest settings immediately and spending a week fighting thousands of messages is rarely a good plan. It is better to choose a level the team can handle, fix the most valuable findings, and then raise the bar over time. Static analysis should support development, not become a new source of frustration.

A good rule is that new code should not add new problems. Older debt can be reduced step by step, while the pipeline prevents it from growing. Combined with PHPDoc, collection generics, more precise return types, and clearer service boundaries, Larastan starts showing value quickly.

Where it fits in the process

Larastan makes the most sense in CI next to tests, Pint, Rector, and other checks. Tests verify behavior, Pint checks style, Rector handles mechanical refactoring, and Larastan looks at type and structural consistency. Each tool protects a different part of the project. Together, they give the developer more confidence that a change is not only green locally, but also fits the wider codebase.

For AI-assisted development, that matters even more. An agent can generate code that looks convincing while still using the wrong type or calling a method that does not exist. Larastan can often catch that faster than a person reading a diff. Human review and judgment are still necessary, but static analysis reduces the space for quiet mistakes that would otherwise travel too far.

add_circle Missing a tool?

Suggest a tool for the list

If you could not find a useful tool here, send me its name and URL. I will review it and consider adding it to the public catalog.

alternate_email

Let's stay in touch

Subscribe to get the latest Laravel and infrastructure insights straight to your inbox.