-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathcoding-standard.texy
More file actions
83 lines (54 loc) · 2.51 KB
/
coding-standard.texy
File metadata and controls
83 lines (54 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Nette Coding Standard
*********************
.[perex]
[Coding Standard |https://github.com/nette/coding-standard] automatically formats your PHP code according to the [Nette coding standard |contributing:coding-standard], from indentation and braces to spacing and the ordering of imports.
Under the hood it combines two industry-standard tools, PHP CS Fixer and PHP CodeSniffer, which come preconfigured, so you do not have to set anything up yourself.
Installation
============
Install the tool globally using Composer:
```shell
composer global require nette/coding-standard
```
and make sure your global vendor binaries directory is in [your `$PATH` environment variable|https://getcomposer.org/doc/03-cli.md#global].
Usage
=====
The tool provides two commands. The `check` command only reports violations, while `fix` repairs them automatically:
```shell
ecs check
ecs fix
```
Without any arguments it scans the `src/` and `tests/` directories; you can also pass one or more of your own paths, for example `ecs check app bin`.
Before you run the fixer for the first time, be sure to back up your files.
PHP Version
===========
The tool supports PHP 8.0 to 8.5 and applies the rules incrementally according to the version. It detects the version automatically from your project's `composer.json`, but you can also set it explicitly with the `--preset` option:
```shell
ecs check --preset php81
```
Custom Rules
============
You can tweak the rules for your project. To change the PHP CS Fixer rules, create an `ncs.php` file in the project root:
```php
<?php
return [
'strict_comparison' => false,
];
```
To change the PHP CodeSniffer rules, create an `ncs.xml` file. It does not need to reference the version preset (it is combined with it automatically), and you can use `$presets/` to pull in any bundled preset:
```xml
<?xml version="1.0"?>
<ruleset name="MyProject">
<!-- enable use function/const imports -->
<rule ref="$presets/optimize-fn.xml"/>
<!-- disable a rule -->
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint"/>
</ruleset>
```
Both files are discovered automatically and your rules take precedence over the preset. For a one-off override that you do not want to commit, pass an extra file with `--config-file` (`.php` for the fixer, `.xml` for the sniffer).
Continuous Integration
======================
In a CI pipeline, install the tool as a project and run it from there:
```yaml
- run: composer create-project nette/coding-standard temp/coding-standard
- run: php temp/coding-standard/ecs check
```