From a07f453362987f620a51683b8d2ec1a247278986 Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Fri, 4 Feb 2022 12:11:11 +0100 Subject: [PATCH] NullableTypeDeclarationForDefaultNullValueFixer - handle "readonly" as constructor property modifier --- ...ypeDeclarationForDefaultNullValueFixer.php | 21 +++++++++++-------- ...eclarationForDefaultNullValueFixerTest.php | 19 ++++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixer.php b/src/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixer.php index 8c5faa8b711..1d12b109530 100644 --- a/src/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixer.php +++ b/src/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixer.php @@ -121,6 +121,16 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void */ private function fixFunctionParameters(Tokens $tokens, array $arguments): void { + $constructorPropertyModifiers = [ + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, + ]; + + if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required + $constructorPropertyModifiers[] = T_READONLY; + } + foreach (array_reverse($arguments) as $argumentInfo) { if ( // Skip, if the parameter @@ -136,17 +146,10 @@ private function fixFunctionParameters(Tokens $tokens, array $arguments): void $argumentTypeInfo = $argumentInfo->getTypeAnalysis(); - if ( - \PHP_VERSION_ID >= 80000 - && false === $this->configuration['use_nullable_type_declaration'] - ) { + if (\PHP_VERSION_ID >= 80000 && false === $this->configuration['use_nullable_type_declaration']) { $visibility = $tokens[$tokens->getPrevMeaningfulToken($argumentTypeInfo->getStartIndex())]; - if ($visibility->isGivenKind([ - CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, - CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, - CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, - ])) { + if ($visibility->isGivenKind($constructorPropertyModifiers)) { continue; } } diff --git a/tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php b/tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php index c086d0a92b5..a98e10f7ae0 100644 --- a/tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php +++ b/tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php @@ -505,8 +505,12 @@ function foo2(?string &/*comment*/$param2 = null) {} * @dataProvider provideFix81Cases * @requires PHP 8.1 */ - public function testFix81(string $expected): void + public function testFix81(string $expected, ?array $config): void { + if (null !== $config) { + $this->fixer->configure($config); + } + $this->doTest($expected); } @@ -521,6 +525,19 @@ public function __construct( ) {} } ', + null, + ]; + + yield [ + ' false], ]; } }