Dzianis Kotau
About Me

Мy name is Dzianis Kotau. I'm Solutions Architect and Zend Certified PHP Engineer. I'm PHP evangelist and loved in it.

Laravel Pint Format Option

Dzianis Kotau • July 30, 2022

php cicd

Pint is a new awesome tool in Laravel's ecosystem developed by Nuno Maduro. It's built on the top of PHP-CS-Fixer and provides simple but yet powerful interface to PHP-CS-Fixer options.

Laravel Pint

I started to use it from version 0.x and at that moment it didn't have option that I need: the ability to generate reports for CI/CD pipelines. For example in GitHub Actions, you can generate checkstyle report in your workflows. Such functionality is called Annotations. Then Github will use it to highlight errors directly in Pull Request UI. You don't need to open workflow logs and read them line by line.

So, I decided to contribute to Laravel Pint and added --format option that uses same option from PHP-CS-Fixer. The changes were accepted in Adds --format option PR and included in version 1.1 of Laravel Pint.

How can you use such functionality in your pipelines? Let's go through examples for Github Actions. Here I will use additional cs2pr third-party action.

Simple workflow action can be look like this:

- name: Show Pint results in PR
run: pint --test --format=checkstyle | cs2pr

If you want to have both logs and annotations you need to run pint twice:

- name: Check PHP code style
id: cs-check
run: pint --test

- name: Generate Annotations on CS errors
if: failure() && steps.cs-check.outcome != 'success'
run: pint --test --format=checkstyle | cs2pr

Bonus. If you look at my PR you can see that I suggested to use one more option --report to save format generated report to the file. In this case, you need to run pint only once to generate both logs and annotations:

- name: Check PHP code style
id: cs-check
run: pint --test --format=checkstyle --report=./pint-report.xml

- name: Show CS error results in PR
if: failure() && steps.cs-check.outcome != 'success'
run: cs2pr ./pint-report.xml

But this option was not accepted.