From 9f522745673a24be89dc27537e8d8f3d78fd8104 Mon Sep 17 00:00:00 2001 From: Martin Guth Date: Tue, 16 Apr 2024 10:34:01 +0200 Subject: [PATCH] fixup! Upgrade everything to its newest version, use PSR-12 --- CHANGELOG.md | 6 ++-- README.md | 80 ++++++++++++++++++++++++++------------------------ UPGRADE-4.0.md | 3 +- composer.json | 2 +- ecs.php | 2 -- 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbb4103..1464251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,14 +5,14 @@ ## Unreleased -- Lock PHP support to 8.0+ +- Require PHP ^8.0 - Update to slevomat/coding-standard ^8.0 - Update to squizlabs/php_codesniffer ^3.9 - Update to symplify/easy-coding-standard ^12.1 - Move coding standard declarations from `ecs-7.4.php` and `ecs-8.0.php` to `ecs.php` and remove the former files - Change deprecated rules to new ones -- Add new `ecs-8.2.php` coding standard declaration file for PHP 8.2+. -- Add new `ecs-8.3.php` coding standard declaration file for PHP 8.3+. +- Add new `ecs-8.2.php` coding standard declaration file for PHP 8.2+ +- Add new `ecs-8.3.php` coding standard declaration file for PHP 8.3+ ## 3.3.1 - 2022-05-23 - Lock `symplify/easy-coding-standard` to <10.2.4 because of backward incompatibilities introduced in its bugfix releases. diff --git a/README.md b/README.md index 3248836..e44e6b7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ PHP coding standard used in [LMC](https://www.lmc.eu/en/) projects. -Standard is based on [PSR-12](https://www.php-fig.org/psr/psr-12/) (from version 4.0, before it was PSR-2) and adds +Standard is based on [PSR-12](https://www.php-fig.org/psr/psr-12/) and adds various checks to make sure the code is readable, does follow the same conventions and does not contain common mistakes. We use [EasyCodingStandard] to define and execute checks created for both [PHP-CS-Fixer] and [PHP_CodeSniffer]. @@ -22,18 +22,27 @@ composer require --dev lmc/coding-standard ```php import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); +use Symplify\EasyCodingStandard\Config\ECSConfig; +return ECSConfig::configure() + ->withSets( + [ + __DIR__ . '/vendor/lmc/coding-standard/ecs.php', + ] + ); + // Be default only checks compatible with PHP 8.0 are enabled. - // Depending on the lowest PHP version your project need to support, you can enable additional checks for PHP 7.4, 8.0 or 8.1. + // Depending on the lowest PHP version your project need to support, you can enable additional checks for + // PHP 8.1, 8.2 and 8.3. // Import one of ecs-8.1.php, ecs-8.2.php or ecs-8.3.php. Use only one file (for the highest possible PHP version). - //$containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs-8.3.php'); -}; + //->withSets( + // [ + // __DIR__ . '/vendor/lmc/coding-standard/ecs.php', + // __DIR__ . '/vendor/lmc/coding-standard/ecs-8.3.php', + // ] + //); ``` 2. Run the check command (for `src/` and `tests/` directories): @@ -71,21 +80,18 @@ Be aware you must add these settings **after** import of the base LMC code-style use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff; use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; - -return static function (ContainerConfigurator $containerConfigurator): void { - $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); - - $services = $containerConfigurator->services(); +use Symplify\EasyCodingStandard\Config\ECSConfig; +return ECSConfig::configure() + ->withSets( + [ + __DIR__ . '/vendor/lmc/coding-standard/ecs.php', + ] + ) // Enforce line-length to 120 characters - $services->set(LineLengthSniff::class) - ->property('absoluteLineLimit', 120); - + ->withConfiguredRule(LineLengthSniff::class, ['absoluteLineLimit' => 120]) // Tests must have @test annotation - $services->set(PhpUnitTestAnnotationFixer::class) - ->call('configure', [['style' => 'annotation']]); -}; + ->withConfiguredRule(PhpUnitTestAnnotationFixer::class, ['style' => 'annotation']); ``` See [EasyCodingStandard docs](https://github.com/symplify/easy-coding-standard#configuration) for more configuration options. @@ -102,28 +108,24 @@ Unlike adding/modifying checks, skips must be added **before** import of the bas use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff; use PHP_CodeSniffer\Standards\Squiz\Sniffs\Arrays\ArrayDeclarationSniff; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -use Symplify\EasyCodingStandard\ValueObject\Option; - -return static function (ContainerConfigurator $containerConfigurator): void { - $parameters = $containerConfigurator->parameters(); - - $parameters->set( - Option::SKIP, +use Symplify\EasyCodingStandard\Config\ECSConfig; + +return ECSConfig::configure() + ->withSkip([ + // Ignore specific check only in specific files + ForbiddenFunctionsSniff::class => [__DIR__ . '/src-tests/bootstrap.php'], + // Disable check entirely + ArrayDeclarationSniff::class, + // Skip one file + __DIR__ . '/file/to/be/skipped.php', + // Skip entire directory + __DIR__ . '/ignored/directory/*', + ]) + ->withSets( [ - // Ignore specific check only in specific files - ForbiddenFunctionsSniff::class => [__DIR__ . '/src-tests/bootstrap.php'], - // Disable check entirely - ArrayDeclarationSniff::class, - // Skip one file - __DIR__ . '/file/to/be/skipped.php', - // Skip entire directory - __DIR__ . '/ignored/directory/*', + __DIR__ . '/vendor/lmc/coding-standard/ecs.php', ] ); - - $containerConfigurator->import(__DIR__ . '/vendor/lmc/coding-standard/ecs.php'); -}; ``` See [EasyCodingStandard docs](https://github.com/symplify/easy-coding-standard#configuration) for more configuration options. diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 39065e7..731b381 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -27,7 +27,8 @@ Rules are now set using `ECSConfig::configure()->withRules([])` or `ECSConfig::c Skiping tests is now done using `ECSConfig::configure()->withSkip()` instead of `$parameters->set(Option::SKIP, ...)`. Imports are now done using `ECSConfig::configure()->withSets()` instead of `$containerConfigurator->import()`. -See [ECS documentation](https://github.com/easy-coding-standard/easy-coding-standard/tree/main?tab=readme-ov-file#configure) for more configuration options and examples. +See [ECS documentation](https://github.com/easy-coding-standard/easy-coding-standard/tree/main?tab=readme-ov-file#configure) for more configuration options +Examples of configurations can be seen [here](https://tomasvotruba.com/blog/new-in-ecs-simpler-config) ### 3. Remove imports of `ecs-7.4.php` and/or `ecs-8.0.php` from your `ecs.php` ```diff diff --git a/composer.json b/composer.json index f848c38..f80e8fc 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "nette/utils": "^3.2", "slevomat/coding-standard": "^8.0", "squizlabs/php_codesniffer": "^3.9", - "symplify/easy-coding-standard": "^12.1" + "symplify/easy-coding-standard": "^12.1.4" }, "require-dev": { "ergebnis/composer-normalize": "^2.13.2", diff --git a/ecs.php b/ecs.php index 50ded15..97a62fb 100644 --- a/ecs.php +++ b/ecs.php @@ -459,8 +459,6 @@ ->withConfiguredRule(OperatorSpacingSniff::class, ['ignoreNewlines' => true]) // PHP arrays should be declared using the configured syntax ->withConfiguredRule(ArraySyntaxFixer::class, ['syntax' => 'short']) - // The body of each structure MUST be enclosed by braces. Braces should be properly placed - ->withConfiguredRule(BracesFixer::class, ['allow_single_line_closure' => true, 'allow_single_line_anonymous_class_with_empty_body' => true]) // Class, trait and interface elements must be separated with one or none blank line ->withConfiguredRule(ClassAttributesSeparationFixer::class, ['elements' => ['method' => 'one']]) // Visibility MUST be declared on all properties, methods and class constants