phpunit/php-code-coverage
phpunit/php-code-coverage collects, processes, and renders PHP code coverage data. Integrate it in test runs to start/stop coverage collection, filter included files, and generate reports such as OpenClover, including from serialized coverage data.
Artisan::call(), Http::fake()) to measure coverage of routes, controllers, and service layers.--dev dependency) and requires minimal configuration for basic usage. Works out-of-the-box with PHPUnit or Pest.phpunit/phpunit or custom scripts.php-coveralls).spatie/laravel-coverage (for simplified setup).barryvdh/laravel-ide-helper (to exclude generated files from coverage).vendor/, bootstrap/cache/).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| PHP Version Support | Drops PHP 8.3 in v14.0.0. Laravel 10+ uses PHP 8.2/8.3. Minor risk if using older Laravel versions. | Pin to ~12.5 for PHP 8.2+ compatibility. Monitor Laravel’s PHP version roadmap. |
| Xdebug/PCOV Conflicts | Issues with Xdebug + PCOV (e.g., #1131). May affect parallel test runs in CI. | Use Xdebug 3.2+ or PCOV exclusively. Configure CI to avoid mixed setups. |
| Performance Overhead | Serialization/deserialization and XML report generation can be slow for large codebases. | Cache reports in CI (e.g., store .coverage files). Use --coverage-text for lightweight checks. |
| Report Accuracy | Edge cases in branch/path coverage (e.g., closures, match expressions). May lead to false negatives in strict coverage policies. |
Validate reports against manual reviews or static analysis tools. Use --coverage-clover for detailed debugging. |
| Dependency Bloat | Adding to composer.json may bloat dev dependencies if not scoped properly. |
Scope to require-dev and exclude from production builds. |
| Laravel-Specific Gaps | No native Laravel Artisan commands or ServiceProvider wrappers. Requires manual setup. | Leverage spatie/laravel-coverage or build a custom Artisan command for Laravel-specific workflows. |
Coverage Goals:
Toolchain Integration:
Performance Tradeoffs:
vendor/, generated files)?--parallel) be supported, or is sequential safer?Laravel-Specific Needs:
app/Http/Controllers/) or exclude Laravel internals (e.g., Illuminate/)? How to configure filters?actingAs(), fake())? Need to ensure coverage data isn’t skewed by test doubles.Maintenance:
| Component | Compatibility | Notes |
|---|---|---|
| PHP Versions | PHP 8.1–8.3 (v12.x), PHP 8.2+ (v14.x). Laravel 10+ uses PHP 8.2/8.3. | Recommendation: Use ~12.5 for broad compatibility. |
| Testing Frameworks | PHPUnit (primary), Pest (via PHPUnit under the hood), Laravel’s built-in testing tools. | Pest users can leverage pest --coverage out-of-the-box. |
| CI/CD Systems | GitHub Actions, GitLab CI, CircleCI, Jenkins. | Works with any system that runs PHP CLI commands. Example: GitHub Actions uses phpunit/phpunit with --coverage-clover. |
| Reporting Tools | SonarQube (Cobertura), Codecov (Clover), custom HTML dashboards, PHPStorm. | SonarQube: Use --coverage-cobertura. Codecov: Use --coverage-clover. |
| Laravel Ecosystem | spatie/laravel-coverage, barryvdh/laravel-ide-helper, Laravel Forge/Envoyer. |
spatie/laravel-coverage simplifies setup. laravel-ide-helper can exclude generated files from coverage. |
| Monitoring Tools | Prometheus (via custom exporters), Datadog, New Relic. | Coverage data can be scraped and exposed as metrics (e.g., coverage_percent for SLOs). |
Assessment Phase:
Pilot Integration:
composer.json as dev dependency:
composer require --dev phpunit/php-code-coverage
<!-- phpunit.xml -->
<phpunit>
<extensions>
<extension class="SebastianBergmann\CodeCoverage\CodeCoverage"/>
</extensions>
<coverage>
<include>
<directory>./app</directory>
<exclude>
<directory>./app/Models/Generated*</directory>
</exclude>
</include>
<report>
<php>build/coverage.php</php>
How can I help you explore Laravel packages today?