- Notifications
You must be signed in to change notification settings - Fork1
🏭 A factory for custom rulesets for PHP CS Fixer.
License
NexusPHP/cs-config
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This library provides a factory for custom rulesets forfriendsofphp/php-cs-fixer.
You can add this library as a local, per-project dependency to your projectusingComposer:
composer require nexusphp/cs-configIf you only need this library during development, for instance to run your project's test suite,then you should add it as a development-time dependency:
composer require --dev nexusphp/cs-config- Create a
.php-cs-fixer.dist.phpat the root of your project:
<?phpuseNexus\CsConfig\Factory;useNexus\CsConfig\Ruleset\Nexus82;return Factory::create(newNexus82())->forProjects();
- Include the cache file in your
.gitignore. Bydefault, the cache file will be saved in the project root.
vendor/+# php-cs-fixer+.php-cs-fixer.php+.php-cs-fixer.cache
You can create a preformatted license header to all PHP files by using the publicforLibrary() methodinstead offorProjects(). This method accepts two required arguments (the library name and author) andtwo optional arguments (the email address and starting year of license).
- Scenario 1: Providing all arguments
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82;-return Factory::create(new Nexus82())->forProjects();+return Factory::create(new Nexus82())->forLibrary('My Library', 'John Doe', 'john@doe.com', 2020);
This setting will configure a license header similar to below:
<?php/** * This file is part of My Library. * * (c) 2020 John Doe <john@doe.com> * * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */namespaceNexus\CsConfig;
- Scenario 2: Providing only the required arguments
If you opted not to provide any of the optional arguments (i.e., email address, starting license year),these will not be shown on the license header allowing flexibility on the copyright portion.
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82;-return Factory::create(new Nexus82())->forProjects();+return Factory::create(new Nexus82())->forLibrary('My Library', 'John Doe');
This will give the following license header:
<?php/** * This file is part of My Library. * * (c) John Doe * * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */namespaceNexus\CsConfig;
If you feel that a specific rule in the ruleset is not appropriate for you, you can override it instead of creating a new ruleset:
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82;-return Factory::create(new Nexus82())->forProjects();+return Factory::create(new Nexus82(), [+ 'binary_operator_spaces' => false,+])->forProjects();
TheFactory class returns an instance ofPhpCsFixer\Config and fully supports all ofits properties setup. You can pass an array to the third parameter ofFactory::create()containing your desired options.
Options
| Key | Allowed Types | Default |
|---|---|---|
| cacheFile | string | .php-cs-fixer.cache |
| customFixers | FixerInterface[], iterable, \Traversable | [] |
| finder | iterable, string[], \Traversable | defaultPhpCsFixer\Finder instance |
| format | string | txt |
| hideProgress | bool | false |
| indent | string | ' ' // 4 spaces |
| lineEnding | string | "\n" |
| isRiskyAllowed | bool | false |
| usingCache | bool | true |
| customRules | array | [] |
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82;-return Factory::create(new Nexus82())->forProjects();+return Factory::create(new Nexus82(), [], [+ 'usingCache' => false,+ 'hideProgress => true,+])->forProjects();
What is the purpose of a configuration factory if not able to create a custom ruleset foran organization-wide usage, right? Well, you are not constrained to use the default rulesetsand putting a long array of overrides. That's pretty nasty.
The way to achieve this is dependent on you but the main idea is creating a new ruleset thatextendsNexus\CsConfig\Ruleset\AbstractRuleset. Yup, it's that easy. Then you just need toprovide details for its required four (4) protected properties.
<?phpnamespaceMyCompany\CodingStandards\Ruleset;useNexus\CsConfig\Ruleset\AbstractRuleset;finalclass MyCompanyextends AbstractRuleset{publicfunction__construct() {$this->name ='My Company';$this->rules = ['@PSR2' =>true, ... ];$this->requiredPHPVersion =80200;$this->autoActivateIsRiskyAllowed =true; }}
Then, in creating your.php-cs-fixer.dist.php, use your own ruleset.
<?phpuseNexus\CsConfig\Factory;useMyCompany\CodingStandards\Ruleset\MyCompany;return Factory::create(newMyCompany())->forProjects();
This project is inspired by and an enhancement ofergebnis/php-cs-fixer-config.
Contributions are very much welcome. If you see an improvement or bugfix, open aPR now!
About
🏭 A factory for custom rulesets for PHP CS Fixer.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.