Skip to content

Commit de21d55

Browse files
committed
Extensive update of README
1 parent 7ebfbfd commit de21d55

File tree

1 file changed

+214
-2
lines changed

1 file changed

+214
-2
lines changed

README.md

Lines changed: 214 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# NexusPHP CS Config
22

3+
[![PHP version](https://img.shields.io/packagist/php-v/nexusphp/cs-config)](https://php.net)
4+
![build](https://github.com/NexusPHP/cs-config/workflows/build/badge.svg?branch=develop)
5+
[![Coverage Status](https://coveralls.io/repos/github/NexusPHP/cs-config/badge.svg?branch=develop)](https://coveralls.io/github/NexusPHP/cs-config?branch=develop)
6+
[![Latest Stable Version](https://poser.pugx.org/nexusphp/cs-config/v)](//packagist.org/packages/nexusphp/cs-config)
7+
[![license MIT](https://img.shields.io/github/license/nexusphp/cs-config)](LICENSE)
8+
39
This library provides a factory for custom rulesets for [`friendsofphp/php-cs-fixer`][1].
410

511
[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
612

7-
*This is the drop-in replacement for [Liaison CS Config](https://github.com/paulbalandan/liaison-cs-config).*
13+
*This is the drop-in replacement for [Liaison CS Config](https://github.com/paulbalandan/liaison-cs-config), but is not fully backwards compatible. See [MIGRATION.md](MIGRATION.md) for details.*
814

915
## Installation
1016

@@ -28,7 +34,7 @@ then you should add it as a development-time dependency:
2834
use Nexus\CsConfig\Factory;
2935
use Nexus\CsConfig\Ruleset\Nexus73;
3036

31-
return Factory::create(new Nexus73());
37+
return Factory::create(new Nexus73())->forProjects();
3238

3339
```
3440

@@ -42,3 +48,209 @@ vendor/
4248
+.php_cs
4349
+.php_cs.cache
4450
```
51+
52+
## Advanced Configuration
53+
54+
### Adding Preformatted License Header
55+
56+
You can create a preformatted license header to all PHP files by using the public `forLibrary()` method
57+
instead of `forProjects()`. This method accepts two required arguments (the library name and author) and
58+
two optional arguments (the email address and starting year of license).
59+
60+
* Scenario 1: Providing all arguments
61+
```diff
62+
<?php
63+
64+
use Nexus\CsConfig\Factory;
65+
use Nexus\CsConfig\Ruleset\Nexus73;
66+
67+
-return Factory::create(new Nexus73())->forProjects();
68+
+return Factory::create(new Nexus73())->forLibrary('My Library', 'John Doe', '[email protected]', 2020);
69+
```
70+
71+
This setting will configure a license header similar to below:
72+
73+
```php
74+
<?php
75+
76+
/**
77+
* This file is part of My Library.
78+
*
79+
* (c) 2020 John Doe <john@doe.com>
80+
*
81+
* For the full copyright and license information, please view the LICENSE
82+
* file that was distributed with this source code.
83+
*/
84+
85+
namespace Nexus\CsConfig;
86+
```
87+
88+
* Scenario 2: Providing only the required arguments
89+
90+
If you opted not to provided any of the optional arguments (i.e., email address, starting license year),
91+
these will not be shown on the license header allowing flexibility on the copyright portion.
92+
93+
```diff
94+
<?php
95+
96+
use Nexus\CsConfig\Factory;
97+
use Nexus\CsConfig\Ruleset\Nexus73;
98+
99+
-return Factory::create(new Nexus73())->forProjects();
100+
+return Factory::create(new Nexus73())->forLibrary('My Library', 'John Doe');
101+
```
102+
103+
This will give the following license header:
104+
105+
```php
106+
<?php
107+
108+
/**
109+
* This file is part of My Library.
110+
*
111+
* (c) John Doe
112+
*
113+
* For the full copyright and license information, please view the LICENSE
114+
* file that was distributed with this source code.
115+
*/
116+
117+
namespace Nexus\CsConfig;
118+
```
119+
120+
### Overriding Rules in a Ruleset
121+
122+
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:
123+
124+
```diff
125+
<?php
126+
127+
use Nexus\CsConfig\Factory;
128+
use Nexus\CsConfig\Ruleset\Nexus73;
129+
130+
-return Factory::create(new Nexus73())->forProjects();
131+
+return Factory::create(new Nexus73(), [
132+
+ 'binary_operator_spaces' => false,
133+
+])->forProjects();
134+
135+
```
136+
137+
### Specifying Options to `PhpCsFixer\Config`
138+
139+
The `Factory` class returns an instance of `PhpCsFixer\Config` and fully supports all of
140+
its properties setup. You can pass an array to the third parameter of `Factory::create()`
141+
containing your desired options.
142+
143+
**Options**
144+
145+
| Key | Allowed Types | Default |
146+
| -------------- | :--------------------------------------: | :----------------------------------: |
147+
| cacheFile | `string` | `.php_cs.cache` |
148+
| customFixers | `FixerInterface[], iterable, \Traversable` | `[]` |
149+
| finder | `iterable, string[], \Traversable` | default `PhpCsFixer\Finder` instance |
150+
| format | `string` | `txt` |
151+
| hideProgress | `bool` | `false` |
152+
| indent | `string` | `' '` // 4 spaces |
153+
| lineEnding | `string` | `"\n"` |
154+
| phpExecutable | `null, string` | `null` |
155+
| isRiskyAllowed | `bool` | `false` |
156+
| usingCache | `bool` | `true` |
157+
| customRules | `array` | `[]` |
158+
159+
```diff
160+
<?php
161+
162+
use Nexus\CsConfig\Factory;
163+
use Nexus\CsConfig\Ruleset\Nexus73;
164+
165+
-return Factory::create(new Nexus73())->forProjects();
166+
+return Factory::create(new Nexus73(), [], [
167+
+ 'usingCache' => false,
168+
+ 'hideProgress => true,
169+
+])->forProjects();
170+
```
171+
172+
## Customization of Ruleset
173+
174+
What is the purpose of a configuration factory if not able to create a custom ruleset for
175+
an organization-wide usage, right? Well, you are not constrained to use the default rulesets
176+
and putting a long array of overrides. That's pretty nasty.
177+
178+
The way to achieve this is dependent on you but the main idea is creating a new ruleset that
179+
extends `Nexus\CsConfig\Ruleset\AbstractRuleset`. Yup, it's that easy. Then you just need to
180+
provide details for its required four (4) protected properties.
181+
182+
```php
183+
<?php
184+
185+
namespace MyCompany\CodingStandards\Ruleset;
186+
187+
use Nexus\CsConfig\Ruleset\AbstractRuleset;
188+
189+
class MyCompany extends AbstractRuleset
190+
{
191+
/**
192+
* Name of this ruleset
193+
*
194+
* @var string
195+
*/
196+
protected $name = 'My Company';
197+
198+
/**
199+
* Your list of rules
200+
*
201+
* @var array
202+
*/
203+
protected $rules = [
204+
'@PSR2' => true,
205+
...
206+
];
207+
208+
/**
209+
* PHP_VERSION_ID that this ruleset is targeting.
210+
*
211+
* @var int
212+
*/
213+
protected $requiredPHPVersion = 70400;
214+
215+
/**
216+
* Does this ruleset have risky rules?
217+
*
218+
* If yes and `PhpCsFixer\Config` has the `$isRiskyAllowed`
219+
* flag set to `false`, those risky rules won't be run.
220+
*
221+
* Set this flag to `true` to automatically setup
222+
* the `$isRiskyAllowed` flag.
223+
*
224+
* @var bool
225+
*/
226+
protected $autoActivateIsRiskyAllowed = false;
227+
}
228+
229+
```
230+
231+
Then, in creating your `.php_cs.dist`, use your own ruleset.
232+
233+
```php
234+
<?php
235+
236+
use Nexus\CsConfig\Factory;
237+
use MyCompany\CodingStandards\Ruleset\MyCompany;
238+
239+
return Factory::create(new MyCompany())->forProjects();
240+
241+
```
242+
243+
## Migrating from Liaison CS Config Factory
244+
245+
If you are coming from the use of Liaison CS Config Factory, be warned that this is not 100% backwards
246+
compatible. Though some class methods were retained, a lot were refactored for the `Factory` class.
247+
248+
See [MIGRATION.md](MIGRATION.md) for more details.
249+
250+
## Credits
251+
252+
This project is inspired by and an enhancement of [`ergebnis/php-cs-fixer-config`](https://github.com/ergebnis/php-cs-fixer-config).
253+
254+
## Contributing
255+
256+
Contributions are very much welcome. If you see an improvement or bugfix, open a [PR](https://github.com/NexusPHP/cs-config/pulls) now!

0 commit comments

Comments
 (0)