From 4395da47e5ec571122091545636a90c18515aa3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Machulda?= Date: Mon, 13 May 2024 17:55:03 +0200 Subject: [PATCH] Feat: Re-add fixers for property/parameter/return types and constructor property promotion --- ecs.php | 31 +++++++++++++++++++ .../Fixtures/NewPhpFeatures.correct.php.inc | 5 +-- .../Fixtures/NewPhpFeatures.wrong.php.inc | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ecs.php b/ecs.php index 5d49511..f8723da 100644 --- a/ecs.php +++ b/ecs.php @@ -69,6 +69,9 @@ use PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer; use PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer; use PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer; +use PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer; +use PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer; +use PhpCsFixer\Fixer\FunctionNotation\PhpdocToReturnTypeFixer; use PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer; use PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer; use PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer; @@ -130,10 +133,14 @@ use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer; use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer; use PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer; +use SlevomatCodingStandard\Sniffs\Classes\RequireConstructorPropertyPromotionSniff; use SlevomatCodingStandard\Sniffs\ControlStructures\RequireNullSafeObjectOperatorSniff; use SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff; use SlevomatCodingStandard\Sniffs\Functions\RequireTrailingCommaInCallSniff; use SlevomatCodingStandard\Sniffs\Functions\RequireTrailingCommaInDeclarationSniff; +use SlevomatCodingStandard\Sniffs\TypeHints\ParameterTypeHintSniff; +use SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff; +use SlevomatCodingStandard\Sniffs\TypeHints\ReturnTypeHintSniff; use SlevomatCodingStandard\Sniffs\TypeHints\UnionTypeHintFormatSniff; use Symplify\CodingStandard\Fixer\Commenting\ParamReturnAndVarTagMalformsFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; @@ -370,6 +377,18 @@ ReferenceThrowableOnlySniff::class, // The @param, @return, @var and inline @var annotations should keep standard format ParamReturnAndVarTagMalformsFixer::class, + // Takes `@var` annotation of non-mixed types and adjusts accordingly the property signature to a native PHP 7.4+ type-hint. + PhpdocToPropertyTypeFixer::class, + PropertyTypeHintSniff::class, + // Takes `@param` annotations of non-mixed types and adjusts accordingly the function signature to a native type-hints. + PhpdocToParamTypeFixer::class, + ParameterTypeHintSniff::class, + // Takes `@return` annotation of non-mixed types and adjusts accordingly the function signature. + PhpdocToReturnTypeFixer::class, + ReturnTypeHintSniff::class, + // Promote constructor properties + // For php-cs-fixer implementation @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5956 + RequireConstructorPropertyPromotionSniff::class, // switch -> match // @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/5894 @@ -511,6 +530,18 @@ // Allow single line closures ScopeClosingBraceSniff::class . '.ContentBefore' => null, + // Skip unwanted rules from PropertyTypeHintSniff + PropertyTypeHintSniff::class . '.' . PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION => null, + PropertyTypeHintSniff::class . '.' . PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT => null, + + // Skip unwanted rules from ParameterTypeHintSniff + ParameterTypeHintSniff::class . '.' . ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION => null, + ParameterTypeHintSniff::class . '.' . ParameterTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT => null, + + // Skip unwanted rules from ReturnTypeHintSniff + ReturnTypeHintSniff::class . '.' . ReturnTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION => null, + ReturnTypeHintSniff::class . '.' . ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT => null, + // We use declare(strict_types=1); after opening tag BlankLineAfterOpeningTagFixer::class => null, ]); diff --git a/tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc b/tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc index f2962dd..c6e2726 100644 --- a/tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc +++ b/tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc @@ -4,11 +4,8 @@ namespace Lmc\CodingStandard\Integration\Fixtures; class NewPhpFeatures { - private string $someString; - - public function __construct(string $someString) + public function __construct(private string $someString) // RequireConstructorPropertyPromotionSniff { - $this->someString = $someString; } public function php80features( diff --git a/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc b/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc index 3435f7e..49d1d08 100644 --- a/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc +++ b/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc @@ -6,7 +6,7 @@ class NewPhpFeatures { private string $someString; - public function __construct(string $someString) + public function __construct(string $someString) // RequireConstructorPropertyPromotionSniff { $this->someString = $someString; }