|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\DeadCode\Rector\Property; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Param; |
| 9 | +use PhpParser\Node\Stmt\Class_; |
| 10 | +use PhpParser\Node\Stmt\Property; |
| 11 | +use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; |
| 12 | +use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
| 13 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 14 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 15 | +use Rector\Privatization\NodeManipulator\VisibilityManipulator; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | + |
| 20 | +/** |
| 21 | + * @see \Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\RemoveUselessReadOnlyTagRectorTest |
| 22 | + */ |
| 23 | +final class RemoveUselessReadOnlyTagRector extends AbstractRector |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly VisibilityManipulator $visibilityManipulator, |
| 27 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 28 | + private readonly DocBlockUpdater $docBlockUpdater |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition('Remove useless @readonly annotation on native readonly type', [ |
| 35 | + new CodeSample( |
| 36 | + <<<'CODE_SAMPLE' |
| 37 | +final class SomeClass |
| 38 | +{ |
| 39 | + /** |
| 40 | + * @readonly |
| 41 | + */ |
| 42 | + private readonly string $name; |
| 43 | +
|
| 44 | + public function __construct(string $name) |
| 45 | + { |
| 46 | + $this->name = $name; |
| 47 | + } |
| 48 | +} |
| 49 | +CODE_SAMPLE |
| 50 | + |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +final class SomeClass |
| 54 | +{ |
| 55 | + private readonly string $name; |
| 56 | +
|
| 57 | + public function __construct(string $name) |
| 58 | + { |
| 59 | + $this->name = $name; |
| 60 | + } |
| 61 | +} |
| 62 | +CODE_SAMPLE |
| 63 | + ), |
| 64 | + ]); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array<class-string<Node>> |
| 69 | + */ |
| 70 | + public function getNodeTypes(): array |
| 71 | + { |
| 72 | + return [Class_::class, Property::class, Param::class]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param Class_|Property|Param $node |
| 77 | + */ |
| 78 | + public function refactor(Node $node): ?Node |
| 79 | + { |
| 80 | + // for param, only on property promotion |
| 81 | + if ($node instanceof Param && $node->flags === 0) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (! $this->visibilityManipulator->isReadonly($node)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
| 90 | + $readonlyDoc = $phpDocInfo->getByName('readonly'); |
| 91 | + if (! $readonlyDoc instanceof PhpDocTagNode) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + if (! $readonlyDoc->value instanceof GenericTagValueNode) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + if ($readonlyDoc->value->value !== '') { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + $phpDocInfo->removeByName('readonly'); |
| 104 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 105 | + |
| 106 | + return $node; |
| 107 | + } |
| 108 | +} |
0 commit comments