{"id":13828282,"url":"https://github.com/matt-allan/laravel-code-style","last_synced_at":"2025-07-09T06:31:29.434Z","repository":{"id":45956882,"uuid":"182856423","full_name":"matt-allan/laravel-code-style","owner":"matt-allan","description":"Automatic code formatting for Laravel projects","archived":true,"fork":false,"pushed_at":"2022-02-09T15:06:52.000Z","size":105,"stargazers_count":404,"open_issues_count":0,"forks_count":33,"subscribers_count":11,"default_branch":"main","last_synced_at":"2024-11-16T02:49:48.850Z","etag":null,"topics":["code-style","formatter","laravel","php"],"latest_commit_sha":null,"homepage":"https://mattallan.me/posts/automate-code-formatting-for-laravel-projects/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matt-allan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-22T19:44:16.000Z","updated_at":"2024-10-12T01:50:38.000Z","dependencies_parsed_at":"2022-08-23T19:00:29.894Z","dependency_job_id":null,"html_url":"https://github.com/matt-allan/laravel-code-style","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-allan%2Flaravel-code-style","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-allan%2Flaravel-code-style/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-allan%2Flaravel-code-style/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-allan%2Flaravel-code-style/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matt-allan","download_url":"https://codeload.github.com/matt-allan/laravel-code-style/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225492420,"owners_count":17482869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-style","formatter","laravel","php"],"created_at":"2024-08-04T09:02:39.652Z","updated_at":"2024-11-20T08:30:23.447Z","avatar_url":"https://github.com/matt-allan.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"## Laravel Code Style\n[![Packagist License](https://poser.pugx.org/matt-allan/laravel-code-style/license.png)](http://choosealicense.com/licenses/mit/)\n[![Latest Stable Version](https://poser.pugx.org/matt-allan/laravel-code-style/version.png)](https://packagist.org/packages/matt-allan/laravel-code-style)\n![Tests](https://github.com/matt-allan/laravel-code-style/workflows/Tests/badge.svg)\n\n\u003e ⚠️ This package is no longer maintained. See [Jubeki/laravel-code-style](https://github.com/Jubeki/laravel-code-style) for a maintained fork.\n\n\nThis package provides automatic code style checking and formatting for Laravel applications and packages. Your code is formatted following Laravel's code style guide.\n\nThe 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.\n\nYou 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.\n\nIf you are wondering why this package exists you can [read the announcement post](https://mattallan.me/posts/automate-code-formatting-for-laravel-projects/).\n\n## Installation\n\n\u003e ⚠️ These docs are for the latest version. If you are using an older version you can find the docs for previous releases [here](#releases).\n\nRequire this package with composer. It is recommended to only require the package for development.\n\n```shell\ncomposer require matt-allan/laravel-code-style --dev\n```\n\nThe service provider will be automatically registered using [package discovery](https://laravel.com/docs/5.8/packages#package-discovery).\n\nIf you don't use auto-discovery you should add the service provider to the providers array in `config/app.php`.\n\n```php\n// existing providers...\nMattAllan\\LaravelCodeStyle\\ServiceProvider::class,\n```\n\nOnce the package is installed you should publish the configuration.\n\n```shell\nphp artisan vendor:publish --provider=\"MattAllan\\LaravelCodeStyle\\ServiceProvider\"\n```\n\nPublishing 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.\n\nA 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.\n\n```shell\necho '.php_cs.cache' \u003e\u003e .gitignore\n```\n\n## Usage\n\nOnce 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.\n\n### Fixing\n\nTo automatically fix the code style of your project you may use the `php-cs-fixer fix` command.\n\n```shell\nvendor/bin/php-cs-fixer fix\n```\n\nThis will automatically fix the code style of every file in your project.\n\nBy default only the file names of every file fixed will be shown.  To see a full diff of every change append the `--diff` flag.\n\n```shell\nvendor/bin/php-cs-fixer fix --diff\n```\n\n### Checking\n\nIf you would like to check the formatting without actually altering any files you should use the `fix` command with the `--dry-run` flag.\n\n```shell\nvendor/bin/php-cs-fixer fix --dry-run --diff\n```\n\nIn 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.\n\n### Composer script\n\nTo 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).\n\nThe following example allows anyone to check the code style by calling `composer check-style` and to fix the code style with `composer fix-style`.\n\n```javascript\n{\n    // ...\n    \"scripts\": {\n        \"check-style\": \"php-cs-fixer fix --dry-run --diff\",\n        \"fix-style\": \"php-cs-fixer fix\"\n    }\n}\n```\n\n### More Options\n\nFor a complete list of options please consult the [php-cs-fixer documentation](https://github.com/FriendsOfPhp/PHP-CS-Fixer#usage).\n\n## Configuration\n\nThe default configuration is published as `.php-cs-fixer.dist.php` in the project root.  You can customize this file to change options such as the paths searched or the fixes applied.\n\n### Paths\n\nYou 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.\n\nFor example, to search the `examples` directory you would append `-\u003ein('examples')`:\n\n```php\n\u003c?php\n\nrequire __DIR__ . '/vendor/autoload.php';\n\nreturn (new MattAllan\\LaravelCodeStyle\\Config())\n    -\u003esetFinder(\n        PhpCsFixer\\Finder::create()\n            -\u003ein(app_path())\n            // ...\n            -\u003ein('examples')\n    )\n    // ...\n\n```\n\nThe default paths are setup for a Laravel application.  If you are writing a package the path helper functions will not available and you will need to change the paths as necessary, i.e. `PhpCsFixer\\Finder::create()-\u003ein(__DIR__)`.\n\nFor a complete list of options refer to the [Symfony Finder documentation](https://symfony.com/doc/current/components/finder.html).\n\n### Rules\n\nBy default only the `@Laravel` preset is enabled.  This preset enforces the [PSR-2 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) as well as nearly 100 other rules such as ordering use statements alphabetically and requiring trailing commas in multiline arrays.\n\nA `@Laravel:risky` preset is also available.  The `@Laravel:risky` preset enables rules that may change code behavior.  To enable risky rules you need to add the preset and set `isRiskyEnabled` to true.\n\n```php\nreturn (new MattAllan\\LaravelCodeStyle\\Config())\n        -\u003esetFinder(\n            // ...\n        )\n        -\u003esetRules([\n            '@Laravel' =\u003e true,\n            '@Laravel:risky' =\u003e true,\n        ])\n        -\u003esetRiskyAllowed(true);\n```\n\nIt is possible to override a specific rule from the preset.  For example, you could disable the `no_unused_imports` rule like this:\n\n```php\nreturn (new MattAllan\\LaravelCodeStyle\\Config())\n        -\u003esetFinder(\n            // ...\n        )\n        -\u003esetRules([\n            '@Laravel' =\u003e true,\n            'no_unused_imports' =\u003e false,\n        ]);\n```\n\nFor a complete list of available rules please refer to the [php-cs-fixer documentation](https://github.com/FriendsOfPhp/PHP-CS-Fixer#usage).\n\n## Continuous Integration\n\nTo automatically fix the code style when someone opens a pull request or pushes a commit check out [StyleCI](https://styleci.io).  StyleCI wrote many of the open source fixer rules this package depends on and StyleCI's Laravel preset is the official definition of Laravel's code style.\n\n## Editor Support\n\nAny editor plugin for php-cs-fixer will work. Check the [php-cs-fixer readme](https://github.com/FriendsOfPhp/PHP-CS-Fixer#helpers) for more info.\n\n## How It Works\n\nLaravel does not publish an official php-cs-fixer ruleset.  To create the rule set we compare StyleCI's preset to the available php-cs-fixer rules.  In some cases StyleCI is using a rule that is no longer available.  For these rules we have to dig through the git history of php-cs-fixer and determine which rule replaced the deprecated rule.\n\nIt isn't possible to add your own presets to php-cs-fixer.  Instead `PhpCsFixer\\Config` is extended to search the rules for our custom presets and merge the rules if they are found.\n\nTo ensure the rules stay in sync an automated test formats the entire Laravel framework and compares the results.  If an existing Laravel file does not match our rule set the build is failed.\n\n## Releases\n\nWhen Laravel changes the code style a new major release is created for this package. You will need to edit the version constraint in your `composer.json` to pull in the updated rules. If you would like your code style to match a previous version of Laravel you may pull in an older release of this package.\n\n Laravel  | Code Style\n:---------|:----------\n 5.x      | [0.4.x](https://github.com/matt-allan/laravel-code-style/tree/0.4.0)\n 6.x-7.x  | [0.5.x](https://github.com/matt-allan/laravel-code-style/tree/0.5.0)\n 8.x      | [0.6.x](https://github.com/matt-allan/laravel-code-style/tree/0.6.0)\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](./.github/CONTRIBUTING.md) for details.\n\n## Credits\n\n- [Matt Allan](https://github.com/matt-allan)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-allan%2Flaravel-code-style","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatt-allan%2Flaravel-code-style","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-allan%2Flaravel-code-style/lists"}