|
1 | | -override |
| 1 | +# The Easiest way to use Coding Standard |
| 2 | + |
| 3 | +[](https://packagist.org/packages/symplify/easy-coding-standard/stats) |
| 4 | + |
| 5 | +Easy Coding Standard (ECS) is a single tool that runs [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) and [PHP-CS-Fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) rules together, configured through one simple PHP file. |
| 6 | + |
| 7 | +> [!NOTE] |
| 8 | +> This is the **deployed** package. Development happens in the [symplify/easy-coding-standard](https://github.com/easy-coding-standard/easy-coding-standard) repository - report issues and send pull requests there. |
| 9 | +
|
| 10 | +<br> |
| 11 | + |
| 12 | +## Killer Features |
| 13 | + |
| 14 | +- Install on **any PHP 7.2-PHP 8.5** project with any dependencies |
| 15 | +- Blazing fast with parallel run out of the box |
| 16 | +- Use [PHP_CodeSniffer or PHP-CS-Fixer](https://tomasvotruba.com/blog/2017/05/03/combine-power-of-php-code-sniffer-and-php-cs-fixer-in-3-lines/) - anything you like |
| 17 | +- Use **prepared sets** and [PHP CS Fixer sets](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst) to save time |
| 18 | + |
| 19 | +<br> |
| 20 | + |
| 21 | +## Install |
| 22 | + |
| 23 | +```bash |
| 24 | +composer require symplify/easy-coding-standard --dev |
| 25 | +``` |
| 26 | + |
| 27 | +<br> |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +```bash |
| 32 | +vendor/bin/ecs |
| 33 | +``` |
| 34 | + |
| 35 | +On the first run, ECS creates `ecs.php` config file with directories and first rule to kick off. |
| 36 | + |
| 37 | +Then you can run again to see the suggested diffs: |
| 38 | + |
| 39 | +```bash |
| 40 | +vendor/bin/ecs |
| 41 | +``` |
| 42 | + |
| 43 | +To actually **fix your code**, add `--fix`: |
| 44 | + |
| 45 | +```bash |
| 46 | +vendor/bin/ecs --fix |
| 47 | +``` |
| 48 | + |
| 49 | +That's it! |
| 50 | + |
| 51 | +<br> |
| 52 | + |
| 53 | +## Configure |
| 54 | + |
| 55 | +Most of the time, you'll be happy with the default configuration. The most relevant part is configuring paths, checkers and sets: |
| 56 | + |
| 57 | +```php |
| 58 | +use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer; |
| 59 | +use PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer; |
| 60 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 61 | + |
| 62 | +return ECSConfig::configure() |
| 63 | + ->withPaths([__DIR__ . '/src', __DIR__ . '/tests']) |
| 64 | + |
| 65 | + // start slow with sole rules |
| 66 | + ->withRules([ |
| 67 | + ListSyntaxFixer::class, |
| 68 | + ]) |
| 69 | + ->withConfiguredRule( |
| 70 | + ArraySyntaxFixer::class, |
| 71 | + ['syntax' => 'long'] |
| 72 | + ) |
| 73 | + |
| 74 | + // apply full set |
| 75 | + ->withPreparedSets(psr12: true); |
| 76 | +``` |
| 77 | + |
| 78 | +<br> |
| 79 | + |
| 80 | +Do you want to check all `*.php` files in your root (`ecs.php`, `rector.php` etc.)? Instead of listing them one by one, use `->withRootFiles()` method: |
| 81 | + |
| 82 | +```php |
| 83 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 84 | + |
| 85 | +return ECSConfig::configure() |
| 86 | + ->withPaths([__DIR__ . '/src', __DIR__ . '/tests']) |
| 87 | + ->withRootFiles(); |
| 88 | +``` |
| 89 | + |
| 90 | +<br> |
| 91 | + |
| 92 | +Do you want to include one of sets from [php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst)? |
| 93 | + |
| 94 | +You can: |
| 95 | + |
| 96 | +```php |
| 97 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 98 | + |
| 99 | +return ECSConfig::configure() |
| 100 | + ->withPaths([__DIR__ . '/src', __DIR__ . '/tests']) |
| 101 | + ->withPhpCsFixerSets(perCS20: true, doctrineAnnotation: true); |
| 102 | +``` |
| 103 | + |
| 104 | +<br> |
| 105 | + |
| 106 | +### Gradual Adoption with Levels |
| 107 | + |
| 108 | +Want to adopt a coding standard step by step instead of all at once? Use `with*Level()` methods to start from the safest rules and raise the level as your codebase catches up: |
| 109 | + |
| 110 | +```php |
| 111 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 112 | + |
| 113 | +return ECSConfig::configure() |
| 114 | + ->withPaths([__DIR__ . '/src', __DIR__ . '/tests']) |
| 115 | + ->withSpacesLevel(0) |
| 116 | + ->withArrayLevel(0) |
| 117 | + ->withControlStructuresLevel(0) |
| 118 | + ->withDocblockLevel(0); |
| 119 | +``` |
| 120 | + |
| 121 | +Each level enables the first N+1 rules from a curated list, ordered from safest to most invasive. Bump the number once your codebase is clean on the current level. |
| 122 | + |
| 123 | +<br> |
| 124 | + |
| 125 | +### How to Skip Files/Rules? |
| 126 | + |
| 127 | +Love the sets of rules, but want to skip single rule or some files? |
| 128 | + |
| 129 | +```php |
| 130 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 131 | + |
| 132 | +return ECSConfig::configure() |
| 133 | + ->withSkip([ |
| 134 | + // skip single rule |
| 135 | + ArraySyntaxFixer::class, |
| 136 | + |
| 137 | + // skip single rule in specific paths |
| 138 | + ArraySyntaxFixer::class => [ |
| 139 | + __DIR__ . '/src/ValueObject/', |
| 140 | + ], |
| 141 | + |
| 142 | + // skip directory by absolute or * mask |
| 143 | + __DIR__ . '/src/Migrations', |
| 144 | + |
| 145 | + // skip directories by mask |
| 146 | + __DIR__ . '/src/*/Legacy', |
| 147 | + ]); |
| 148 | +``` |
| 149 | + |
| 150 | +<br> |
| 151 | + |
| 152 | +### Less Common Options |
| 153 | + |
| 154 | +You probably won't use these, but they can give you more control over the internal process: |
| 155 | + |
| 156 | +```php |
| 157 | +use Symplify\EasyCodingStandard\Config\ECSConfig; |
| 158 | +use Symplify\EasyCodingStandard\ValueObject\Option; |
| 159 | + |
| 160 | +return ECSConfig::configure() |
| 161 | + // file extensions to scan |
| 162 | + ->withFileExtensions(['php']) |
| 163 | + |
| 164 | + // configure cache paths and namespace - useful e.g. Gitlab CI caching, where getcwd() produces always different path |
| 165 | + ->withCache( |
| 166 | + directory: sys_get_temp_dir() . '/_changed_files_detector_tests', |
| 167 | + namespace: getcwd() // normalized to directory separator |
| 168 | + ) |
| 169 | + |
| 170 | + // print contents with specific indent rules |
| 171 | + ->withSpacing(indentation: Option::INDENTATION_SPACES, lineEnding: PHP_EOL) |
| 172 | + |
| 173 | + // modify parallel run |
| 174 | + ->withParallel(timeoutSeconds: 120, maxNumberOfProcess: 32, jobSize: 20); |
| 175 | +``` |
| 176 | + |
| 177 | +Mentioned values are default ones. |
| 178 | + |
| 179 | +<br> |
| 180 | + |
| 181 | +### Controlling Output Format |
| 182 | + |
| 183 | +You may want to use ECS to generate reports for third-party tooling. |
| 184 | + |
| 185 | +We currently provide formatters for: |
| 186 | + |
| 187 | +- `console`: Human-oriented printing à la PHP CS Fixer. |
| 188 | +- `json`: A custom JSON blob for arbitrary tooling. |
| 189 | +- `junit`: JUnit format to be used in different CI environments. |
| 190 | +- `checkstyle`: Useful for Github Action Reports. |
| 191 | +- `gitlab`: For Gitlab code quality reports or Code Climate tooling. |
| 192 | + |
| 193 | +You can use the output format option as below |
| 194 | + |
| 195 | +```bash |
| 196 | +vendor/bin/ecs --output-format=checkstyle |
| 197 | +``` |
| 198 | + |
| 199 | +<br> |
| 200 | + |
| 201 | +## FAQ |
| 202 | + |
| 203 | +### How do I clear cache? |
| 204 | + |
| 205 | +```bash |
| 206 | +vendor/bin/ecs --clear-cache |
| 207 | +``` |
| 208 | + |
| 209 | +### How can I see all used rules? |
| 210 | + |
| 211 | +```bash |
| 212 | +vendor/bin/ecs list-checkers |
| 213 | +``` |
| 214 | + |
| 215 | +Do you look for json format? |
| 216 | + |
| 217 | +```bash |
| 218 | +vendor/bin/ecs list-checkers --output-format json |
| 219 | +``` |
| 220 | + |
| 221 | +<br> |
| 222 | + |
| 223 | +### Can I Use My [`.editorconfig`](https://editorconfig.org/)? |
| 224 | + |
| 225 | +Mostly! By using `->withEditorConfig()` in `ecs.php`, ECS will automatically discover |
| 226 | +the `.editorconfig` file in the project's root directory. It will use any |
| 227 | +rules under `[*]` or `[*.php]` (the latter taking priority) and respect the |
| 228 | +settings for: |
| 229 | + |
| 230 | +- `indent_style` |
| 231 | +- `end_of_line` |
| 232 | +- `max_line_length` |
| 233 | +- `trim_trailing_whitespace` |
| 234 | +- `insert_final_newline` |
| 235 | +- [`quote_type`](https://github.com/jednano/codepainter#quote_type-single-double-auto) |
| 236 | + - Only `single` and `auto` are respected. |
| 237 | + - Warning: this is a proposed field, but not fully standard. |
| 238 | + |
| 239 | +These settings will take precedence over similar rules configured through sets |
| 240 | +like PSR12, to avoid conflicting with other tooling using your `.editorconfig`. |
| 241 | + |
| 242 | +<br> |
| 243 | + |
| 244 | +## How to Migrate from another coding standard tool? |
| 245 | + |
| 246 | +Do you use another tool and want to migrate? It's pretty straightforward - here is "how to": |
| 247 | + |
| 248 | +* for [PHP_CodeSniffer](https://tomasvotruba.com/blog/2018/06/04/how-to-migrate-from-php-code-sniffer-to-easy-coding-standard) |
| 249 | +* and [PHP CS Fixer](https://tomasvotruba.com/blog/2018/06/07/how-to-migrate-from-php-cs-fixer-to-easy-coding-standard). |
0 commit comments