PHPackages                             adiletmaks/laravel-code-style - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. adiletmaks/laravel-code-style

ActiveLibrary

adiletmaks/laravel-code-style
=============================

Code formatting for Laravel projects

0.4.1(6y ago)06MITPHPPHP &gt;=7.1

Since Apr 23Pushed 6y agoCompare

[ Source](https://github.com/adiletmaks/laravel-code-style)[ Packagist](https://packagist.org/packages/adiletmaks/laravel-code-style)[ Docs](https://github.com/adiletmaks/laravel-code-style)[ RSS](/packages/adiletmaks-laravel-code-style/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (6)Used By (0)

Laravel Code Style
------------------

[](#laravel-code-style)

[![Packagist License](https://camo.githubusercontent.com/e3ac4ddfcac570896801f55cd9606a5519ff6b8e4c7959fed2f5f4da619e93a4/68747470733a2f2f706f7365722e707567782e6f72672f6d6174742d616c6c616e2f6c61726176656c2d636f64652d7374796c652f6c6963656e73652e706e67)](http://choosealicense.com/licenses/mit/)[![Latest Stable Version](https://camo.githubusercontent.com/f8a7bf82b4947398d6f711db42bf0144b0010719ea410d9641cc3df1dedbe4b8/68747470733a2f2f706f7365722e707567782e6f72672f6d6174742d616c6c616e2f6c61726176656c2d636f64652d7374796c652f76657273696f6e2e706e67)](https://packagist.org/packages/matt-allan/laravel-code-style)[![Build Status](https://camo.githubusercontent.com/c5a94c724f28c11b6b71cae41552785e60a2f0a47f507132383166d5c7c9752b/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6d6174742d616c6c616e2f6c61726176656c2d636f64652d7374796c652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/matt-allan/laravel-code-style)

This package 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](https://github.com/FriendsOfPhp/PHP-CS-Fixer) tool and a community maintained ruleset to your application. The ruleset is a best effort attempt to match the code style the Laravel framework itself uses. Check out an [example](./examples/User.php) 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.

If you are wondering why this package exists you can [read the announcement post](https://mattallan.me/posts/automate-code-formatting-for-laravel-projects/).

[![example code formatting](https://repository-images.githubusercontent.com/182856423/07c42600-6fe5-11e9-88fb-dd2e7451c77a)](https://repository-images.githubusercontent.com/182856423/07c42600-6fe5-11e9-88fb-dd2e7451c77a)

Installation
------------

[](#installation)

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

```
composer require matt-allan/laravel-code-style --dev
```

The service provider will be automatically registered using [package discovery](https://laravel.com/docs/5.8/packages#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...
MattAllan\LaravelCodeStyle\ServiceProvider::class,
```

Once the package is installed you should publish the configuration.

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

Publishing the config will add a `.php_cs` configuration file to the root of your project. You may customize this file as needed. The `.php_cs` file should be committed to version control.

A cache file will be written to `.php_cs.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.cache' >> .gitignore
```

Usage
-----

[](#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

[](#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

[](#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
```

In dry-run mode any violations will [cause the command to return a non-zero exit code](https://github.com/FriendsOfPhp/PHP-CS-Fixer#exit-code). You can use this command to fail a CI build or git commit hook.

### Composer script

[](#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](https://getcomposer.org/doc/articles/scripts.md).

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

[](#more-options)

For a complete list of options please consult the [php-cs-fixer documentation](https://github.com/FriendsOfPhp/PHP-CS-Fixer#usage).

Configuration
-------------

[](#configuration)

The default configuration is published as `.php_cs` in the project root. You can customize this file to change options such as the paths searched or the fixes applied.

### Paths

[](#paths)

You can change the paths searched for PHP files by chaining method calls onto the `PhpCsFixer\Finder` instance being passed to the `MattAllan\LaravelCodeStyle\Config::setFinder` method.

For example, to search the `examples` directory you would append `->in('examples')`:

```
