Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Code Coverage Laravel Package

phpunit/php-code-coverage

phpunit/php-code-coverage collects, processes, and renders PHP code coverage data. Use it to start/stop coverage during tests, filter included files, and generate reports such as OpenClover XML from live runs or serialized coverage data.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Core Use Case Alignment: The package is a direct fit for Laravel/PHP projects requiring granular code coverage analysis beyond PHPUnit’s built-in reporting. It enables:
    • Custom coverage collection (e.g., for non-test execution paths).
    • Advanced filtering (e.g., excluding vendor files, focusing on specific modules).
    • Integration with CI/CD pipelines via serialized reports (e.g., OpenClover for SonarQube).
  • Laravel Synergy:
    • Works seamlessly with Laravel’s testing stack (PHPUnit/Pest) but can also instrument non-test code (e.g., CLI commands, queues).
    • Supports parallel test execution (critical for Laravel’s large test suites).
    • Compatible with Laravel’s service container for dependency injection (e.g., injecting CodeCoverage into test listeners).

Integration Feasibility

  • Low Friction:
    • Composer-ready: Single composer require with no Laravel-specific setup.
    • PHPUnit Integration: Replaces or augments PHPUnit’s --coverage flag via custom test listeners.
    • Pest Support: Can be wired into Pest’s extend() or before/after hooks.
  • Reporting Flexibility:
    • Generates OpenClover XML (SonarQube), HTML, or Clover XML—useful for Laravel’s multi-tool ecosystem (e.g., GitHub Actions + SonarCloud).
    • Serialization/deserialization enables offline analysis (e.g., coverage reports in CI without real-time collection).

Technical Risk

Risk Area Mitigation Strategy
PHP Version Compatibility Laravel 10+ uses PHP 8.2–8.3; package drops PHP 8.3 support in v13.0.0. Upgrade to v12.x for full compatibility.
Xdebug/PCOV Conflicts Package handles conflicts (e.g., #1131), but test locally with both drivers.
Performance Overhead XML report generation is optimized (v12.5.x+), but parallel test runs may still hit race conditions (fixed in v13.0.2).
Breaking Changes v14.0.0 removed Report\PHP; migrate to Serialization\Serializer.
Dark Mode/HTML Reports v12.4.0+ supports dark mode, but custom CSS may override styles.

Key Questions for TPM

  1. Coverage Scope:
    • Should coverage be test-only (PHPUnit/Pest) or application-wide (e.g., CLI routes, jobs)?
    • Impact: Wider scope requires manual instrumentation (e.g., wrapping Artisan::call()).
  2. Reporting Needs:
    • Does the team need SonarQube integration (OpenClover) or internal dashboards (HTML)?
    • Impact: OpenClover requires schema validation; HTML reports need styling adjustments.
  3. CI/CD Pipeline:
    • Should reports be generated per-test-suite or aggregated (e.g., for multi-project Laravel apps)?
    • Impact: Aggregation needs custom scripting (e.g., merging XML files).
  4. Parallel Testing:
    • Will tests run in parallel (e.g., Pest’s --parallel)?
    • Impact: Race conditions in v<13.0.2 may require locking mechanisms or downgrades.
  5. Legacy Code:
    • Are there PHP 8.1/8.2-specific coverage tools (e.g., custom Xdebug configs)?
    • Impact: May need polyfills or version pinning.

Integration Approach

Stack Fit

  • Primary Integration Points:
    • PHPUnit: Replace --coverage with a custom listener using php-code-coverage.
    • Pest: Hook into extend() to inject coverage collection.
    • Laravel Console: Instrument Artisan::call() or HandleCommands for CLI coverage.
    • Queues/Jobs: Use before/after hooks in HandleQueues to start/stop coverage.
  • Reporting Stack:
    • SonarQube: Use OpenClover format (experimental in v12.3.0+).
    • GitHub Actions: Cache serialized coverage data between jobs.
    • Internal Tools: HTML reports can be served via Laravel’s Storage or Vite.

Migration Path

Step Action Tools/Libraries
1. Add Dependency composer require --dev phpunit/php-code-coverage Composer
2. Configure PHPUnit Replace --coverage with a custom listener (see example below). PHPUnit
3. Filter Scope Define Filter to exclude vendor/, node_modules/, etc. SebastianBergmann\CodeCoverage\Filter
4. Generate Reports Use ReportFacade for OpenClover/HTML in post-test hooks. Report\Facade
5. CI/CD Integration Serialize coverage (Serializer) and upload to SonarQube/GitHub. GitHub Actions, SonarScanner
6. Optimize Enable caching (CacheWarmer) for large codebases. SebastianBergmann\CodeCoverage\Cache

Example: PHPUnit Listener

use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\Facade as ReportFacade;

class CoverageListener implements \PHPUnit\Runner\Listener
{
    public function startTestSuite(\PHPUnit\Framework\TestSuite $suite): void
    {
        $filter = (new Filter)->includeFiles([__DIR__.'/../src/*']);
        $coverage = new CodeCoverage((new Selector)->forLineCoverage($filter), $filter);
        $coverage->start('laravel-tests');
    }

    public function endTestSuite(\PHPUnit\Framework\TestSuite $suite): void
    {
        $coverage->stop();
        ReportFacade::fromObject($coverage)->renderOpenClover(storage_path('app/coverage.xml'));
    }
}

Compatibility

  • Laravel-Specific:
    • Service Container: Bind CodeCoverage as a singleton for reuse across tests.
    • Testing Packages: Works with laravel/pint, spatie/laravel-test-factory, etc.
    • Dusk/Cypress: Requires Xdebug for browser coverage (not supported by PCOV).
  • Tooling:
    • SonarQube: OpenClover format is mostly compatible (see #1080).
    • Codecov: Use Clover XML format (stable).
    • Parallelizr: No conflicts; race conditions fixed in v13.0.2+.

Sequencing

  1. Phase 1: PHPUnit Integration
    • Replace --coverage with custom listener.
    • Validate reports against baseline (e.g., 80% coverage).
  2. Phase 2: CI/CD Pipeline
    • Add serialization/deserialization for cached reports.
    • Integrate with SonarQube/GitHub.
  3. Phase 3: Advanced Use Cases
    • Instrument CLI/queues for non-test coverage.
    • Customize HTML reports with Laravel Blade templates.

Operational Impact

Maintenance

  • Dependency Updates:
    • Critical: Pin to ^12.5 for PHP 8.2 compatibility (Laravel 10).
    • Non-Critical: Upgrade to ^14.0 for new features (e.g., dark mode), but test thoroughly.
  • Configuration Drift:
    • Filter rules may break if Laravel’s directory structure changes (e.g., app/src/).
    • Mitigation: Use relative paths in Filter (e.g., ./src/*).
  • Reporting Schema:
    • OpenClover format is experimental; monitor for breaking changes in minor releases.

Support

  • Debugging:
    • Coverage Gaps: Use Filter::includeUncoveredFiles() to debug missed files.
    • Xdebug Issues: Check for #1131 if reports are empty.
  • Community:
    • GitHub Issues: Active maintenance (100+ issues closed in 2023
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport