Skip to content

Commit

Permalink
Support @immutable phpdoc in RestoreDefaultNullToNullableTypePropert…
Browse files Browse the repository at this point in the history
…yRector (#5795)

* Added failling test

* Support `@immutable` phpdoc in RestoreDefaultNullToNullableTypePropertyRector
  • Loading branch information
staabm committed Apr 3, 2024
1 parent f889e41 commit 3f1ccb3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector\Fixture;

/**
* @immutable
*/
final class SkipReadonlyPhpdocClass
{
public string|null $display;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->isReadonly()) {
if ($this->isReadonlyClass($node)) {
return null;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ private function shouldSkip(Property $property, Class_ $class): bool
return true;
}

if ($this->isReadonly($property)) {
if ($this->isReadonlyProperty($property)) {
return true;
}

Expand All @@ -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()) {
Expand All @@ -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 !== [];
}
}

0 comments on commit 3f1ccb3

Please sign in to comment.