From 590e1ba2801593c277ac0196436950db1c1167e1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 23 Nov 2024 00:14:53 +0700 Subject: [PATCH] [Downgrade][Php81][Php80] Handle nullable downgrade initialization on use DowngradeNewInInitializerRector+DowngradePropertyPromotionRector (#243) * [Downgrade][Php81][Php80] Handle nullable downgrade initialization on use DowngradeNewInInitializerRector+DowngradePropertyPromotionRector * [ci-review] Rector Rectify * fix * fix --------- Co-authored-by: GitHub Action --- .../Fixture/new_not_promoted.php.inc | 4 +-- .../DowngradeNewInInitializerRector.php | 3 +- .../Fixture/new_in_array.php.inc | 4 +-- .../DowngradeNullableInitializationTest.php | 28 ++++++++++++++++ .../add_nullable_initialization.php.inc | 32 +++++++++++++++++++ .../config/configured_rule.php | 11 +++++++ 6 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 tests/Issues/DowngradeNullableInitialization/DowngradeNullableInitializationTest.php create mode 100644 tests/Issues/DowngradeNullableInitialization/Fixture/add_nullable_initialization.php.inc create mode 100644 tests/Issues/DowngradeNullableInitialization/config/configured_rule.php diff --git a/rules-tests/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector/Fixture/new_not_promoted.php.inc b/rules-tests/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector/Fixture/new_not_promoted.php.inc index d53a060c..70cd2b6b 100644 --- a/rules-tests/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector/Fixture/new_not_promoted.php.inc +++ b/rules-tests/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector/Fixture/new_not_promoted.php.inc @@ -22,9 +22,9 @@ final class NewNotPromoted { public stdClass $obj; - public function __construct(stdClass $obj = null) + public function __construct(?stdClass $obj = null) { - $obj ??= new stdClass; + $this->obj = $obj ?? new stdClass; $this->obj = $obj; } } diff --git a/rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php b/rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php index 854c3f67..1a2da269 100644 --- a/rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php +++ b/rules/DowngradePhp81/Rector/FunctionLike/DowngradeNewInInitializerRector.php @@ -162,8 +162,7 @@ private function replaceNewInParams(ClassMethod|Closure|Function_ $functionLike) /** @var Variable $paramVar */ $paramVar = $param->var; - // check for property promotion - if ($isConstructor && $param->flags > 0) { + if ($isConstructor) { $propertyFetch = new PropertyFetch(new Variable('this'), $paramVar->name); $coalesce = new Coalesce($param->var, $default); $assign = new Assign($propertyFetch, $coalesce); diff --git a/tests/Issues/DowngradeNewInArrayInitializerPromotion/Fixture/new_in_array.php.inc b/tests/Issues/DowngradeNewInArrayInitializerPromotion/Fixture/new_in_array.php.inc index cbbfd3ce..fb4b6cd2 100644 --- a/tests/Issues/DowngradeNewInArrayInitializerPromotion/Fixture/new_in_array.php.inc +++ b/tests/Issues/DowngradeNewInArrayInitializerPromotion/Fixture/new_in_array.php.inc @@ -33,9 +33,9 @@ class NewInArray /** * @param array $property */ - public function __construct(array $property = null) + public function __construct(?array $property = null) { - $property ??= [ + $this->property = $property ?? [ 'a' => new stdClass() ]; $this->property = $property; diff --git a/tests/Issues/DowngradeNullableInitialization/DowngradeNullableInitializationTest.php b/tests/Issues/DowngradeNullableInitialization/DowngradeNullableInitializationTest.php new file mode 100644 index 00000000..2fce742e --- /dev/null +++ b/tests/Issues/DowngradeNullableInitialization/DowngradeNullableInitializationTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/DowngradeNullableInitialization/Fixture/add_nullable_initialization.php.inc b/tests/Issues/DowngradeNullableInitialization/Fixture/add_nullable_initialization.php.inc new file mode 100644 index 00000000..3a77f94b --- /dev/null +++ b/tests/Issues/DowngradeNullableInitialization/Fixture/add_nullable_initialization.php.inc @@ -0,0 +1,32 @@ + new \PHPStan\Type\BooleanType(), + ] + ) {} +} + +?> +----- +doctrineTypeToScalarType = $doctrineTypeToScalarType ?? [ + 'tinyint' => new \PHPStan\Type\BooleanType(), + ]; + $this->doctrineTypeToScalarType = $doctrineTypeToScalarType; + } +} + +?> diff --git a/tests/Issues/DowngradeNullableInitialization/config/configured_rule.php b/tests/Issues/DowngradeNullableInitialization/config/configured_rule.php new file mode 100644 index 00000000..1e9cfd79 --- /dev/null +++ b/tests/Issues/DowngradeNullableInitialization/config/configured_rule.php @@ -0,0 +1,11 @@ +rules([DowngradeNewInInitializerRector::class, DowngradePropertyPromotionRector::class]); +};