Github Actions to Format Laravel Project Files

In this Article, we will see how we can use Github Actions to automatically format the PHP Files using php-cs-fixer on a Laravel Project. Even though the article discusses Laravel Project, it is also applicable to any PHP Project.

While working on a Project, it is a common practice to enforce some Coding Standard so as to make sure that your code is well maintained and easy to understand. PSR-2 is one Standard that is normally followed in PHP or Laravel Projects. And although the modern Editors allows you to format the code using the Extensions, there are a certain disadvantages to this approach.

  • You must have phpcs installed on your machine.
  • There could be inconsistency due to different rules or extensions.
  • Developer might simply forget to format the code if auto formatting is disabled.
  • Enforcing a New Rule or updating an existing Rule is difficult.

Enter Github Actions. Among other things, you can also use Github Actions to Format your Code. You can configure the Github Actions in such a way that all the Code Formatting will take place when you push the code to Github. This way the consistency is maintained and the whole process is opaque to developers.

First of all you will need to create a File at the root of the application, which will define all the Phpcs rules that we are going to enforce on our Project. Let us call it .php_cs.dist.php and here are the contents of the file.

<?php

$finder = Symfony\Component\Finder\Finder::create()
    ->in([
        __DIR__ . '/app',
    ])
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return (new PhpCsFixer\Config())
    ->setRules([
        '@PSR2' => true,
        'array_syntax' => ['syntax' => 'short'],
        'ordered_imports' => ['sort_algorithm' => 'alpha'],
        'no_unused_imports' => true,
        'not_operator_with_successor_space' => true,
        'trailing_comma_in_multiline' => true,
        'phpdoc_scalar' => true,
        'unary_operator_spaces' => true,
        'binary_operator_spaces' => true,
        'blank_line_before_statement' => [
            'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
        ],
        'phpdoc_single_line_var_spacing' => true,
        'phpdoc_var_without_name' => true,
        'method_argument_space' => [
            'on_multiline' => 'ensure_fully_multiline',
            'keep_multiple_spaces_after_comma' => true,
        ],
        'single_trait_insert_per_statement' => true,
    ])
    ->setFinder($finder);

Here we are just telling Symfony Finder Component the directory in which we want the Code Formatting to be enforced along with the File Extensions to include as well as exclude. If you have used Laravel, it should be pretty obvious. Then we have define some rules. You can of course, remove or modify any of these Rules. We have taken these rules from Spatie Open Source Project, which should be enough to vouch for the viability of the standards.

And now we are going to create a Github Action. Within your Repo on Github, you can go to Actions Tab and click on “New Workflow”. Here is the one that we are using.

name: Check &amp; fix styling

on: [push]

jobs:
    php-cs-fixer:
        runs-on: ubuntu-latest

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

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

            - name: Commit changes
              uses: stefanzweifel/git-auto-commit-action@v4
              with:
                  commit_message: Fix styling

This file follows the yml standard. We have given it a Name. You can Name it anything that you want. Then we have specified that we want to run the Github Action on a push Event.

Then we are going to define the various steps of our Job. Most of it is self explanatory and will probably remain same for your project. However, there are a couple of important lines there.

args: --config=.php_cs.dist.php --allow-risky=yes

Here we are specifying the path of the file which is present in the root of our Project. Please make sure that name, .php_cs.dist.php, matches with the one that you have used.

commit_message: Fix styling

And then we have the commit message. If any Coding Standards Rules are enforced and there are file changes, Github will automatically make a commit with the above commit message.

You can save this file with any Name and you are good to go. You can check the similar Github Actions that I have implemented in one of our Open Source Project. This has really helped us improve the collaboration within our team. Next time you start working on a project, this should be one of the first thing to setup.