From 5bf804fc23048ca8a17f3872ab4fb29456ba7356 Mon Sep 17 00:00:00 2001 From: Samuel Maudo Date: Thu, 7 Mar 2024 09:09:57 +0100 Subject: [PATCH] Review Codely's rules --- README.md | 59 +++++++++- composer.json | 2 +- sets/base.php | 101 ++++++++++++++++++ sets/libraries.php | 2 +- sets/projects.php | 80 ++------------ src/CodingStyle.php | 1 + ...tilineWhitespaceBeforeDoubleColonFixer.php | 2 +- 7 files changed, 172 insertions(+), 75 deletions(-) create mode 100644 sets/base.php diff --git a/README.md b/README.md index d6645ca..4c5d66c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,62 @@ Hereldar's Easy Coding Standards configuration [license-badge]: https://img.shields.io/badge/license-MIT-brightgreen.svg [license-url]: LICENSE -Opinionated linting configuration considering modern PHP best -practices and providing consistency. +Opinionated linting configuration inspired on [Codely's Coding Style](https://github.com/CodelyTV/php-coding_style-codely). + +How to use +---------- + +### Installation + +Install the package via Composer: + +```bash +composer require --dev hereldar/coding-style +``` + +### Configuration + +Create a `ecs.php` file in the root of your project: + +```php +use Hereldar\CodingStyle; +use Symplify\EasyCodingStandard\Config\ECSConfig; + +return ECSConfig + ::configure() + ->withSets([ + CodingStyle::PROJECTS, + ]) + ->withPaths([ + __DIR__.'/src', + __DIR__.'/tests', + ]) + ->withRootFiles(); +``` + +### Usage + +Execute the following command to see the suggested changes: + +```bash +vendor/bin/ecs +``` + +To actually fix your code, add `--fix`: + +```bash +vendor/bin/ecs --fix +``` + +For more information, check the [Easy Coding Standard documentation](https://github.com/easy-coding-standard/easy-coding-standard) + +What it does +------------ + +Checks the code style of your project using: + +- [PER Coding Style](https://www.php-fig.org/per/coding-style/) +- [Symfony](https://symfony.com/doc/current/contributing/code/standards.html) +- Some custom rules (you can see them [here](sets)). **Currently under development.** diff --git a/composer.json b/composer.json index 4a30d9f..b86bb53 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "symplify/easy-coding-standard": "^12.0" + "symplify/easy-coding-standard": "^12.1" }, "autoload": { "psr-4": { diff --git a/sets/base.php b/sets/base.php new file mode 100644 index 0000000..f08493e --- /dev/null +++ b/sets/base.php @@ -0,0 +1,101 @@ +withPhpCsFixerSets( + php80Migration: true, + php80MigrationRisky: true, + per: true, + perRisky: true, + symfony: true, + symfonyRisky: true, + ) + ->withRules([ + ArrayIndentationFixer::class, + BlankLineAfterStrictTypesFixer::class, + ClarifyingParenthesesAroundComparisonsFixer::class, + CombineConsecutiveIssetsFixer::class, + CombineConsecutiveUnsetsFixer::class, + ExplicitStringVariableFixer::class, + MethodChainingIndentationFixer::class, + MultilineCommentOpeningClosingFixer::class, + MultilineWhitespaceBeforeDoubleColonFixer::class, + NoUselessElseFixer::class, + PhpdocOrderByValueFixer::class, + PhpUnitDedicateAssertFixer::class, + PhpUnitDedicateAssertInternalTypeFixer::class, + PhpUnitExpectationFixer::class, + SelfStaticAccessorFixer::class, + SimplifiedIfReturnFixer::class, + SingleLineEmptyBodyFixer::class, + ]) + ->withConfiguredRule( + GlobalNamespaceImportFixer::class, + ['import_classes' => true, 'import_constants' => false, 'import_functions' => false], + ) + ->withConfiguredRule( + OperatorLinebreakFixer::class, + ['position' => 'beginning'], + ) + ->withConfiguredRule( + PhpdocAlignFixer::class, + ['align' => 'left'], + ) + ->withConfiguredRule( + PhpdocNoAliasTagFixer::class, + ['replacements' => ['type' => 'var', 'link' => 'see']], + ) + ->withConfiguredRule( + PhpdocSeparationFixer::class, + ['groups' => [ + ['deprecated', 'link', 'see', 'since'], + ['author', 'copyright', 'license'], + ['category', 'package', 'subpackage'], + ['property', 'property-read', 'property-write', 'phpstan-property', 'phpstan-property-read', 'phpstan-property-write', 'psalm-property', 'psalm-property-read', 'psalm-property-write'], + ['pure', 'phpstan-pure', 'psalm-pure'], + ['param', 'phpstan-param', 'psalm-param'], + ['return', 'phpstan-return', 'psalm-return'], + ['throws', 'phpstan-throws', 'psalm-throws'], + ]], + ) + ->withConfiguredRule( + PhpdocToCommentFixer::class, + ['ignored_tags' => ['var', 'phpstan-var', 'psalm-var', 'phpstan-ignore-next-line', 'psalm-suppress']], + ) + ->withConfiguredRule( + StringImplicitBackslashesFixer::class, + ['single_quoted' => 'ignore'], + ) + ->withSpacing( + indentation: ECS::INDENTATION_SPACES, + lineEnding: PHP_EOL + ); diff --git a/sets/libraries.php b/sets/libraries.php index 7585a9f..719a030 100644 --- a/sets/libraries.php +++ b/sets/libraries.php @@ -11,7 +11,7 @@ return ECSConfig ::configure() ->withSets([ - CodingStyle::PROJECTS, + CodingStyle::BASE, ]) ->withConfiguredRule( NativeConstantInvocationFixer::class, diff --git a/sets/projects.php b/sets/projects.php index be5c273..8623326 100644 --- a/sets/projects.php +++ b/sets/projects.php @@ -2,89 +2,29 @@ declare(strict_types=1); -use Hereldar\CodingStyle\Rules\ClarifyingParenthesesAroundComparisonsFixer; -use Hereldar\CodingStyle\Rules\MultilineWhitespaceBeforeDoubleColonFixer; -use PhpCsFixer\Fixer\Comment\MultilineCommentOpeningClosingFixer; +use Hereldar\CodingStyle; +use PhpCsFixer\Fixer\ClassNotation\FinalClassFixer; +use PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer; +use PhpCsFixer\Fixer\ClassUsage\DateTimeImmutableFixer; use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer; use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer; use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer; -use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer; -use PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveIssetsFixer; -use PhpCsFixer\Fixer\LanguageConstruct\CombineConsecutiveUnsetsFixer; -use PhpCsFixer\Fixer\Operator\OperatorLinebreakFixer; -use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer; -use PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer; -use PhpCsFixer\Fixer\Phpdoc\PhpdocOrderByValueFixer; -use PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer; -use PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer; -use PhpCsFixer\Fixer\StringNotation\ExplicitStringVariableFixer; -use PhpCsFixer\Fixer\StringNotation\StringImplicitBackslashesFixer; -use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer; -use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; return ECSConfig ::configure() - ->withPhpCsFixerSets( - php80Migration: true, - php80MigrationRisky: true, - per: true, - perRisky: true, - symfony: true, - symfonyRisky: true, - ) + ->withSets([ + CodingStyle::BASE, + ]) ->withSkip([ NativeConstantInvocationFixer::class, NativeFunctionInvocationFixer::class, ]) ->withRules([ - ArrayIndentationFixer::class, - ClarifyingParenthesesAroundComparisonsFixer::class, - CombineConsecutiveIssetsFixer::class, - CombineConsecutiveUnsetsFixer::class, - ExplicitStringVariableFixer::class, - MethodChainingIndentationFixer::class, - MultilineCommentOpeningClosingFixer::class, - MultilineWhitespaceBeforeDoubleColonFixer::class, - PhpdocOrderByValueFixer::class, + DateTimeImmutableFixer::class, + FinalClassFixer::class, + ProtectedToPrivateFixer::class, ]) - ->withConfiguredRule( - GlobalNamespaceImportFixer::class, - ['import_classes' => true, 'import_constants' => false, 'import_functions' => false], - ) - ->withConfiguredRule( - OperatorLinebreakFixer::class, - ['position' => 'beginning'], - ) - ->withConfiguredRule( - PhpdocAlignFixer::class, - ['align' => 'left'], - ) - ->withConfiguredRule( - PhpdocNoAliasTagFixer::class, - ['replacements' => ['type' => 'var', 'link' => 'see']], - ) - ->withConfiguredRule( - PhpdocSeparationFixer::class, - ['groups' => [ - ['deprecated', 'link', 'see', 'since'], - ['author', 'copyright', 'license'], - ['category', 'package', 'subpackage'], - ['property', 'property-read', 'property-write', 'phpstan-property', 'phpstan-property-read', 'phpstan-property-write', 'psalm-property', 'psalm-property-read', 'psalm-property-write'], - ['pure', 'phpstan-pure', 'psalm-pure'], - ['param', 'phpstan-param', 'psalm-param'], - ['return', 'phpstan-return', 'psalm-return'], - ['throws', 'phpstan-throws', 'psalm-throws'], - ]], - ) - ->withConfiguredRule( - PhpdocToCommentFixer::class, - ['ignored_tags' => ['var', 'phpstan-var', 'psalm-var', 'phpstan-ignore-next-line', 'psalm-suppress']], - ) - ->withConfiguredRule( - StringImplicitBackslashesFixer::class, - ['single_quoted' => 'ignore'], - ) ->withConfiguredRule( YodaStyleFixer::class, ['equal' => false, 'identical' => false, 'less_and_greater' => false], diff --git a/src/CodingStyle.php b/src/CodingStyle.php index 5ae03e8..68c30c8 100644 --- a/src/CodingStyle.php +++ b/src/CodingStyle.php @@ -6,6 +6,7 @@ final class CodingStyle { + public const BASE = __DIR__.'/../sets/base.php'; public const PROJECTS = __DIR__.'/../sets/projects.php'; public const LIBRARIES = __DIR__.'/../sets/libraries.php'; } diff --git a/src/CodingStyle/Rules/MultilineWhitespaceBeforeDoubleColonFixer.php b/src/CodingStyle/Rules/MultilineWhitespaceBeforeDoubleColonFixer.php index 80a41f0..ecbeb6f 100644 --- a/src/CodingStyle/Rules/MultilineWhitespaceBeforeDoubleColonFixer.php +++ b/src/CodingStyle/Rules/MultilineWhitespaceBeforeDoubleColonFixer.php @@ -98,7 +98,7 @@ private function findIndentBeforeNextObjectOperators(int $index, Tokens $tokens) $previousIndex = $index - 1; $previous = $tokens[$previousIndex]; - if (str_contains($previous->getContent(), "\n")) { + if (\str_contains($previous->getContent(), "\n")) { return WhitespacesAnalyzer::detectIndent($tokens, $index); } }