Tracking Type Coverage in PHP and Laravel

Announcing support for tracking type coverage in PHP and Laravel

27. Dec 2023
ยท

Recently I contributed a PR to the pestphp/pest-plugin-type-coverage repository, that allows you to output the type coverage as a JSON file, so that we can use this data to feed our OtterWise type coverage badges, graphs and more.

The Why

Aiming for high type coverage can be a great way to rule out potential bugs and also make your code more readable to newcomers and your IDE.

Being able to know if a Pull Request changes type coverage for the worse, or see how your team is adopting stricter typing, can be super powerful. It will let your team make strict rules that prevent merging PRs that lower type coverage, and show the historical progress for each repository, and the entire organisation.

The How

Install the plugin (if you haven't already)

See installation instructions here: https://pestphp.com/docs/type-coverage.

You must use version 2.6.0 or higher of the plugin (v2.29.0 of Pest), as this is the version that includes the JSON output option.

Modify Pest script in your CI pipeline

Your current Pest script might look something like this:

- name: Execute tests
  run: vendor/bin/pest --coverage-clover=build/logs/clover.xml

This will run your test suite, but since --type-coverage argument will actually exclusively scan your code for types, and not run any tests, we need to add this as a separate step, before uploading Code Coverage to OtterWise:

- name: Execute tests
  run: vendor/bin/pest --coverage-clover=build/logs/clover.xml
  
- name: Calculate type coverage
  run: vendor/bin/pest --type-coverage --type-coverage-json=pest-coverage.json

This will output your Pest type coverage to a file called pest-coverage.json in the root of your project.

Feed the file into the OtterWise bash command

Now that the file is generated, we can let OtterWise know where it is, so that it can be uploaded for processing.

bash <(curl -s https://raw.githubusercontent.com/getOtterWise/bash-uploader/main/uploader.sh) --type-coverage-file pest-coverage.json

That's it! We will now process type coverage for your project, and generate badges, reports and more. You can of course continue uploading Code Coverage as well, and much more.

Once you process a build, it will upload to OtterWise, and within seconds you will be able to see the results in your dashboard, as well as enable CI rules to fail your build if the type coverage drops below a certain threshold, to prevent a 'bad' pull request from being merged.

The future

Adding Pest support was an obvious first choice, as we ourselves use Pest for testing (the OtterWise test suite runs on Pest + Type Coverage w/ Pest), but this is just step 1 out of many. Currently I am implementing TypeScript support as well, to get the same metrics for your frontend code.

Improve code quality today_

With OtterWise, you can track Code Coverage, test performance, contributor stats, code health, and much more.