- Introduction
- Set up the GitHub Actions workflow
- Configure the workflow file
- Checkout the repository
- Set up the PHP environment
- Run the linting
- Complete workflow file
- Conclusion
Introduction
A high quality and consistency of your code is crucial to ensure a robust and maintainable codebase. An efficient way to achieve this is code linting, which helps identifying/fixing code standard violations and standardizes the formatting across multiple developers. For PHP projects, php-cs-fixer
is a powerful tool that not only checks the code but also automatically fixes it according to predefined coding standards. By integrating php-cs-fixer
with GitHub Actions, we can automate the linting process, ensuring that every pull request adheres to our rules before being merged. In this blog post, I am going to describe how to set up the GitHub Actions worklow to automatically lint your PHP application.
Set up the GitHub Actions workflow
The following steps are necessary to run automatic workflows on GitHub. The obvious one is, that your repository has to be on GitHub. Make sure your repository contains a .github/workflows
directory, as YAML files in this directory will be considered as potential workflows for GitHub. Create a lint.yml
file in this directory. Below, I will focus on the contents of this file.
Configure the workflow file
Start the file by naming the workflow:
name: Lint
Next, specify the events that trigger the workflow. In our case, we want to lint our application on each pull request to develop
and release/**
branches:
on:
pull_request:
branches:
- develop
- "release/**"
Afterwards, set up the jobs that are part of the workflow. As we just want to run the linting, there is only one lint-applications
job. This job uses the latest stable version of the Ubuntu operating system (ubuntu-latest
) as a virtual environment:
jobs:
lint-application:
name: Lint application
runs-on: ubuntu-latest
Checkout the repository
The job itself contains multiple steps. The first step checks out the repository, so the workflow can access it:
steps:
- name: Checkout repository
uses: actions/checkout@v4
Set up the PHP environment
As the next step, use the shivammathur/setup-php@v2
action to set up PHP and install php-cs-fixer
:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
tools: php-cs-fixer
Run the linting
To perform the actual linting, add a step to run php-cs-fixer
. By providing the --dry-run
flag, this will just identify code standard violations without fixing them. --config
allows the code to be validated against a custom configuration file (php-cs-fixer.php
) with custom code rules. If you want to know more about this configuration and available rules, check out the official documentation:
- name: Run linting
run: php-cs-fixer fix --dry-run --config=./.php-cs-fixer.php
Complete workflow file
By combining the snippets listed above, you get the complete workflow file:
name: Lint
on:
pull_request:
branches:
- develop
- "release/**"
jobs:
lint-application:
name: Lint application
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
tools: php-cs-fixer
- name: Run linting
run: php-cs-fixer fix --dry-run --config=./.php-cs-fixer.php
Conclusion
Integrating linting tools, such as php-cs-fixer
, with GitHub Actions, can significantly improve your codebase to be more robust and maintainable. If you do not have an automatic linting process in place yet, this blog post will help you set up a workflow on GitHub.