Skip to content

Commit c1257da

Browse files
committed
feat: add PHP-CS-Fixer Config and Rules classes for shared configuration
Provides a proper API for sharing php-cs-fixer config across projects, replacing the hacky vendor path require approach.
1 parent e4e3e4e commit c1257da

File tree

4 files changed

+108
-67
lines changed

4 files changed

+108
-67
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace IxDFCodingStandard\PhpCsFixer;
4+
5+
use PhpCsFixer\Config as BaseConfig;
6+
use PhpCsFixer\Finder;
7+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
8+
9+
/**
10+
* Pre-configured PHP-CS-Fixer config factory for IxDF projects.
11+
*
12+
* @see README.md for usage examples
13+
*/
14+
final class Config
15+
{
16+
/**
17+
* @param array<string, mixed> $ruleOverrides Rules to merge on top of the shared ruleset
18+
*/
19+
public static function create(
20+
string $projectDir,
21+
array $ruleOverrides = [],
22+
?Finder $finder = null,
23+
): BaseConfig {
24+
$finder ??= self::defaultFinder($projectDir);
25+
26+
return (new BaseConfig())
27+
->setParallelConfig(ParallelConfigFactory::detect())
28+
->setUsingCache(true)
29+
->setCacheFile($projectDir.'/.cache/.php-cs-fixer.cache')
30+
->setRiskyAllowed(true)
31+
->setIndent(' ')
32+
->setLineEnding("\n")
33+
->setRules(array_merge(Rules::get(), $ruleOverrides))
34+
->setFinder($finder);
35+
}
36+
37+
private static function defaultFinder(string $projectDir): Finder
38+
{
39+
return Finder::create()
40+
->in($projectDir)
41+
->exclude([
42+
'.cache',
43+
'.docker',
44+
'bootstrap/cache',
45+
'node_modules',
46+
'public',
47+
'storage',
48+
'vendor',
49+
])
50+
->name('*.php')
51+
->notName('*.blade.php')
52+
->notName('_ide_helper.php')
53+
->notName('.phpstorm.meta.php')
54+
->ignoreDotFiles(false)
55+
->ignoreVCS(true)
56+
->ignoreVCSIgnored(true);
57+
}
58+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace IxDFCodingStandard\PhpCsFixer;
4+
5+
/**
6+
* Shared PHP-CS-Fixer rules for IxDF projects.
7+
*
8+
* @see https://mlocati.github.io/php-cs-fixer-configurator/
9+
*/
10+
final class Rules
11+
{
12+
/** @var array<string, mixed>|null */
13+
private static ?array $rules = null;
14+
15+
/** @return array<string, mixed> */
16+
public static function get(): array
17+
{
18+
return self::$rules ??= require dirname(__DIR__, 2).'/.php-cs-fixer-rules.php';
19+
}
20+
}

README.md

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,107 +3,70 @@
33
[![PHP Psalm Level](https://shepherd.dev/github/InteractionDesignFoundation/coding-standard/level.svg)](https://shepherd.dev/github/InteractionDesignFoundation/coding-standard)
44
[![PHP Psalm Type Coverage](https://shepherd.dev/github/InteractionDesignFoundation/coding-standard/coverage.svg)](https://shepherd.dev/github/InteractionDesignFoundation/coding-standard)
55

6-
# IxDF Coding Standard for Laravel
6+
# IxDF Coding Standard
77

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.
109

1110
## Installation
1211

13-
1. Install the package via composer by running:
1412
```shell
1513
composer require --dev interaction-design-foundation/coding-standard
1614
```
1715

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
2517

26-
3. Create file `phpcs.xml` on the base path of your repository with content
18+
Create `phpcs.xml` in your project root:
2719
```xml
2820
<?xml version="1.0"?>
2921
<ruleset name="My Coding Standard">
30-
<!-- Include all rules from the IxDF Coding Standard -->
3122
<rule ref="IxDFCodingStandard"/>
32-
33-
<!-- Paths to check -->
3423
<file>app</file>
3524
<file>config</file>
3625
<file>database</file>
37-
<file>lang</file>
3826
<file>routes</file>
3927
<file>tests</file>
4028
</ruleset>
4129
```
4230

43-
## Usage
31+
## PHP-CS-Fixer
4432

45-
- To run checks only:
33+
Create `.php-cs-fixer.php` in your project root:
4634

47-
```shell
48-
composer cs:check
49-
```
35+
```php
36+
<?php declare(strict_types=1);
5037

51-
- To automatically fix many CS issues:
38+
use IxDFCodingStandard\PhpCsFixer\Config;
5239

53-
```shell
54-
composer cs:fix
40+
return Config::create(__DIR__);
5541
```
5642

57-
## Ignoring parts of a File
58-
59-
Disable parts of a file:
43+
With rule overrides:
6044

6145
```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+
]);
6749
```
6850

69-
Disable a specific rule:
51+
With a custom Finder:
7052

7153
```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;
7956

80-
Ignore a specific violation:
57+
$finder = Finder::create()->in(__DIR__)->name('*.php');
8158

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);
8860
```
8961

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+
```
9866

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
10168

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+
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "interaction-design-foundation/coding-standard",
3-
"description": "IxDF Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.",
3+
"description": "IxDF coding standard: PHP_CodeSniffer rules and shared PHP-CS-Fixer configuration.",
44
"license": "MIT",
55
"type": "phpcodesniffer-standard",
66
"require": {
77
"php": "^8.3",
88
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
9+
"friendsofphp/php-cs-fixer": "^3.89",
910
"slevomat/coding-standard": "^8.25",
1011
"squizlabs/php_codesniffer": "^4.0"
1112
},
1213
"require-dev": {
13-
"friendsofphp/php-cs-fixer": "^3.89",
1414
"phpunit/phpunit": "^12.4",
1515
"vimeo/psalm": "^6.13"
1616
},

0 commit comments

Comments
 (0)