Jose Jimenez
Jose Jimenez
Software Architect & Developer
> >

PHP Coding Standards Fixer with Github Actions

Published in PHP, Laravel, GitHub Actions, Composer on Jan 31, 2023

This tutorial will guide you on how to configure PHP CS Fixer with GitHub Actions.

This will allow your code to follow code standards and be unified within your application.

To get started install PHP CS Fixer in your application using composer:

1composer require friendsofphp/php-cs-fixer --dev

Next you will want to create the .php-cs-fixer.php in the root of your application. You can configure a list of rules, If you are heavy into Laravel, you can use the Laravel Style rule set found here (thanks to the Laravel Shift team).

Next create the GitHub Action in your repository by adding the following structure and file:

.github/workflows/php-cs-fixer.yml

This will kick off GitHub Action for PHP-CS-Fixer found here.

Add the following file:

1name: Apply PHP CS Fixer
 2
3on: [push]
 4
 5jobs:
6 php-cs-fixer:
7 runs-on: ubuntu-latest
 8
9 steps:
10 - name: Checkout code
11 uses: actions/checkout@v3
12 with:
13 ref: ${{ github.head_ref }}
14
15 - name: Run PHP CS Fixer
16 uses: docker://oskarstark/php-cs-fixer-ga
17 with:
18 args: --config=.php-cs-fixer.php --allow-risky=yes
19
20 - name: Commit changes
21 uses: stefanzweifel/git-auto-commit-action@v4
22 with:
23 commit_message: Apply PHP CS Fixer Changes

If you follow git-flow or have separate branches you may want to apply code formatting to one branch, you can update the on: [push] configuration:

1on:
2 push:
3 branches:
4 - develop

Final Thoughts

Popular editors will support PHP CS Fixer, allowing your code to be formatted as you work, you can follow instructions for the more popular ones here.