From 895c4d182a558ff3eb4f4b76e5fd6ab5bfa0d3dd Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Tue, 10 Dec 2024 12:59:11 +0700 Subject: [PATCH 01/12] docs: update default config --- ecs.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ecs.php b/ecs.php index 5d8a49f..35beac3 100644 --- a/ecs.php +++ b/ecs.php @@ -4,9 +4,11 @@ use Symplify\EasyCodingStandard\Config\ECSConfig; -return static function (ECSConfig $ecsConfig): void { - $ecsConfig->paths([ - __DIR__ . '/', +return static function (ECSConfig $config): void { + $config->paths([ + 'src', + 'tests', ]); - $ecsConfig->import('config/whatwedo-common.php'); + $config->skip([]); + $config->import('vendor/whatwedo/php-coding-standard/config/whatwedo-common.php'); }; From 3dff412c64170e46d8dab14bfb34b5b02a723bf7 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Tue, 10 Dec 2024 13:00:04 +0700 Subject: [PATCH 02/12] chore: minor update to composer packages --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 12ef952..3c7ffb7 100644 --- a/composer.json +++ b/composer.json @@ -21,8 +21,8 @@ "require": { "php": ">=7.4", "kubawerlos/php-cs-fixer-custom-fixers": "^3.0", - "slevomat/coding-standard": "^8.5", - "symplify/easy-coding-standard": "^12.1" + "slevomat/coding-standard": "^8.15", + "symplify/easy-coding-standard": "^12.4" }, "autoload": { "psr-4": { From e778bb34af62211473eb1fba5bf2d282294cb2b0 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Tue, 10 Dec 2024 13:00:37 +0700 Subject: [PATCH 03/12] docs: add changelog for new v2 release and adjust README --- CHANGELOG.md | 47 +++++++++++++++++++++++++++++++++++++++++++++-- README.md | 17 ++++++++--------- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a773f78..166ed4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,51 @@ # Changelog +## v2.0.0 - December 2024 -## [Unreleased] - 2024-05-21 +This version is a major release and includes breaking changes. +Adjust your configuration, apply the fixes and review code for the new version. + +### PER Coding Style 2.0 +PER Coding Style 2.0 (https://www.php-fig.org/per/coding-style/) was introduced. +This leads to some adjustments in the code which most probably can be autofixed with `ecs check --fix`. +Review your code after running the fixer. + +Rules applied to the set can be found here: https://cs.symfony.com/doc/ruleSets/PER-CS2.0.html. This is the base from which we make our own small adjustments. +"Concat Space" for example, is a rule which has already been applied to the Symfony rule set in the past, which we enforce in the standard everywhere. + +### Unify rule sets +Removed `whatwedo-wordpress.php` and `whatwedo-symfony.php` in favor of `whatwedo-common.php`. If you have used `whatwedo-symfony.php` or `whatwedo-wordpress.php`, you have to switch to `whatwedo-common.php` in the config. + +### Update ECS Configuration +Update your configuration (`ecs.php`) to the new version and adjust to your preferences. See our example file in the root of this repository. + +### Enforce types via PHP instead of DocBlocks +`PhpdocToReturnTypeFixer` and `PhpdocToParamTypeFixer` will enforce the types in the PHP code instead of the DocBlocks. +If you don't use PHPStan or similar for static type checking, that could be a problem for you since those types can be wrong. You can disable these fixers in your local configuration if you can't migrate them properly for now. + +## Technical + +### Changed +- Unify rule sets (https://github.com/whatwedo/PhpCodingStandard/issues/17) +- Minor update to `symplify/easy-coding-standard 12.4` + +### Added +- DynamicSet `@PER-CS2.0` was added to the configuration (https://github.com/whatwedo/PhpCodingStandard/issues/19) +- Add `MultilinePromotedPropertiesFixer` (https://github.com/whatwedo/PhpCodingStandard/issues/27) +- Add `MultilinePromotedPropertiesFixer` (https://github.com/whatwedo/PhpCodingStandard/issues/27) +- Add `PhpdocToReturnTypeFixer` and `PhpdocToParamTypeFixer` +- `NoDoctrineMigrationsGeneratedCommentFixer` is now part of the default configuration (https://github.com/whatwedo/PhpCodingStandard/issues/16) +- `ConcatSpaceFixer` => `'spacing' => 'none'` is now part of the default configuration (https://github.com/whatwedo/PhpCodingStandard/issues/15) + +### Removed +- Remove deprecated `NoTrailingCommaInListCallFixer` +- `AssignmentInConditionSniff` is skipped + +--- + +## v1.2.5 - March 2024 ### Changed -- update to `symplify/easy-coding-standard ^12` +- Update to `symplify/easy-coding-standard ^12` +- Dump Fixer added diff --git a/README.md b/README.md index 67310be..f26058d 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ # PhpCodingStandard This project is a set of coding standard rules, which we are using at [whatwedo](https://whatwedo.ch). It's heavily based on [Simplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard). +It's based on PER Coding Style 2.0 (https://www.php-fig.org/per/coding-style/). ## Installation @@ -20,15 +21,13 @@ composer require whatwedo/php-coding-standard You can run the checks without project specific configuration using one of following commands: ``` -vendor/bin/ecs check SRC_DIRECTORY --config vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php # Symfony projects -vendor/bin/ecs check SRC_DIRECTORY --config vendor/whatwedo/php-coding-standard/config/whatwedo-wordpress.php # WordPress projects -vendor/bin/ecs check SRC_DIRECTORY --config vendor/whatwedo/php-coding-standard/config/whatwedo-common.php # Common PHP projects +vendor/bin/ecs check SRC_DIRECTORY --config vendor/whatwedo/php-coding-standard/config/whatwedo-common.php ``` - ### With custom configuration -If you want to add additional checkers or exclude files, you have to create an `ecs.php` file in your own project root directory. +But we suggest to create an `ecs.php` file in your own project root directory. +There's a sample configuration file in the root of this repository. ```php skip() - $ecsConfig->skip([ + // Remove rules with $config->skip() + $config->skip([ SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff::class => null, // Explicitly remove some rules in a specific files @@ -50,7 +49,7 @@ return static function (ECSConfig $ecsConfig): void { */ // This need to come last - $ecsConfig->sets([__DIR__ . '/vendor/whatwedo/php-coding-standard/config/whatwedo-common.php']); + $config->sets([__DIR__ . '/vendor/whatwedo/php-coding-standard/config/whatwedo-common.php']); }; ``` From 92c60c6f700bee092b193c2c596145032779916c Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Thu, 14 Nov 2024 17:37:17 +0100 Subject: [PATCH 04/12] feat: add php cs fixer configuration for symfony --- src/PhpCsFixerConfig/SymfonyCsFixerConfig.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/PhpCsFixerConfig/SymfonyCsFixerConfig.php diff --git a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php new file mode 100644 index 0000000..7b71b8b --- /dev/null +++ b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php @@ -0,0 +1,49 @@ +setFinder($finder); + self::configure($config); + return $config; + } + + + public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) + { + $finder->in($projectDir) + ->exclude([ + 'assets', + 'bin', + 'config', + 'node_modules', + 'public', + 'vendor', + 'var', + ] + ); + } + public static function configure(PhpCsFixer\ConfigInterface $config) + { + $config->setRiskyAllowed(true) + ->setRules([ + '@PER-CS' => true, + '@PHP83Migration' => true, + '@PSR12' => true, + '@Symfony' => true, + 'strict_param' => true, + // make ecs compatible + 'phpdoc_to_comment' => false, + 'single_line_throw' => false, + ]); + } +} From ef21ad0ae6f165d0a5fcfb9d301b179f21a504da Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Thu, 14 Nov 2024 17:49:40 +0100 Subject: [PATCH 05/12] feat: cascade fixers --- src/PhpCsFixerConfig/BaseCsFixerConfig.php | 46 +++++++++++++++++++ src/PhpCsFixerConfig/Php38CsFixerConfig.php | 40 ++++++++++++++++ src/PhpCsFixerConfig/SymfonyCsFixerConfig.php | 25 +++++----- 3 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 src/PhpCsFixerConfig/BaseCsFixerConfig.php create mode 100644 src/PhpCsFixerConfig/Php38CsFixerConfig.php diff --git a/src/PhpCsFixerConfig/BaseCsFixerConfig.php b/src/PhpCsFixerConfig/BaseCsFixerConfig.php new file mode 100644 index 0000000..5f73f2c --- /dev/null +++ b/src/PhpCsFixerConfig/BaseCsFixerConfig.php @@ -0,0 +1,46 @@ +setFinder($finder); + self::configure($config); + return $config; + } + + + public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) + { + $finder->in($projectDir) +; + } + public static function configure(PhpCsFixer\ConfigInterface $config) + { + + $existingRulues = $config->getRules(); + $config + ->setRiskyAllowed(true) + ->setRules( + array_merge( + $existingRulues, + [ + '@PER-CS' => true, + '@PSR12' => true, + 'strict_param' => true, + // make ecs compatible + 'phpdoc_to_comment' => false, + 'single_line_throw' => false, + ] + ) + ); + } +} diff --git a/src/PhpCsFixerConfig/Php38CsFixerConfig.php b/src/PhpCsFixerConfig/Php38CsFixerConfig.php new file mode 100644 index 0000000..c15f014 --- /dev/null +++ b/src/PhpCsFixerConfig/Php38CsFixerConfig.php @@ -0,0 +1,40 @@ +setFinder($finder); + self::configure($config); + return $config; + } + + + public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) + { + $finder->in($projectDir) +; + } + public static function configure(PhpCsFixer\ConfigInterface $config) + { + $existingRulues = $config->getRules(); + $config + ->setRiskyAllowed(true) + ->setRules( + array_merge( + $existingRulues, + [ + '@PHP83Migration' => true, + ] + ) + ); + } +} diff --git a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php index 7b71b8b..8a9ca34 100644 --- a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php +++ b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php @@ -13,6 +13,8 @@ public static function createFixer(string $projectDir): PhpCsFixer\ConfigInterfa $config = new PhpCsFixer\Config(); $config->setFinder($finder); + BaseCsFixerConfig::configure($config); + Php38CsFixerConfig::configure($config); self::configure($config); return $config; } @@ -32,18 +34,19 @@ public static function configureFinder(PhpCsFixer\Finder $finder, string $projec ] ); } + public static function configure(PhpCsFixer\ConfigInterface $config) { - $config->setRiskyAllowed(true) - ->setRules([ - '@PER-CS' => true, - '@PHP83Migration' => true, - '@PSR12' => true, - '@Symfony' => true, - 'strict_param' => true, - // make ecs compatible - 'phpdoc_to_comment' => false, - 'single_line_throw' => false, - ]); + $existingRulues = $config->getRules(); + $config + ->setRiskyAllowed(true) + ->setRules( + array_merge( + $existingRulues, + [ + '@Symfony' => true, + ] + ) + ); } } From 662777d9d630dacc0db5a9bce5f809a74bb22190 Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Thu, 14 Nov 2024 17:56:18 +0100 Subject: [PATCH 06/12] feat: add interface --- src/PhpCsFixerConfig/BaseCsFixerConfig.php | 4 ++-- src/PhpCsFixerConfig/Php38CsFixerConfig.php | 4 ++-- src/PhpCsFixerConfig/SymfonyCsFixerConfig.php | 4 ++-- .../WwdPhpCsFixerConfigInterface.php | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php diff --git a/src/PhpCsFixerConfig/BaseCsFixerConfig.php b/src/PhpCsFixerConfig/BaseCsFixerConfig.php index 5f73f2c..225bc44 100644 --- a/src/PhpCsFixerConfig/BaseCsFixerConfig.php +++ b/src/PhpCsFixerConfig/BaseCsFixerConfig.php @@ -4,9 +4,9 @@ use PhpCsFixer; -class BaseCsFixerConfig +class BaseCsFixerConfig implements WwdPhpCsFixerConfigInterface { - public static function createFixer(string $projectDir): PhpCsFixer\ConfigInterface + public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterface { $finder = new PhpCsFixer\Finder(); self::configureFinder($finder, $projectDir); diff --git a/src/PhpCsFixerConfig/Php38CsFixerConfig.php b/src/PhpCsFixerConfig/Php38CsFixerConfig.php index c15f014..d299a96 100644 --- a/src/PhpCsFixerConfig/Php38CsFixerConfig.php +++ b/src/PhpCsFixerConfig/Php38CsFixerConfig.php @@ -4,9 +4,9 @@ use PhpCsFixer; -class Php38CsFixerConfig +class Php38CsFixerConfig implements WwdPhpCsFixerConfigInterface { - public static function createFixer(string $projectDir): PhpCsFixer\ConfigInterface + public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterface { $finder = new PhpCsFixer\Finder(); self::configureFinder($finder, $projectDir); diff --git a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php index 8a9ca34..8f88ba2 100644 --- a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php +++ b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php @@ -4,9 +4,9 @@ use PhpCsFixer; -class SymfonyCsFixerConfig +class SymfonyCsFixerConfig implements WwdPhpCsFixerConfigInterface { - public static function createFixer(string $projectDir): PhpCsFixer\ConfigInterface + public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterface { $finder = new PhpCsFixer\Finder(); self::configureFinder($finder, $projectDir); diff --git a/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php b/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php new file mode 100644 index 0000000..a7b9877 --- /dev/null +++ b/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php @@ -0,0 +1,14 @@ + Date: Thu, 14 Nov 2024 18:25:59 +0100 Subject: [PATCH 07/12] feat: remove cascading, order of rules matter! --- src/PhpCsFixerConfig/BaseCsFixerConfig.php | 25 +++++------- src/PhpCsFixerConfig/Php38CsFixerConfig.php | 40 ------------------- src/PhpCsFixerConfig/SymfonyCsFixerConfig.php | 21 +++++----- 3 files changed, 22 insertions(+), 64 deletions(-) delete mode 100644 src/PhpCsFixerConfig/Php38CsFixerConfig.php diff --git a/src/PhpCsFixerConfig/BaseCsFixerConfig.php b/src/PhpCsFixerConfig/BaseCsFixerConfig.php index 225bc44..2bc0c0a 100644 --- a/src/PhpCsFixerConfig/BaseCsFixerConfig.php +++ b/src/PhpCsFixerConfig/BaseCsFixerConfig.php @@ -20,27 +20,22 @@ public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterf public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) { - $finder->in($projectDir) -; + $finder->in($projectDir); } + public static function configure(PhpCsFixer\ConfigInterface $config) { - - $existingRulues = $config->getRules(); $config ->setRiskyAllowed(true) ->setRules( - array_merge( - $existingRulues, - [ - '@PER-CS' => true, - '@PSR12' => true, - 'strict_param' => true, - // make ecs compatible - 'phpdoc_to_comment' => false, - 'single_line_throw' => false, - ] - ) + [ + '@PER-CS' => true, + '@PSR12' => true, + 'strict_param' => true, + // make ecs compatible + 'phpdoc_to_comment' => false, + 'single_line_throw' => false, + ] ); } } diff --git a/src/PhpCsFixerConfig/Php38CsFixerConfig.php b/src/PhpCsFixerConfig/Php38CsFixerConfig.php deleted file mode 100644 index d299a96..0000000 --- a/src/PhpCsFixerConfig/Php38CsFixerConfig.php +++ /dev/null @@ -1,40 +0,0 @@ -setFinder($finder); - self::configure($config); - return $config; - } - - - public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) - { - $finder->in($projectDir) -; - } - public static function configure(PhpCsFixer\ConfigInterface $config) - { - $existingRulues = $config->getRules(); - $config - ->setRiskyAllowed(true) - ->setRules( - array_merge( - $existingRulues, - [ - '@PHP83Migration' => true, - ] - ) - ); - } -} diff --git a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php index 8f88ba2..d1820f1 100644 --- a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php +++ b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php @@ -13,8 +13,6 @@ public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterf $config = new PhpCsFixer\Config(); $config->setFinder($finder); - BaseCsFixerConfig::configure($config); - Php38CsFixerConfig::configure($config); self::configure($config); return $config; } @@ -37,16 +35,21 @@ public static function configureFinder(PhpCsFixer\Finder $finder, string $projec public static function configure(PhpCsFixer\ConfigInterface $config) { - $existingRulues = $config->getRules(); + $config ->setRiskyAllowed(true) ->setRules( - array_merge( - $existingRulues, - [ - '@Symfony' => true, - ] - ) + [ + '@PER-CS' => true, + '@PSR12' => true, + '@PHP83Migration' => true, + '@Symfony' => true, + 'strict_param' => true, + // make ecs compatible + 'phpdoc_to_comment' => false, + 'single_line_throw' => false, + ] + ); } } From aab99120bfe84ec20e15c9bdab582a421f09e1d7 Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Thu, 14 Nov 2024 18:35:26 +0100 Subject: [PATCH 08/12] doc: usage for php cs fixer rules --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index f26058d..17aba8b 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,35 @@ To fix certain issues automatically add `--fix` add the end For other configuration options, check out [Simplify/EasyCodingStandard](https://github.com/Symplify/EasyCodingStandard). +## Usage with PHP CS Fixer *Experimental* + +add `.php-cs-fixer.dist.php` in your project + +```php +getFinder()->exclude( + [ + 'data', + 'doc', + 'docker', + 'drivers', + 'vendor-bin', + ]); + +// add some project rules +// be careful, can be tricky, rules are dependent of the order +$existingRules = $config->getRules(); +$existingRules['some_new_rule'] => true; + +return $config; +``` ## Dependencies From e0fe05e111372f90b294b04b324b4fa43951d410 Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Fri, 15 Nov 2024 10:49:01 +0100 Subject: [PATCH 09/12] feat: Config builder for cascading rules --- README.md | 26 ++--- src/PhpCsFixerConfig/BaseCsFixerConfig.php | 39 +++----- src/PhpCsFixerConfig/SymfonyCsFixerConfig.php | 57 +++-------- .../WwdPhpCsFixerConfigInterface.php | 11 +-- src/PhpCsFixerConfigBuilder.php | 99 +++++++++++++++++++ 5 files changed, 140 insertions(+), 92 deletions(-) create mode 100644 src/PhpCsFixerConfigBuilder.php diff --git a/README.md b/README.md index 17aba8b..4fd972a 100644 --- a/README.md +++ b/README.md @@ -70,25 +70,19 @@ add `.php-cs-fixer.dist.php` in your project ```php getFinder()->exclude( - [ - 'data', - 'doc', - 'docker', - 'drivers', - 'vendor-bin', - ]); -// add some project rules -// be careful, can be tricky, rules are dependent of the order -$existingRules = $config->getRules(); -$existingRules['some_new_rule'] => true; +$config = PhpCsFixerConfigBuilder::build(__DIR__, $configs); return $config; ``` diff --git a/src/PhpCsFixerConfig/BaseCsFixerConfig.php b/src/PhpCsFixerConfig/BaseCsFixerConfig.php index 2bc0c0a..ff58689 100644 --- a/src/PhpCsFixerConfig/BaseCsFixerConfig.php +++ b/src/PhpCsFixerConfig/BaseCsFixerConfig.php @@ -2,40 +2,23 @@ namespace whatwedo\PhpCodingStandard\PhpCsFixerConfig; -use PhpCsFixer; - class BaseCsFixerConfig implements WwdPhpCsFixerConfigInterface { - public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterface + public static function getRules(): array { - $finder = new PhpCsFixer\Finder(); - self::configureFinder($finder, $projectDir); - - $config = new PhpCsFixer\Config(); - $config->setFinder($finder); - self::configure($config); - return $config; - } + return [ + 100 => ['@PER-CS' => true], + 200 => ['@PSR12' => true], - - public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) - { - $finder->in($projectDir); + 10000 => ['strict_param' => true], + // make ecs compatible + 15000 => ['phpdoc_to_comment' => false], + 16000 => ['single_line_throw' => false], + ]; } - public static function configure(PhpCsFixer\ConfigInterface $config) + public static function getExcludes(): array { - $config - ->setRiskyAllowed(true) - ->setRules( - [ - '@PER-CS' => true, - '@PSR12' => true, - 'strict_param' => true, - // make ecs compatible - 'phpdoc_to_comment' => false, - 'single_line_throw' => false, - ] - ); + return []; } } diff --git a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php index d1820f1..17dec94 100644 --- a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php +++ b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php @@ -2,54 +2,27 @@ namespace whatwedo\PhpCodingStandard\PhpCsFixerConfig; -use PhpCsFixer; - class SymfonyCsFixerConfig implements WwdPhpCsFixerConfigInterface { - public static function createConfig(string $projectDir): PhpCsFixer\ConfigInterface - { - $finder = new PhpCsFixer\Finder(); - self::configureFinder($finder, $projectDir); - - $config = new PhpCsFixer\Config(); - $config->setFinder($finder); - self::configure($config); - return $config; - } - - public static function configureFinder(PhpCsFixer\Finder $finder, string $projectDir) + public static function getRules(): array { - $finder->in($projectDir) - ->exclude([ - 'assets', - 'bin', - 'config', - 'node_modules', - 'public', - 'vendor', - 'var', - ] - ); + return [ + 300 => ['@PHP83Migration' => true], + 400 => ['@Symfony' => true], + ]; } - public static function configure(PhpCsFixer\ConfigInterface $config) + public static function getExcludes(): array { - - $config - ->setRiskyAllowed(true) - ->setRules( - [ - '@PER-CS' => true, - '@PSR12' => true, - '@PHP83Migration' => true, - '@Symfony' => true, - 'strict_param' => true, - // make ecs compatible - 'phpdoc_to_comment' => false, - 'single_line_throw' => false, - ] - - ); + return [ + 'assets', + 'bin', + 'config', + 'node_modules', + 'public', + 'vendor', + 'var', + ]; } } diff --git a/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php b/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php index a7b9877..1513d56 100644 --- a/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php +++ b/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php @@ -2,13 +2,12 @@ namespace whatwedo\PhpCodingStandard\PhpCsFixerConfig; -use PhpCsFixer\ConfigInterface; -use PhpCsFixer\Finder; - interface WwdPhpCsFixerConfigInterface { - public static function createConfig(string $basePath); - public static function configure(ConfigInterface $config); - public static function configureFinder(Finder $finder, string $projectDir); + /** + * @var array > + */ + public static function getRules(): array; + public static function getExcludes(): array; } diff --git a/src/PhpCsFixerConfigBuilder.php b/src/PhpCsFixerConfigBuilder.php new file mode 100644 index 0000000..921cc89 --- /dev/null +++ b/src/PhpCsFixerConfigBuilder.php @@ -0,0 +1,99 @@ +[] $configs + */ + public static function build( + string $projectDir, + array $configs + ): ConfigInterface + { + $excludes = self::buildExcludes($configs); + + $finder = new PhpCsFixer\Finder(); + $finder->in($projectDir) + ->exclude($excludes); + + $config = new PhpCsFixer\Config(); + $config->setFinder($finder) + ->setRiskyAllowed(true) + ->setRules(self::buildRules($configs)); + + return $config; + } + + private static function getNextOrder(array $rules, int $order) + { + while (array_key_exists($order, $rules)) { + $order++; + } + return $order; + } + + /** + * @param class-string[] $configs + */ + private static function buildRules(array $configs): array + { + /** + * @var array > + */ + $rulesGroups = []; + + foreach ($configs as $config) { + foreach ($config::getRules() as $order => $configRule) { + $rulesGroups[self::getNextOrder($rulesGroups, $order)] = $configRule; + } + } + + // order rules from Configs + ksort($rulesGroups); + + $rules = []; + foreach ($rulesGroups as $rulesGroup) { + foreach ($rulesGroup as $rule => $ruleSetting) { + $rules[$rule] = $ruleSetting; + } + } + + return $rules; + } + + /** + * @param class-string[] $configs + */ + public static function dumpRules(array $configs) + { + var_dump(self::buildRules($configs)); + } + + /** + * @param class-string[] $configs + */ + public static function dumpExcludes(array $configs) + { + var_dump(self::buildExcludes($configs)); + } + + /** + * @param class-string[] $configs + */ + private static function buildExcludes(array $configs): array + { + $excludes = []; + foreach ($configs as $config) { + foreach ($config::getExcludes() as $configExclude) { + $excludes[] = $configExclude; + } + } + return $excludes; + } +} From 9f48004cf17fa615d6a42033062ad8a6c2e662be Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Tue, 10 Dec 2024 16:38:31 +0100 Subject: [PATCH 10/12] feat: unite symfony and base rules --- README.md | 1 - src/PhpCsFixerConfig/BaseCsFixerConfig.php | 3 +- src/PhpCsFixerConfig/SymfonyCsFixerConfig.php | 28 ------------------- 3 files changed, 2 insertions(+), 30 deletions(-) delete mode 100644 src/PhpCsFixerConfig/SymfonyCsFixerConfig.php diff --git a/README.md b/README.md index 4fd972a..4b2b250 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,6 @@ use whatwedo\PhpCodingStandard\PhpCsFixerConfig\SymfonyCsFixerConfig; use whatwedo\PhpCodingStandard\PhpCsFixerConfigBuilder; $configs = [ - SymfonyCsFixerConfig::class, BaseCsFixerConfig::class, ]; //PhpCsFixerConfigBuilder::dumpRules($configs); diff --git a/src/PhpCsFixerConfig/BaseCsFixerConfig.php b/src/PhpCsFixerConfig/BaseCsFixerConfig.php index ff58689..51453f2 100644 --- a/src/PhpCsFixerConfig/BaseCsFixerConfig.php +++ b/src/PhpCsFixerConfig/BaseCsFixerConfig.php @@ -9,7 +9,8 @@ public static function getRules(): array return [ 100 => ['@PER-CS' => true], 200 => ['@PSR12' => true], - + 300 => ['@PHP83Migration' => true], + 400 => ['@Symfony' => true], 10000 => ['strict_param' => true], // make ecs compatible 15000 => ['phpdoc_to_comment' => false], diff --git a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php b/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php deleted file mode 100644 index 17dec94..0000000 --- a/src/PhpCsFixerConfig/SymfonyCsFixerConfig.php +++ /dev/null @@ -1,28 +0,0 @@ - ['@PHP83Migration' => true], - 400 => ['@Symfony' => true], - ]; - } - - public static function getExcludes(): array - { - return [ - 'assets', - 'bin', - 'config', - 'node_modules', - 'public', - 'vendor', - 'var', - ]; - } -} From 7733657416ea8f3b36dbd0dc6c032afd08fbaf67 Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Wed, 11 Dec 2024 14:32:32 +0100 Subject: [PATCH 11/12] feat: align phpcs-config to ECS config --- README.md | 1 - src/PhpCsFixerConfig/BaseCsFixerConfig.php | 32 +++++++++++++++++----- src/PhpCsFixerConfigBuilder.php | 4 +-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4b2b250..8bde6ee 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,6 @@ add `.php-cs-fixer.dist.php` in your project ['@PER-CS' => true], - 200 => ['@PSR12' => true], - 300 => ['@PHP83Migration' => true], - 400 => ['@Symfony' => true], + 100 => ['@Symfony' => true], 10000 => ['strict_param' => true], - // make ecs compatible - 15000 => ['phpdoc_to_comment' => false], - 16000 => ['single_line_throw' => false], + 11001 => [ + 'function_declaration' => [ + 'closure_fn_spacing' => 'none' + ], + 'phpdoc_to_return_type' => true, + 'phpdoc_to_param_type' => true, + \PhpCsFixerCustomFixers\Fixer\NoNullableBooleanTypeFixer::name() => true, + \PhpCsFixerCustomFixers\Fixer\MultilinePromotedPropertiesFixer::name() => true, + 'single_line_empty_body' => true, //reset @Syfmony setting + 'trailing_comma_in_multiline' => [ //reset @Syfmony setting + 'after_heredoc' => true, + 'elements' => [ + 'arguments', + 'array_destructuring', + 'arrays', + 'match', + 'parameters' + ] + ], + 'phpdoc_to_comment' => false, + 'single_line_throw' => false, + + ], + ]; } diff --git a/src/PhpCsFixerConfigBuilder.php b/src/PhpCsFixerConfigBuilder.php index 921cc89..6c57220 100644 --- a/src/PhpCsFixerConfigBuilder.php +++ b/src/PhpCsFixerConfigBuilder.php @@ -3,7 +3,7 @@ namespace whatwedo\PhpCodingStandard; use PhpCsFixer; -use PhpCsFixer\ConfigInterface; +use PhpCsFixer\ParallelAwareConfigInterface; use whatwedo\PhpCodingStandard\PhpCsFixerConfig\WwdPhpCsFixerConfigInterface; class PhpCsFixerConfigBuilder @@ -14,7 +14,7 @@ class PhpCsFixerConfigBuilder public static function build( string $projectDir, array $configs - ): ConfigInterface + ): ParallelAwareConfigInterface { $excludes = self::buildExcludes($configs); From 4e7f0ef780372da5b7260a2b67e7a92f31af39ad Mon Sep 17 00:00:00 2001 From: Maurizio Monticelli Date: Wed, 11 Dec 2024 14:44:51 +0100 Subject: [PATCH 12/12] feat: add configure interface --- src/PhpCsFixerConfig/BaseCsFixerConfig.php | 10 +++++++++- src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php | 4 ++++ src/PhpCsFixerConfigBuilder.php | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/PhpCsFixerConfig/BaseCsFixerConfig.php b/src/PhpCsFixerConfig/BaseCsFixerConfig.php index b16b2d7..1113a9e 100644 --- a/src/PhpCsFixerConfig/BaseCsFixerConfig.php +++ b/src/PhpCsFixerConfig/BaseCsFixerConfig.php @@ -2,6 +2,8 @@ namespace whatwedo\PhpCodingStandard\PhpCsFixerConfig; +use PhpCsFixer\ConfigInterface; + class BaseCsFixerConfig implements WwdPhpCsFixerConfigInterface { public static function getRules(): array @@ -30,7 +32,6 @@ public static function getRules(): array ], 'phpdoc_to_comment' => false, 'single_line_throw' => false, - ], ]; @@ -40,4 +41,11 @@ public static function getExcludes(): array { return []; } + + public static function configure(ConfigInterface $config): void + { + $config->registerCustomFixers(new \PhpCsFixerCustomFixers\Fixers()); + } + + } diff --git a/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php b/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php index 1513d56..a6c3468 100644 --- a/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php +++ b/src/PhpCsFixerConfig/WwdPhpCsFixerConfigInterface.php @@ -2,6 +2,8 @@ namespace whatwedo\PhpCodingStandard\PhpCsFixerConfig; +use PhpCsFixer\ConfigInterface; + interface WwdPhpCsFixerConfigInterface { /** @@ -10,4 +12,6 @@ interface WwdPhpCsFixerConfigInterface public static function getRules(): array; public static function getExcludes(): array; + + public static function configure(ConfigInterface $config): void; } diff --git a/src/PhpCsFixerConfigBuilder.php b/src/PhpCsFixerConfigBuilder.php index 6c57220..466dc22 100644 --- a/src/PhpCsFixerConfigBuilder.php +++ b/src/PhpCsFixerConfigBuilder.php @@ -23,6 +23,7 @@ public static function build( ->exclude($excludes); $config = new PhpCsFixer\Config(); + self::configure($config, $configs); $config->setFinder($finder) ->setRiskyAllowed(true) ->setRules(self::buildRules($configs)); @@ -96,4 +97,12 @@ private static function buildExcludes(array $configs): array } return $excludes; } + + private static function configure(PhpCsFixer\Config $config, array $configs) + { + /** @var WwdPhpCsFixerConfigInterface $configurationSet */ + foreach ($configs as $configurationSet) { + $configurationSet::configure($config); + } + } }