PHP Coding Standards Fixer with Github Actions
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:
.github/workflows/php-cs-fixer.yml
This will kick off GitHub Action for PHP-CS-Fixer found here.
Add the following file:
name: Apply PHP CS Fixer
on: [push]
jobs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php-cs-fixer.php --allow-risky=yes
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
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:
on:
push:
branches:
- 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.