From 3f1ccb3ea131997a435fc2211620e968c6732220 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 3 Apr 2024 13:19:17 +0200 Subject: [PATCH] Support @immutable phpdoc in RestoreDefaultNullToNullableTypePropertyRector (#5795) * Added failling test * Support `@immutable` phpdoc in RestoreDefaultNullToNullableTypePropertyRector --- .../skip_readonly_phpdoc_class.php.inc | 11 +++++++++++ ...efaultNullToNullableTypePropertyRector.php | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_readonly_phpdoc_class.php.inc diff --git a/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_readonly_phpdoc_class.php.inc b/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_readonly_phpdoc_class.php.inc new file mode 100644 index 00000000000..846e5ab5a46 --- /dev/null +++ b/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_readonly_phpdoc_class.php.inc @@ -0,0 +1,11 @@ +isReadonly()) { + if ($this->isReadonlyClass($node)) { return null; } @@ -108,7 +108,7 @@ private function shouldSkip(Property $property, Class_ $class): bool return true; } - if ($this->isReadonly($property)) { + if ($this->isReadonlyProperty($property)) { return true; } @@ -121,7 +121,7 @@ private function shouldSkip(Property $property, Class_ $class): bool return $this->constructorAssignDetector->isPropertyAssigned($class, $propertyName); } - private function isReadonly(Property $property): bool + private function isReadonlyProperty(Property $property): bool { // native readonly if ($property->isReadonly()) { @@ -133,4 +133,17 @@ private function isReadonly(Property $property): bool $tags = $phpDocInfo->getTagsByName('@readonly'); return $tags !== []; } + + private function isReadonlyClass(Class_ $class): bool + { + // native readonly + if ($class->isReadonly()) { + return true; + } + + // @immutable annotation + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class); + $tags = $phpDocInfo->getTagsByName('@immutable'); + return $tags !== []; + } }