framesnpictures / el-code-format
PHP-CS-Fixer configuration replicating Laravel Pint, with custom rules
Requires
- php: >=8.2
- friendsofphp/php-cs-fixer: ^3.64
Requires (Dev)
- laravel/pint: ^1.29
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP-CS-Fixer configuration that formats code the same way as
Laravel Pint, using the laravel preset plus the FramesNPictures rules. It also
adds custom rules that Pint can't run.
| Rule | Risky | Description |
|---|---|---|
Fnp/ordered_int_backed_enum_cases |
yes | Sorts the cases of int backed enums by value. |
Installation
composer require --dev framesnpictures/el-code-format
Create .php-cs-fixer.dist.php in the project root:
<?php use Fnp\CodeFormat\Config; return new Config;
Then run vendor/bin/php-cs-fixer fix, or vendor/bin/php-cs-fixer check in CI.
Configuration
Config is a PhpCsFixer\Config with the following settings already applied:
- Rules: the Pint
laravelpreset (Preset::laravel()), then the FramesNPictures rules on top (Preset::fnp()), which also turn on the custom rules. The FramesNPictures rules replace whole preset rules, the same way Pint mergespint.json. - Finder: the current working directory, skipping the same files as Pint:
vendor,bootstrap/cache,build,node_modules,storage, IDE helper files, Blade templates and dot files. - Other settings: risky rules allowed, cache on, parallel runs, and unsupported PHP versions allowed.
Override rules or the path:
return (new Config(path: __DIR__ . '/src')) ->mergeRules([ 'Fnp/ordered_int_backed_enum_cases' => ['direction' => 'descending'], 'concat_space' => ['spacing' => 'none'], ]);
To change the finder, call getFinder() (for example $config->getFinder()->exclude('tests/Fixtures')) or replace it
with setFinder().
Pint parity
tests/PintParityTest.php formats the fixtures in tests/Fixtures/parity with Pint, configured by
tests/Fixtures/pint.json, and with this config, and expects the same output. When Pint changes its preset, refresh
Preset::laravel() from Pint's resources/presets/laravel.php.
The FramesNPictures rules include new_with_braces, which php-cs-fixer has deprecated, so every run prints a
deprecation notice. The rule stays in so the output matches Pint.
Fnp/ordered_int_backed_enum_cases
Orders the cases of int backed enums by their value.
enum Flag: int
{
- case Write = 1 << 1;
- case Read = 1 << 0; // r
case None = 0;
+ case Read = 1 << 0; // r
+ case Write = 1 << 1;
}
- Only the positions taken by cases change. Constants, trait imports and methods stay where they are.
- Comments, docblocks and attributes in front of a case move with it, and so does a comment on the same line after it.
- The fixer works out case values written as integer literals (decimal, hex, octal, binary and
_separators) or as constant expressions built from them with( ) - + ~ * % << >> & ^ |. If any case refers to something else, such asself::OTHERorFoo::BAR, the whole enum is skipped. - Cases with the same value keep their original order.
- String backed and pure enums are ignored.
- The rule is risky because it changes the order of
Enum::cases().
Options
| Option | Values | Default |
|---|---|---|
direction |
ascending, descending |
ascending |
'Fnp/ordered_int_backed_enum_cases' => ['direction' => 'descending'],