psalm/plugin-phpunit
Psalm plugin that understands PHPUnit tests to improve static analysis accuracy. Adds PHPUnit-aware stubs and type inference so assertions, mocks, and test helpers are checked correctly. Requires Psalm v4+. Install via Composer and enable with psalm-plugin.
framework, nova, and forge). It extends Psalm’s type system to understand PHPUnit-specific constructs, reducing false positives in test analysis.#[Test], #[DataProvider], #[Before]) and Psalm v7’s plugin API, making it ideal for Laravel’s 2024+ roadmap (PHP 8.2+, Laravel 10+).vapor). Reduces CI flakiness by preemptively catching type mismatches, unused providers, and invalid assertions.composer require --dev) and enables via psalm-plugin enable, requiring no code changes to existing tests. Compatible with Laravel’s monorepo structure (e.g., packages/framework, packages/forge).psalm/psalm-plugin-api, ensuring future compatibility with Psalm’s evolving architecture. Laravel’s existing Psalm configs (e.g., psalm.xml) can be extended without disruption.0.19.0). Laravel teams using Psalm v6 must upgrade to v7 (or use 0.19.x as a stopgap).framework package), reducing upgrade friction.checkForThrowsDocblock (fixed in 0.15.2) or handling non-iterable data providers (fixed in 0.15.1).0.19.x temporarily, but plan Psalm v7 migration.#[DataProvider], createMock(), or Prophecy? If yes, high ROI; if trivial unit tests, marginal benefit.feature/psalm-test-analysis) before full rollout.Http::fake(), Sanctum mocks, or database migration tests? These are prime candidates for the plugin’s value.#[Test], #[DataProvider]) and Psalm v7’s plugin API.# Laravel project root
composer require --dev psalm/plugin-phpunit
vendor/bin/psalm-plugin enable psalm/plugin-phpunit
psalm.xml to prioritize test analysis:
<file-list>
<directory>tests/</directory>
<exclude-name>Feature/*Test.php</exclude-name> <!-- Optional: Focus on critical tests -->
</file-list>
<plugins>
<plugin-class>PsalmPlugin\PHPUnitPlugin</plugin-class>
</plugins>
php-cs-fixer or robo.phar to run Psalm analysis before commits.phpunit step) to fail builds on test analysis errors:
- name: Run Psalm with PHPUnit Plugin
run: vendor/bin/psalm --no-cache --output-format=github
Phase 1: Psalm v7 Upgrade (If Needed)
framework package already uses v7).composer.json:
"require-dev": {
"psalm/psalm": "^7.0",
"psalm/plugin-phpunit": "^0.20"
}
vendor/bin/psalm --init to regenerate configs.Phase 2: Plugin Installation
composer require --dev psalm/plugin-phpunit
vendor/bin/psalm-plugin enable psalm/plugin-phpunit
Phase 3: Incremental Adoption
tests/Feature/AuthTest.php).assertSame() with dynamic data).Phase 4: Full Rollout
tests/Unit/, tests/Integration/).@dataProvider is unused").| Component | Compatibility | Notes |
|---|---|---|
| Psalm | v7.0+ (required), v6.x (limited via 0.19.x) |
Must upgrade for full feature support. |
| PHPUnit | 8.0+ (recommended), 9.0+ (full attribute support) | Laravel 10+ uses PHPUnit 10; no issues. |
| PHP | 8.0+ (plugin supports PHP 8.2+ via Psalm v7) | Laravel 10+ requires PHP 8.1+; safe. |
| Laravel | 9.0+ (Psalm v7 support), 10.0+ (recommended) | Ideal for Laravel’s modern stack. |
| Monorepos | Yes (install plugin per-package) | Useful for Laravel’s packages/ structure. |
| CI Systems | GitHub Actions, GitLab CI, Jenkins (any with PHP support) | No vendor-specific dependencies. |
#[Test] attributes.iterable<list<type>> support.composer update psalm/plugin-phpunit).How can I help you explore Laravel packages today?