Blog Detail

22

Aug
Automatic Code formatting for Apps with Laravel Code Style cover image

arrow_back Automatic Code formatting for Apps with Laravel Code Style

Laravel Code Style is an awesome package that provides automatic code style checking and formatting for Laravel applications and packages. Your code is formatted following Laravel’s code style guide. The package adds the php-cs-fixer tool and a community-maintained ruleset to your application. The ruleset is the best effort to match the code style the Laravel framework uses. Check out an example to see what the code style looks like.

You might want to use this package if you are writing a Laravel application, package, or tutorial and you want to match the framework’s code style.

Installation

Require this package with the composer. It is recommended to only require the package for development.

composer require jubeki/laravel-code-style --dev

The service provider will be automatically registered using package discovery.

If you don’t use auto-discovery you should add the service provider to the providers array in config/app.php.

// existing providers...
Jubeki\LaravelCodeStyle\ServiceProvider::class,

Once the package is installed you should publish the configuration.

php artisan vendor:publish --provider="Jubeki\LaravelCodeStyle\ServiceProvider"

Publishing the config will add a .php-cs-fixer.dist.php configuration file to the root of your project. You may customize this file as needed. The .php-cs-fixer.dist.php file should be committed to version control.

A cache file will be written to .php-cs-fixer.cache in the project root the first time you run the fixer. You should ignore this file so it is not added to your version control system.

echo '.php-cs-fixer.cache' >> .gitignore

Usage

Once the package is installed you can check and fix your code formatting with the php-cs-fixer command. The command will be available in Composer’s vendor/bin directory.

Fixing

To automatically fix the code style of your project you may use the php-cs-fixer fix command.

vendor/bin/php-cs-fixer fix

This will automatically fix the code style of every file in your project.

By default, only the file names of every file fixed will be shown. To see a full diff of every change append the –diff flag.

vendor/bin/php-cs-fixer fix --diff

Checking

If you would like to check the formatting without actually altering any files you should use the fix command with the –dry-run flag.

vendor/bin/php-cs-fixer fix --dry-run --diff

Composer script

To make checking and fixing code style easier for contributors to your project it’s recommended to add the commands as a composer script.

The following example allows anyone to check the code style by calling composer check-style and to fix the code style with composer fix-style.

{
    // ...
    "scripts": {
        "check-style": "php-cs-fixer fix --dry-run --diff",
        "fix-style": "php-cs-fixer fix"
    }
}

More Options

For a complete list of options please consult the php-cs-fixer documentation.

For more details about this package, Please visit Github

Note

If you are an enterprise and want to use develop a CRM, e-commerce, or SAAS web application in Laravel, you must discover an impeccable team with proficiency in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Our CRM development with Laravel holds a wide range of features for entities to manage their interactions with customers.

So if you have a great idea, then please feel free to contact us or you can launch a project with us.

Published at : 22-08-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project