Laravel packages are not a one-time release. To keep up with updates to Laravel itself, PHP, and dependent packages over years of maintenance, you need to run static analysis continuously alongside tests.Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
This page is a companion guide to Laravel Package Development, Testing Laravel Packages with Orchestra Testbench, and Package Version Compatibility Management. Adding static analysis to your implementation, testing, and versioning strategy makes packages easier to maintain in the long run.
What Is Static Analysis?
Static analysis is a technique that detects type mismatches and potential bugs without executing code. Laravel packages make heavy use of dynamic mechanisms such as the service container, Facades, and Eloquent, so static analysis lets you catch issues early that are easy to miss with runtime testing alone. Introducing static analysis brings the following benefits in particular:- Improved type safety — Detect incorrect arguments and return values before code review
- Early bug detection — Catch calls to non-existent methods and overlooked nullables before tests run
- Better IDE autocompletion — Aligning PHPDoc and Generics improves completion accuracy
- Stable long-term maintenance — Easier to identify breakages when upgrading Laravel or PHP
Setting Up PHPStan
Start by setting up the analysis foundation with PHPStan alone. PHPStan 2.x is stricter aboutmixed and nullable handling than previous versions, which makes it a good opportunity to clean up the public API of your package.
Run analysis against target directories directly
For packages,
src and tests are common starting targets.Setting Up Larastan
PHPStan alone cannot fully understand Laravel-specific mechanisms such as container resolution, Facades, and Eloquent relations. That is why you should pair it with Larastan, the Laravel extension for PHPStan.Add Larastan
The current package name is
larastan/larastan. Older articles may reference nunomaduro/larastan.Include Testbench for package development
Larastan boots a Laravel application container to resolve types. When analysing a Laravel package in isolation,
orchestra/testbench may be required.Configuring phpstan.neon
phpstan.neon is the central configuration file for analysis level, target paths, excluded paths, and ignore rules. For a Laravel package, it is more practical to start with a configuration you can incrementally raise rather than aiming for perfection from day one.
Below is an example phpstan.neon:
Choosing a Level from 0 to 9
PHPStan can be introduced gradually. It is safer to start at a level where the current codebase passes and raise it as fixes are made.| Level | Suitable stage |
|---|---|
| 0–3 | Reducing calls to unknown classes, functions, and methods |
| 4–6 | Aligning argument types, return types, and property types on the public API |
| 7–9 | Strictly handling nullables, unions, and mixed in a long-term maintenance phase |
paths
Specify the directories to analyse explicitly in paths. Including tests alongside src is effective because types tend to drift in test code.
excludePaths
Exclude only generated files, caches, and Workbench directories where static analysis adds little value. Over-excluding can hide errors you actually want to detect.
ignoreErrors
ignoreErrors should be a last resort. Use regular expressions to narrow down the message, and combine with path to limit the scope. This makes it easier to revert entries when Laravel or Larastan improves.
Commonly Used Type Annotations
The accuracy of PHPStan and Larastan depends heavily on how PHPDoc is written. In Laravel packages, it is particularly valuable to align@param, @return, @var, and Generics (@template).
@param class-string<TModel>— indicates the string is a Model class name, not an arbitrary string@return TModel|null— tells PHPStan thatfind()returns a concrete model type@var Collection<int, TModel>— explicitly states the key/value types of the collection@template TModel of Model— expresses a reusable generic repository
Laravel-Specific Considerations
Laravel’s “convenient magic” is not understood by static analysis without additional type information. You need to add type information on the package side so the analyser can understand it.Facades
For custom Facades, annotating the methods callers use with PHPDoc benefits both IDE support and static analysis.Magic Methods
APIs that rely on__call() or Macroable are convenient but prone to type drift. If you expose them as a public API, it is safer to add wrapper methods with clear return types and arguments.
Typing Eloquent Models
Combining@property with Generics on relation methods is effective for Eloquent relations and dynamic properties.
Running in CI
Always run static analysis in CI, not just locally. For packages that support multiple Laravel versions, running it with the same test matrix used in Package Version Compatibility Management lets you monitor both compatibility and type safety at the same time. Below is an example.github/workflows/static-analysis.yml:
Common False Positives and How to Handle Them
When you see static analysis warnings, first ask whether type information is missing. What looks like a false positive is often just insufficient PHPDoc.@phpstan-ignore-next-line
This can be used as a temporary workaround, but limit it to lines where you can explain the reason yourself.
ignoreErrors
Only consider this when the same error appears in multiple locations. Always narrow the path and avoid broad regular expressions that suppress the entire codebase.
Recommended Order of Fixes
- Add PHPDoc
- Explicitly type Facade and relation return values
- Replace
mixedwith concrete types - Only then, ignore errors that are genuinely explainable false positives
Related Pages
Laravel Package Development
Review the fundamentals of package implementation including service providers and publishable resources.
Testing Laravel Packages with Orchestra Testbench
Explore the package testing foundation you will want to run alongside static analysis.
Package Version Compatibility Management
Review the Laravel/PHP compatibility matrix and GitHub Actions matrix strategy.