Skip to content

Commit 3f1ccb3

Browse files
authored
Support @immutable phpdoc in RestoreDefaultNullToNullableTypePropertyRector (#5795)
* Added failling test * Support `@immutable` phpdoc in RestoreDefaultNullToNullableTypePropertyRector
1 parent f889e41 commit 3f1ccb3

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector\Fixture;
4+
5+
/**
6+
* @immutable
7+
*/
8+
final class SkipReadonlyPhpdocClass
9+
{
10+
public string|null $display;
11+
}

rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getNodeTypes(): array
6464
*/
6565
public function refactor(Node $node): ?Node
6666
{
67-
if ($node->isReadonly()) {
67+
if ($this->isReadonlyClass($node)) {
6868
return null;
6969
}
7070

@@ -108,7 +108,7 @@ private function shouldSkip(Property $property, Class_ $class): bool
108108
return true;
109109
}
110110

111-
if ($this->isReadonly($property)) {
111+
if ($this->isReadonlyProperty($property)) {
112112
return true;
113113
}
114114

@@ -121,7 +121,7 @@ private function shouldSkip(Property $property, Class_ $class): bool
121121
return $this->constructorAssignDetector->isPropertyAssigned($class, $propertyName);
122122
}
123123

124-
private function isReadonly(Property $property): bool
124+
private function isReadonlyProperty(Property $property): bool
125125
{
126126
// native readonly
127127
if ($property->isReadonly()) {
@@ -133,4 +133,17 @@ private function isReadonly(Property $property): bool
133133
$tags = $phpDocInfo->getTagsByName('@readonly');
134134
return $tags !== [];
135135
}
136+
137+
private function isReadonlyClass(Class_ $class): bool
138+
{
139+
// native readonly
140+
if ($class->isReadonly()) {
141+
return true;
142+
}
143+
144+
// @immutable annotation
145+
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
146+
$tags = $phpDocInfo->getTagsByName('@immutable');
147+
return $tags !== [];
148+
}
136149
}

0 commit comments

Comments
 (0)