Code format fixer for Laravel/PHP


In this post we are going to talk about adding PhpCsFixer for your Laravel/PHP project.
PhpCsFixer is a tool that can automatically fix coding style issues in your PHP codebase.

Follow below steps to setup the PhpCsFixer in your project.

  • Install PhpCsFixer

Open your terminal and navigate to your Laravel project's root directory.
Then, install php-cs-fixer as a development dependency using Composer by running

composer require --dev friendsofphp/php-cs-fixer
  • Create a Configuration File

create .php-cs-fixer.php file in your root directory and paste below

<?php

$rules = [
    '@Symfony' => true,
    'no_mixed_echo_print' => true,
    'phpdoc_no_empty_return' => false,
    'array_syntax' => ['syntax' => 'short'],
    'no_multiline_whitespace_around_double_arrow' => true,
    'no_trailing_comma_in_singleline_array' => true,
    'trim_array_spaces' => true,
    'normalize_index_brace' => true,
    'yoda_style' => false,
    'concat_space' => ['spacing' => 'one'],
    'not_operator_with_space' => false,
    'increment_style' => ['style' => 'post'],
    'php_unit_method_casing' => ['case' => 'snake_case'],
    'phpdoc_no_alias_tag' => false,
    'global_namespace_import' => [
        'import_classes' => true,
        'import_constants' => true,
    ],
    'phpdoc_align' => [
        'align' => 'vertical',
        'tags' => [
            'param',
            'property',
            'property-read',
            'property-write',
            'return',
            'throws',
            'type',
            'var',
            'method',
        ],
    ],
];

$finder = PhpCsFixer\Finder::create()
    ->in([
        __DIR__ . '/app',
        __DIR__ . '/config',
        __DIR__ . '/database',
        __DIR__ . '/resources',
        __DIR__ . '/routes',
        __DIR__ . '/tests',
    ]);

$config = new PhpCsFixer\Config();
return $config->setRules([
    '@PSR12' => true,
    'array_syntax' => ['syntax' => 'short'],
    // Add more rules as needed
])
    ->setFinder($finder);

    or

return (new PhpCsFixer\Config())
    ->setUsingCache(true)
    ->setRules($rules)
    ->setFinder($finder);

This option instructs PhpCsFixer to adhere to the PSR-12 coding standard and run on the specified directories
(you can modify these to match the organisational structure of your project). The rules can be altered to adhere to your preferred coding standards.

  • Adding composer script

add a script to your composer.json file like

"scripts": {
    "cs-fix": "vendor/bin/php-cs-fixer fix"
}

Now you can run composer cs-fix in the terminal to run PhpCsFixer

  • Run PhpCsFixer directly

you can run PhpCsFixer without adding script in composer.json

vendor/bin/php-cs-fixer fix
  • Integration with Laravel Forge (Optional)

If you're using Laravel Forge to deploy your application, you can configure the "Post-Deployment Script" to run PhpCsFixer after each deployment.

Remember to adjust the paths, rules, and settings in the configuration file to match your project's needs.