From 0f9dd92f6c206568dbeb948f98355ecaaff3d205 Mon Sep 17 00:00:00 2001 From: Dominik Peters Date: Thu, 22 Jul 2021 19:07:09 +0200 Subject: [PATCH] Fix ClassPropertyAssignToConstructorPromotionRector to skip properties that have attributes (#468) * Add failing test fixture for ClassPropertyAssignToConstructorPromotionRector # Failing Test for ClassPropertyAssignToConstructorPromotionRector Based on https://getrector.org/demo/1ebe95c8-2d22-6d0e-b6ff-7be843d783d4 The ClassPropertyAssignToConstructorPromotionRector should ignore properties that have properties linked to them. Looking at the demo it seems to have gotten rid of both properties, which I did not expect/want it to do * Update coordinate.php.inc reverse order of use statement and namespace * fix: skip properties that have attributes * copy over attrGroups from to to keep them * added test case for multiple attributes Co-authored-by: Dominik Peters --- ...n_attribute_and_attribute_property.php.inc | 38 +++++++++++++++++++ .../Fixture/property_with_attribute.php.inc | 35 +++++++++++++++++ .../property_with_multiple_attributes.php.inc | 38 +++++++++++++++++++ ...ertyAssignToConstructorPromotionRector.php | 2 + 4 files changed, 113 insertions(+) create mode 100644 rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/mix_non_attribute_and_attribute_property.php.inc create mode 100644 rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_attribute.php.inc create mode 100644 rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_multiple_attributes.php.inc diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/mix_non_attribute_and_attribute_property.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/mix_non_attribute_and_attribute_property.php.inc new file mode 100644 index 00000000000..8cc64e9923c --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/mix_non_attribute_and_attribute_property.php.inc @@ -0,0 +1,38 @@ +latitude = $latitude; + $this->longitude = $longitude; + } +} +?> +----- + diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_attribute.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_attribute.php.inc new file mode 100644 index 00000000000..289ce025b77 --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_attribute.php.inc @@ -0,0 +1,35 @@ +latitude = $latitude; + } +} +?> +----- + diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_multiple_attributes.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_multiple_attributes.php.inc new file mode 100644 index 00000000000..1e2f7e01d6c --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/property_with_multiple_attributes.php.inc @@ -0,0 +1,38 @@ +latitude = $latitude; + } +} +?> +----- + diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index ddff80eae97..8fb9be04ef3 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -129,6 +129,8 @@ public function refactor(Node $node): ?Node $propertyName = $this->getName($property); $param->var->name = $propertyName; $param->flags = $property->flags; + // Copy over attributes of the "old" property + $param->attrGroups = $property->attrGroups; $this->processNullableType($property, $param); }