diff --git a/.gitignore b/.gitignore index 987e2a2..b3b22fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ composer.lock vendor +/.idea diff --git a/CHANGELOG.md b/CHANGELOG.md index a773f78..edbdc2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,52 @@ # 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' => '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 + +--- + +## 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..2e8a450 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']); }; ``` @@ -72,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 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": { diff --git a/config/whatwedo-common.php b/config/whatwedo-common.php index 4a76dd4..a2392a9 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' => 'one', + ]); $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.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 @@ withSets([ - 'vendor/whatwedo/php-coding-standard/config/whatwedo-symfony.php' - ]) - ->withPaths([ - 'src', - 'tests', - ]) - ->withSkip([ - ]) - ; 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("