From f77ef49b03d73e92585997924d64908253b7f23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Machulda?= Date: Thu, 9 May 2024 18:25:05 +0200 Subject: [PATCH] Feat: Add NullableTypeDeclarationForDefaultNullValueFixer (part of #94) --- ecs.php | 3 +++ tests/Integration/Fixtures/Basic.correct.php.inc | 8 ++++++-- tests/Integration/Fixtures/Basic.wrong.php.inc | 9 ++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ecs.php b/ecs.php index dea4872..b7b4732 100644 --- a/ecs.php +++ b/ecs.php @@ -77,6 +77,7 @@ use PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer; use PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer; use PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer; +use PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer; use PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer; use PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer; use PhpCsFixer\Fixer\FunctionNotation\PhpdocToReturnTypeFixer; @@ -295,6 +296,8 @@ NoUnreachableDefaultArgumentValueFixer::class, // There must be no `sprintf` calls with only the first argument. NoUselessSprintfFixer::class, + // Add `?` before single type declarations when parameters have a default null value. + NullableTypeDeclarationForDefaultNullValueFixer::class, // There must not be a space before colon in return type declarations. ReturnTypeDeclarationFixer::class, // Add `void` return type to functions with missing or empty return statements. diff --git a/tests/Integration/Fixtures/Basic.correct.php.inc b/tests/Integration/Fixtures/Basic.correct.php.inc index e714ccc..ae519cf 100644 --- a/tests/Integration/Fixtures/Basic.correct.php.inc +++ b/tests/Integration/Fixtures/Basic.correct.php.inc @@ -169,6 +169,10 @@ class Basic extends AbstractBasic implements InterfaceFromThisNamespace // Fully ); } - // NullableTypeDeclarationFixer - public function withParameters(?string $nullableValue): void {} + public function withParameters( + // NullableTypeDeclarationFixer + ?string $nullableValue, + // NullableTypeDeclarationForDefaultNullValueFixer + ?string $anotherNullableValue = null, + ): void {} } diff --git a/tests/Integration/Fixtures/Basic.wrong.php.inc b/tests/Integration/Fixtures/Basic.wrong.php.inc index 8cd6609..676a508 100644 --- a/tests/Integration/Fixtures/Basic.wrong.php.inc +++ b/tests/Integration/Fixtures/Basic.wrong.php.inc @@ -166,7 +166,10 @@ but should be heredoc instead'; ); } - // NullableTypeDeclarationFixer - public function withParameters(null|string $nullableValue): void - {} + public function withParameters( + // NullableTypeDeclarationFixer + null|string $nullableValue, + // NullableTypeDeclarationForDefaultNullValueFixer + string $anotherNullableValue = null, + ): void {} }