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:

composer 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:


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

Add the following file:

name: Apply PHP CS Fixer

on: [push]

    runs-on: ubuntu-latest

      - name: Checkout code
        uses: actions/checkout@v3
          ref: ${{ github.head_ref }}

      - name: Run PHP CS Fixer
        uses: docker://oskarstark/php-cs-fixer-ga
          args: --config=.php-cs-fixer.dist.php --allow-risky=yes

      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
          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:

      - 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.