|
3 | 3 | [](https://shepherd.dev/github/InteractionDesignFoundation/coding-standard) |
4 | 4 | [](https://shepherd.dev/github/InteractionDesignFoundation/coding-standard) |
5 | 5 |
|
6 | | -# IxDF Coding Standard for Laravel |
| 6 | +# IxDF Coding Standard |
7 | 7 |
|
8 | | -An opinionated ruleset focused on strict types. |
9 | | -Suitable for both applications and packages. |
| 8 | +An opinionated coding standard for PHP/Laravel projects. Provides two independent tools — use either one or both together: |
| 9 | + |
| 10 | +- **PHP_CodeSniffer** — custom sniffs for strict types and Laravel conventions |
| 11 | +- **PHP-CS-Fixer** — shared config with 80+ rules based on PER-CS 3.0 |
10 | 12 |
|
11 | 13 | ## Installation |
12 | 14 |
|
13 | | -1. Install the package via composer by running: |
14 | 15 | ```shell |
15 | 16 | composer require --dev interaction-design-foundation/coding-standard |
16 | 17 | ``` |
17 | 18 |
|
18 | | -2. Add composer scripts into your `composer.json`: |
19 | | -```json |
20 | | -"scripts": { |
21 | | - "cs:check": "phpcs -p -s --colors --report-full --report-summary", |
22 | | - "cs:fix": "phpcbf -p --colors" |
23 | | -} |
24 | | -``` |
| 19 | +## PHP_CodeSniffer |
25 | 20 |
|
26 | | -3. Create file `phpcs.xml` on the base path of your repository with content |
| 21 | +Create `phpcs.xml` in your project root: |
27 | 22 | ```xml |
28 | 23 | <?xml version="1.0"?> |
29 | 24 | <ruleset name="My Coding Standard"> |
30 | | - <!-- Include all rules from the IxDF Coding Standard --> |
31 | 25 | <rule ref="IxDFCodingStandard"/> |
32 | | - |
33 | | - <!-- Paths to check --> |
34 | 26 | <file>app</file> |
35 | 27 | <file>config</file> |
36 | 28 | <file>database</file> |
37 | | - <file>lang</file> |
38 | 29 | <file>routes</file> |
39 | 30 | <file>tests</file> |
40 | 31 | </ruleset> |
41 | 32 | ``` |
42 | 33 |
|
43 | | -## Usage |
| 34 | +## PHP-CS-Fixer |
44 | 35 |
|
45 | | -- To run checks only: |
| 36 | +Create `.php-cs-fixer.php` in your project root: |
46 | 37 |
|
47 | | -```shell |
48 | | -composer cs:check |
49 | | -``` |
| 38 | +```php |
| 39 | +<?php declare(strict_types=1); |
50 | 40 |
|
51 | | -- To automatically fix many CS issues: |
| 41 | +use IxDFCodingStandard\PhpCsFixer\Config; |
52 | 42 |
|
53 | | -```shell |
54 | | -composer cs:fix |
| 43 | +return Config::create(__DIR__); |
55 | 44 | ``` |
56 | 45 |
|
57 | | -## Ignoring parts of a File |
58 | | - |
59 | | -Disable parts of a file: |
| 46 | +With rule overrides: |
60 | 47 |
|
61 | 48 | ```php |
62 | | -$xmlPackage = new XMLPackage; |
63 | | -// phpcs:disable |
64 | | -$xmlPackage['error_code'] = get_default_error_code_value(); |
65 | | -$xmlPackage->send(); |
66 | | -// phpcs:enable |
| 49 | +return Config::create(__DIR__, ruleOverrides: [ |
| 50 | + 'final_public_method_for_abstract_class' => false, |
| 51 | +]); |
67 | 52 | ``` |
68 | 53 |
|
69 | | -Disable a specific rule: |
| 54 | +With a custom Finder: |
70 | 55 |
|
71 | 56 | ```php |
72 | | -// phpcs:disable Generic.Commenting.Todo.Found |
73 | | -$xmlPackage = new XMLPackage; |
74 | | -$xmlPackage['error_code'] = get_default_error_code_value(); |
75 | | -// TODO: Add an error message here. |
76 | | -$xmlPackage->send(); |
77 | | -// phpcs:enable |
78 | | -``` |
| 57 | +use IxDFCodingStandard\PhpCsFixer\Config; |
| 58 | +use PhpCsFixer\Finder; |
79 | 59 |
|
80 | | -Ignore a specific violation: |
| 60 | +$finder = Finder::create()->in(__DIR__)->name('*.php'); |
81 | 61 |
|
| 62 | +return Config::create(__DIR__, finder: $finder); |
| 63 | +``` |
| 64 | + |
| 65 | +If you only need the rules array: |
82 | 66 | ```php |
83 | | -$xmlPackage = new XMLPackage; |
84 | | -$xmlPackage['error_code'] = get_default_error_code_value(); |
85 | | -// phpcs:ignore Generic.Commenting.Todo.Found |
86 | | -// TODO: Add an error message here. |
87 | | -$xmlPackage->send(); |
| 67 | +$rules = \IxDFCodingStandard\PhpCsFixer\Rules::get(); |
88 | 68 | ``` |
89 | 69 |
|
90 | | -## Development |
| 70 | +## Usage |
| 71 | + |
| 72 | +```shell |
| 73 | +vendor/bin/phpcs # check with PHP_CodeSniffer |
| 74 | +vendor/bin/phpcbf # fix with PHP_CodeSniffer |
| 75 | +vendor/bin/php-cs-fixer fix --dry-run --diff # check with PHP-CS-Fixer |
| 76 | +vendor/bin/php-cs-fixer fix # fix with PHP-CS-Fixer |
| 77 | +``` |
91 | 78 |
|
92 | | -### Versioning |
93 | | -> **New rules or Sniffs may not be introduced in minor or bugfix releases and should always be based on the develop |
94 | | -branch and queued for the next major release, unless considered a bugfix for existing rules.** |
| 79 | +### Composer scripts (recommended) |
95 | 80 |
|
| 81 | +Add to your `composer.json`: |
96 | 82 |
|
97 | | -## Reference |
| 83 | +```json |
| 84 | +"scripts": { |
| 85 | + "cs": "@cs:fix", |
| 86 | + "cs:check": ["@php-cs-fixer:dry", "@phpcs"], |
| 87 | + "cs:fix": ["@php-cs-fixer", "@phpcbf"], |
| 88 | + "phpcs": "phpcs -p -s --colors --report-full --report-summary", |
| 89 | + "phpcbf": "phpcbf -p --colors", |
| 90 | + "php-cs-fixer": "php-cs-fixer fix --no-interaction --ansi --quiet", |
| 91 | + "php-cs-fixer:dry": "php-cs-fixer fix --no-interaction --ansi --verbose --dry-run" |
| 92 | +} |
| 93 | +``` |
98 | 94 |
|
99 | | -Rules can be added, excluded or tweaked locally, depending on your preferences. |
100 | | -More information on how to do this can be found here: |
| 95 | +Then run: |
101 | 96 |
|
102 | | -- [Coding Standard Tutorial](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial) |
103 | | -- [Configuration Options](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options) |
104 | | -- [Selectively Applying Rules](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset#selectively-applying-rules) |
105 | | -- [Customisable Sniff Properties](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties) |
106 | | -- Other coding standards (inspiring us): |
107 | | - - [Slevomat coding standard](https://github.com/slevomat/coding-standard) |
108 | | - - [Doctrine coding standard](https://github.com/doctrine/coding-standard) |
109 | | - - [Laminas coding standard](https://github.com/laminas/laminas-coding-standard) |
| 97 | +```shell |
| 98 | +composer cs:check # run both tools in check mode |
| 99 | +composer cs:fix # run both tools in fix mode |
| 100 | +composer phpcs # PHP_CodeSniffer only |
| 101 | +composer php-cs-fixer # PHP-CS-Fixer only |
| 102 | +``` |
0 commit comments