|
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 **PHP_CodeSniffer** rules and a shared **PHP-CS-Fixer** configuration. |
10 | 9 |
|
11 | 10 | ## Installation |
12 | 11 |
|
13 | | -1. Install the package via composer by running: |
14 | 12 | ```shell |
15 | 13 | composer require --dev interaction-design-foundation/coding-standard |
16 | 14 | ``` |
17 | 15 |
|
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 | | -``` |
| 16 | +## PHP_CodeSniffer |
25 | 17 |
|
26 | | -3. Create file `phpcs.xml` on the base path of your repository with content |
| 18 | +Create `phpcs.xml` in your project root: |
27 | 19 | ```xml |
28 | 20 | <?xml version="1.0"?> |
29 | 21 | <ruleset name="My Coding Standard"> |
30 | | - <!-- Include all rules from the IxDF Coding Standard --> |
31 | 22 | <rule ref="IxDFCodingStandard"/> |
32 | | - |
33 | | - <!-- Paths to check --> |
34 | 23 | <file>app</file> |
35 | 24 | <file>config</file> |
36 | 25 | <file>database</file> |
37 | | - <file>lang</file> |
38 | 26 | <file>routes</file> |
39 | 27 | <file>tests</file> |
40 | 28 | </ruleset> |
41 | 29 | ``` |
42 | 30 |
|
43 | | -## Usage |
| 31 | +## PHP-CS-Fixer |
44 | 32 |
|
45 | | -- To run checks only: |
| 33 | +Create `.php-cs-fixer.php` in your project root: |
46 | 34 |
|
47 | | -```shell |
48 | | -composer cs:check |
49 | | -``` |
| 35 | +```php |
| 36 | +<?php declare(strict_types=1); |
50 | 37 |
|
51 | | -- To automatically fix many CS issues: |
| 38 | +use IxDFCodingStandard\PhpCsFixer\Config; |
52 | 39 |
|
53 | | -```shell |
54 | | -composer cs:fix |
| 40 | +return Config::create(__DIR__); |
55 | 41 | ``` |
56 | 42 |
|
57 | | -## Ignoring parts of a File |
58 | | - |
59 | | -Disable parts of a file: |
| 43 | +With rule overrides: |
60 | 44 |
|
61 | 45 | ```php |
62 | | -$xmlPackage = new XMLPackage; |
63 | | -// phpcs:disable |
64 | | -$xmlPackage['error_code'] = get_default_error_code_value(); |
65 | | -$xmlPackage->send(); |
66 | | -// phpcs:enable |
| 46 | +return Config::create(__DIR__, ruleOverrides: [ |
| 47 | + 'final_public_method_for_abstract_class' => false, |
| 48 | +]); |
67 | 49 | ``` |
68 | 50 |
|
69 | | -Disable a specific rule: |
| 51 | +With a custom Finder: |
70 | 52 |
|
71 | 53 | ```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 | | -``` |
| 54 | +use IxDFCodingStandard\PhpCsFixer\Config; |
| 55 | +use PhpCsFixer\Finder; |
79 | 56 |
|
80 | | -Ignore a specific violation: |
| 57 | +$finder = Finder::create()->in(__DIR__)->name('*.php'); |
81 | 58 |
|
82 | | -```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(); |
| 59 | +return Config::create(__DIR__, finder: $finder); |
88 | 60 | ``` |
89 | 61 |
|
90 | | -## Development |
91 | | - |
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.** |
95 | | - |
96 | | - |
97 | | -## Reference |
| 62 | +If you only need the rules array: |
| 63 | +```php |
| 64 | +$rules = \IxDFCodingStandard\PhpCsFixer\Rules::get(); |
| 65 | +``` |
98 | 66 |
|
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: |
| 67 | +## Usage |
101 | 68 |
|
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) |
| 69 | +```shell |
| 70 | +composer cs:check # check only |
| 71 | +composer cs:fix # auto-fix |
| 72 | +``` |
0 commit comments