From eac75d69cd8ca721f55d58836998b7b4491ce11a Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Tue, 10 Dec 2024 12:58:27 +0700 Subject: [PATCH 1/8] feat: unify rule sets and cleanup/adjust rules --- config/whatwedo-common.php | 19 +++++++++++++++---- config/whatwedo-symfony.php | 15 --------------- config/whatwedo-wordpress.php | 9 --------- ecs.php.symfony.dist | 16 ---------------- 4 files changed, 15 insertions(+), 44 deletions(-) delete mode 100644 config/whatwedo-symfony.php delete mode 100644 config/whatwedo-wordpress.php delete mode 100644 ecs.php.symfony.dist diff --git a/config/whatwedo-common.php b/config/whatwedo-common.php index 4a76dd4..d7198a0 100644 --- a/config/whatwedo-common.php +++ b/config/whatwedo-common.php @@ -13,10 +13,12 @@ use PhpCsFixer\Fixer\CastNotation\CastSpacesFixer; use PhpCsFixer\Fixer\ClassNotation\FinalInternalClassFixer; use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer; -use PhpCsFixer\Fixer\ControlStructure\NoTrailingCommaInListCallFixer; use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer; +use PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer; +use PhpCsFixer\Fixer\FunctionNotation\PhpdocToReturnTypeFixer; use PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer; use PhpCsFixer\Fixer\NamespaceNotation\BlankLineAfterNamespaceFixer; +use PhpCsFixer\Fixer\Operator\ConcatSpaceFixer; use PhpCsFixer\Fixer\Operator\IncrementStyleFixer; use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer; use PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer; @@ -36,6 +38,8 @@ use PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer; use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer; use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer; +use PhpCsFixerCustomFixers\Fixer\MultilinePromotedPropertiesFixer; +use PhpCsFixerCustomFixers\Fixer\NoDoctrineMigrationsGeneratedCommentFixer; use PhpCsFixerCustomFixers\Fixer\NoImportFromGlobalNamespaceFixer; use PhpCsFixerCustomFixers\Fixer\NoNullableBooleanTypeFixer; use PhpCsFixerCustomFixers\Fixer\NoPhpStormGeneratedCommentFixer; @@ -55,6 +59,7 @@ $ecsConfig->sets([SetList::CLEAN_CODE]); $ecsConfig->sets([SetList::COMMON]); $ecsConfig->sets([SetList::PSR_12]); + $ecsConfig->dynamicSets(['@PER-CS2.0']); $ecsConfig->rule(ValidClassNameSniff::class); $ecsConfig->rule(ClassCommentSniff::class); @@ -84,15 +89,22 @@ $ecsConfig->rule(PhpdocSelfAccessorFixer::class); $ecsConfig->rule(PhpdocVarAnnotationCorrectOrderFixer::class); $ecsConfig->rule(ForbiddenAnnotationsSniff::class); - $ecsConfig->rule(AssignmentInConditionSniff::class); $ecsConfig->rule(DeadCatchSniff::class); $ecsConfig->rule(UseFromSameNamespaceSniff::class); + $ecsConfig->rule(MultilinePromotedPropertiesFixer::class); $ecsConfig->rule(DumpFixer::class); - $ecsConfig->ruleWithConfiguration(OperatorLinebreakFixer::class, [ 'only_booleans' => true, 'position' => 'beginning', ]); + $ecsConfig->rule(PhpdocToReturnTypeFixer::class); + $ecsConfig->rule(PhpdocToParamTypeFixer::class); + $ecsConfig->rule(NoDoctrineMigrationsGeneratedCommentFixer::class); + + // https://github.com/whatwedo/PhpCodingStandard/issues/15 + $ecsConfig->ruleWithConfiguration(ConcatSpaceFixer::class, [ + 'spacing' => 'none', + ]); $ecsConfig->skip([ AssignmentInConditionSniff::class => null, @@ -100,7 +112,6 @@ ScopeIndentSniff::class => null, ScopeClosingBraceSniff::class => null, CastSpacesFixer::class => null, - NoTrailingCommaInListCallFixer::class => null, YodaStyleFixer::class => null, IsNullFixer::class => null, BlankLineAfterNamespaceFixer::class => null, diff --git a/config/whatwedo-symfony.php b/config/whatwedo-symfony.php deleted file mode 100644 index f66077f..0000000 --- a/config/whatwedo-symfony.php +++ /dev/null @@ -1,15 +0,0 @@ -sets([__DIR__ . '/whatwedo-common.php']); - $ecsConfig->rule(NoDoctrineMigrationsGeneratedCommentFixer::class); - $ecsConfig->ruleWithConfiguration(ConcatSpaceFixer::class, [ - 'spacing' => 'none', - ]); -}; diff --git a/config/whatwedo-wordpress.php b/config/whatwedo-wordpress.php deleted file mode 100644 index a483ffd..0000000 --- a/config/whatwedo-wordpress.php +++ /dev/null @@ -1,9 +0,0 @@ -sets([__DIR__ . '/whatwedo-common.php']); -}; diff --git a/ecs.php.symfony.dist b/ecs.php.symfony.dist deleted file mode 100644 index 389e9d9..0000000 --- a/ecs.php.symfony.dist +++ /dev/null @@ -1,16 +0,0 @@ -withSets([ - 'vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php' - ]) - ->withPaths([ - 'src', - 'tests', - ]) - ->withSkip([ - ]) - ; From 3ccae742e879cb94dd9efc94ba193ba0ed1071ba Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Tue, 10 Dec 2024 12:59:11 +0700 Subject: [PATCH 2/8] docs: add example config --- ecs.example.php | 14 ++++++++++++++ ecs.php | 4 ++++ 2 files changed, 18 insertions(+) create mode 100644 ecs.example.php diff --git a/ecs.example.php b/ecs.example.php new file mode 100644 index 0000000..35beac3 --- /dev/null +++ b/ecs.example.php @@ -0,0 +1,14 @@ +paths([ + 'src', + 'tests', + ]); + $config->skip([]); + $config->import('vendor/whatwedo/php-coding-standard/config/whatwedo-common.php'); +}; diff --git a/ecs.php b/ecs.php index 5d8a49f..1c70574 100644 --- a/ecs.php +++ b/ecs.php @@ -1,5 +1,9 @@ Date: Tue, 10 Dec 2024 13:00:04 +0700 Subject: [PATCH 3/8] 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 36311647a77fd1fe0bc4b03a6b20a799cb122662 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Tue, 10 Dec 2024 13:00:37 +0700 Subject: [PATCH 4/8] 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 e4b735da71c1d796b58723c59cc1d73fb4800855 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Wed, 11 Dec 2024 10:29:50 +0700 Subject: [PATCH 5/8] feat: change rules according to Symfony's PER-CS2.0 ruleset The PER-CS2.0 ruleset of Symfony doesn't match the documentation. We choose the more readable version, which is also part of the official PER-CS2.0 ruleset (https://cs.symfony.com/doc/ruleSets/PER-CS2.0.html) #15 --- CHANGELOG.md | 3 ++- config/whatwedo-common.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 166ed4f..edbdc2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,10 +35,11 @@ If you don't use PHPStan or similar for static type checking, that could be a pr - 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) +- `ConcatSpaceFixer` => `'spacing' => 'one'` was added (https://github.com/whatwedo/PhpCodingStandard/issues/15) ### Removed - Remove deprecated `NoTrailingCommaInListCallFixer` +- `ConcatSpaceFixer` => `'spacing' => 'none'` was replaced with `'spacing' => 'one'` for projects with the Symfony rule set in place - `AssignmentInConditionSniff` is skipped --- diff --git a/config/whatwedo-common.php b/config/whatwedo-common.php index d7198a0..a2392a9 100644 --- a/config/whatwedo-common.php +++ b/config/whatwedo-common.php @@ -103,7 +103,7 @@ // https://github.com/whatwedo/PhpCodingStandard/issues/15 $ecsConfig->ruleWithConfiguration(ConcatSpaceFixer::class, [ - 'spacing' => 'none', + 'spacing' => 'one', ]); $ecsConfig->skip([ From 68fc28950e3782722cc237ef3ea5ba2f9cb44722 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Wed, 11 Dec 2024 10:47:53 +0700 Subject: [PATCH 6/8] chore: remove IDE project files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 987e2a2..b3b22fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ composer.lock vendor +/.idea From 2e89961eb66b4b7fa6863576bac07c7355b86bf3 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Wed, 11 Dec 2024 11:00:10 +0700 Subject: [PATCH 7/8] docs: add a note for contributors to check the project for the coding standards as well --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index f26058d..2e8a450 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,12 @@ For other configuration options, check out [Simplify/EasyCodingStandard](https:/ * [kubawerlos/php-cs-fixer-custom-fixers](https://github.com/kubawerlos/php-cs-fixer-custom-fixers) * [slevomat/coding-standard](https://github.com/slevomat/coding-standard) +## Contributing + +Feel free to contribute to this project. We are always open for suggestions and improvements. + +Don't forget to check adjustments within this project with the coding standards as well. +Run `make ecs` to check for coding standard violations and fix them. ## License From 8666429ad24a57d031cfebdfce61f8f72efed973 Mon Sep 17 00:00:00 2001 From: Marc Wieland Date: Wed, 11 Dec 2024 11:00:29 +0700 Subject: [PATCH 8/8] style: fix code style in DumpFixer --- src/Fixer/DumpFixer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fixer/DumpFixer.php b/src/Fixer/DumpFixer.php index 21eaf1c..601b050 100644 --- a/src/Fixer/DumpFixer.php +++ b/src/Fixer/DumpFixer.php @@ -52,7 +52,7 @@ public function getDefinition(): FixerDefinitionInterface { return new FixerDefinition( 'Removes dump/var_dump statements, which shouldn\'t be in production ever.', - [new CodeSample("