From b052ed9ca971f97105ac93712853ea5b897d4dd3 Mon Sep 17 00:00:00 2001 From: Maxim Tugaev Date: Tue, 19 Mar 2024 04:18:04 +0300 Subject: [PATCH] Handle readonly class with attrs (#5736) --- .../with_attribute_on_property.php.inc | 23 ++++++++++++++++ ...th_attribute_on_property_promotion.php.inc | 27 +++++++++++++++++++ .../Rector/Class_/ReadOnlyClassRector.php | 10 +++++++ 3 files changed, 60 insertions(+) create mode 100644 rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property.php.inc create mode 100644 rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property_promotion.php.inc diff --git a/rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property.php.inc b/rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property.php.inc new file mode 100644 index 00000000000..f8bb54f7311 --- /dev/null +++ b/rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property.php.inc @@ -0,0 +1,23 @@ + +----- + diff --git a/rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property_promotion.php.inc b/rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property_promotion.php.inc new file mode 100644 index 00000000000..c9eff6216d0 --- /dev/null +++ b/rules-tests/Php82/Rector/Class_/ReadOnlyClassRector/Fixture/with_attribute_on_property_promotion.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/Php82/Rector/Class_/ReadOnlyClassRector.php b/rules/Php82/Rector/Class_/ReadOnlyClassRector.php index 551bee1a55c..6596fbde376 100644 --- a/rules/Php82/Rector/Class_/ReadOnlyClassRector.php +++ b/rules/Php82/Rector/Class_/ReadOnlyClassRector.php @@ -94,11 +94,21 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node if ($constructClassMethod instanceof ClassMethod) { foreach ($constructClassMethod->getParams() as $param) { $this->visibilityManipulator->removeReadonly($param); + + if ($param->attrGroups !== []) { + // invoke reprint with correct newline + $param->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } } } foreach ($node->getProperties() as $property) { $this->visibilityManipulator->removeReadonly($property); + + if ($property->attrGroups !== []) { + // invoke reprint with correct newline + $property->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } } if ($node->attrGroups !== []) {