From ca696bc82163adf4191429af248ac9e06c2409fc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 17 Jan 2022 03:06:23 +0700 Subject: [PATCH] [DowngradePhp74] Do not remove non-null default value on nullable on DowngradeTypedPropertyRector (#1687) --- ...ullable_type_with_default_not_null.php.inc | 24 +++++++++++++++++++ .../Property/DowngradeTypedPropertyRector.php | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector/Fixture/nullable_type_with_default_not_null.php.inc diff --git a/rules-tests/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector/Fixture/nullable_type_with_default_not_null.php.inc b/rules-tests/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector/Fixture/nullable_type_with_default_not_null.php.inc new file mode 100644 index 00000000000..d7120047e81 --- /dev/null +++ b/rules-tests/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector/Fixture/nullable_type_with_default_not_null.php.inc @@ -0,0 +1,24 @@ + +----- + diff --git a/rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php b/rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php index fbfec39ec9a..f8b3e82c929 100644 --- a/rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php +++ b/rules/DowngradePhp74/Rector/Property/DowngradeTypedPropertyRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\ComplexType; +use PhpParser\Node\Expr; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\NullableType; @@ -66,7 +67,8 @@ public function refactor(Node $node): ?Node return null; } - if ($node->type instanceof NullableType) { + $default = $node->props[0]->default; + if ($node->type instanceof NullableType && $default instanceof Expr && $this->valueResolver->isNull($default)) { $node->props[0]->default = null; }