⌘K
Noting Found

View all related articles

Automate Your Workflow Using GitHub Action & PHP_CS-Fixer

General 2 mins read

Table of Content

    Introduction

    When we build an application, codebase getting messy with time, and likely we'll forget to refactor or remove unnecessary stuff such as unused namespaces or make proper indentation etc. Today we will learn about a great feature in GitHub Actions which is: php-cs-fixer job action

    GitHub Action, PHP_CS-Fixer Workflow

    Prerequisite

    You need the basics of GitHub & Background of PHP.

    What is GitHub Action

    From the official landing page

    GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

    What is PHP_CS-Fixer

    a php-cs-fixer it's a tool to automatically fix PHP Coding Standards issues built by symfony maintainers to help you follow the coding standard and best practices.

    From the website

    The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc., or other community driven ones like the Symfony one. You can also define your (team’s) style through configuration.

    It can modernize your code (like converting the pow function to the ** operator on PHP 5.6) and (micro) optimize it.

    If you are already using a linter to identify coding standards problems in your code, you know that fixing them by hand is tedious, especially on large projects. This tool does not only detect them, but also fixes them for you.

    PHP_CS-Fixer in Existing Laravel/PHP Project

    If you want to begin working with php-cs-fixer in you Laravel application, do the following

    Add PHP_CS-Fixer File in Your Base Directory

    Let's create a file called .php_cs.dist.php in the base directory on your project and add the following lines:

    1<?php
    2 
    3$finder = Symfony\Component\Finder\Finder::create()
    4 ->in([
    5 __DIR__ . '/src',
    6 __DIR__ . '/tests',
    7 ])
    8 ->name('*.php')
    9 ->notName('*.blade.php')
    10 ->ignoreDotFiles(true)
    11 ->ignoreVCS(true);
    12 
    13return (new PhpCsFixer\Config())
    14 ->setRules([
    15 '@PSR2' => true,
    16 'array_syntax' => ['syntax' => 'short'],
    17 'ordered_imports' => ['sort_algorithm' => 'alpha'],
    18 'no_unused_imports' => true,
    19 'not_operator_with_successor_space' => true,
    20 'trailing_comma_in_multiline' => true,
    21 'phpdoc_scalar' => true,
    22 'unary_operator_spaces' => true,
    23 'binary_operator_spaces' => true,
    24 'blank_line_before_statement' => [
    25 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
    26 ],
    27 'phpdoc_single_line_var_spacing' => true,
    28 'phpdoc_var_without_name' => true,
    29 'class_attributes_separation' => [
    30 'elements' => [
    31 'method' => 'one',
    32 ],
    33 ],
    34 'method_argument_space' => [
    35 'on_multiline' => 'ensure_fully_multiline',
    36 'keep_multiple_spaces_after_comma' => true,
    37 ],
    38 'single_trait_insert_per_statement' => true,
    39 ])
    40 ->setFinder($finder);

    This file contains a coding standard you php-cs-fixer plugin will use them to fix your project coding standard.

    If you want to know more about these options and extra ones, Take a look here

    Configure GitHub Actions

    In order to add php-cs-fixer to your GitHub action project repository, you need to create in your project codebase two folders

    1cd /path/to/your/project
    2 
    3# And Then run this
    4mkdir -p .github/workflows

    With this command we created 2 folders, one called .github and the second one is workflows to let GitHub know that the files lives in the directory workflows is for GitHub Action

    Create PHP_CS-Fixer Configuration File

    Inside workflows folder, create a YAML file and call it php-cs-fixer.yml and put this inside it

    1name: Check & fix styling
    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@v2
    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.dist.php --allow-risky=yes
    19
    20 - name: Commit changes
    21 uses: stefanzweifel/git-auto-commit-action@v4
    22 with:
    23 commit_message: Fix styling

    In this file, we tell GitHub to run php-cs-fixer inside your codebase and commit the changes into existing project.

    Note:

    If you want to run php-cs-fixer only in specific branch, let's say you want to run this only in the main or master branch and not in dev branch do the following

    Instead of this

    1on: [push]

    Do This

    1on:
    2 push:
    3 branches: [ main ]
    4 pull_request:
    5 branches: [ main ]

    We Tell GitHub Actions to run the php-cs-fixer only when we push changes or accepting a pull_request, otherwise skip this step.

    Conclusion

    Today We Learned about GitHub Actions and PHP-CS-fixer and how to use them in your application to help you automate your workflow and be more productive and focus on your code rather than fixing coding standards such as removing unused namespaces or remove indentation, because it will be a hard work and time-consuming if you have a big project.

    That's it for today and hope you learned something useful, and if you have any question put them in the comment section, and I'll very be happy to answer them.

    Related Tags

    About the Author

    Oussama's Profile Picture
    Oussama
    Full Stack Web Developer | Technical Writer

    Oussama is an experienced full-stack web developer with a strong focus on Laravel. He's passionate about crafting web applications with Filament and the TALL Stack. With 8+ years of experience, and he's a dedicated open-source contributor.

    Comments

    Join our newsletter

    Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

    We care about the protection of your data. Read our Privacy Policy.