Skip to content

Commit

Permalink
[Php81] Remove @readonly doc on transformation to native readonly on …
Browse files Browse the repository at this point in the history
…ReadOnlyPropertyRector (#5789)

* [Php81] Remove @readonly doc on transformation to native readonly on ReadOnlyPropertyRector

* add test for prop promotion

* more test

* fix phpstan
  • Loading branch information
samsonasik committed Apr 3, 2024
1 parent 9b4ad93 commit 717e3e0
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class KeepReadonlyDocOnPropertyPromotionWithDescription
{
private function __construct(
/**
* @readonly some desc
*/
private string $id
){}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class KeepReadonlyDocOnPropertyPromotionWithDescription
{
private function __construct(
/**
* @readonly some desc
*/
private readonly string $id
){}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class KeepReadonlyDocOnPropertyWithDescription
{
/**
* @readonly some description
*/
private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class KeepReadonlyDocOnPropertyWithDescription
{
/**
* @readonly some description
*/
private readonly string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class RemoveReadonlyDocOnProperty
{
/**
* @readonly
*/
private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class RemoveReadonlyDocOnProperty
{
private readonly string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class WithReadonlyDocOnPropertyPromotion
{
private function __construct(
/** @readonly */
private string $id
){}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class WithReadonlyDocOnPropertyPromotion
{
private function __construct(
private readonly string $id
){}
}

?>
34 changes: 33 additions & 1 deletion rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeAnalyzer\ParamAnalyzer;
use Rector\NodeManipulator\PropertyFetchAssignManipulator;
use Rector\NodeManipulator\PropertyManipulator;
Expand All @@ -42,7 +46,9 @@ public function __construct(
private readonly PropertyFetchAssignManipulator $propertyFetchAssignManipulator,
private readonly ParamAnalyzer $paramAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly BetterNodeFinder $betterNodeFinder
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DocBlockUpdater $docBlockUpdater
) {
}

Expand Down Expand Up @@ -171,9 +177,32 @@ private function refactorProperty(Class_ $class, Property $property, Scope $scop
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

$this->removeReadOnlyDoc($property);

return $property;
}

private function removeReadOnlyDoc(Property|Param $node): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

$readonlyDoc = $phpDocInfo->getByName('readonly');
if (! $readonlyDoc instanceof PhpDocTagNode) {
return;
}

if (! $readonlyDoc->value instanceof GenericTagValueNode) {
return;
}

if ($readonlyDoc->value->value !== '') {
return;
}

$phpDocInfo->removeByName('readonly');
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
}

private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $param, Scope $scope): Param | null
{
if (! $this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
Expand Down Expand Up @@ -206,6 +235,9 @@ private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $p
}

$this->visibilityManipulator->makeReadonly($param);

$this->removeReadOnlyDoc($param);

return $param;
}

Expand Down
21 changes: 21 additions & 0 deletions src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ public function removeByType(string $typeToRemove): bool
return $hasChanged;
}

public function removeByName(string $tagName): bool
{
$tagName = '@' . ltrim($tagName, '@');
$hasChanged = false;

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use (
$tagName,
&$hasChanged
): ?int {
if ($node instanceof PhpDocTagNode && $node->name === $tagName) {
$hasChanged = true;
return PhpDocNodeTraverser::NODE_REMOVE;
}

return null;
});

return $hasChanged;
}

public function addTagValueNode(PhpDocTagValueNode $phpDocTagValueNode): void
{
if ($phpDocTagValueNode instanceof DoctrineAnnotationTagValueNode) {
Expand Down

0 comments on commit 717e3e0

Please sign in to comment.