|
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Param; |
| 9 | +use PhpParser\Node\Stmt\ClassMethod; |
9 | 10 | use PhpParser\Node\Stmt\Property; |
10 | 11 | use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; |
11 | 12 | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
12 | 13 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
13 | 14 | use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 15 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
14 | 16 | use Rector\Privatization\NodeManipulator\VisibilityManipulator; |
15 | 17 | use Rector\Rector\AbstractRector; |
| 18 | +use Rector\ValueObject\MethodName; |
16 | 19 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
17 | 20 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
18 | 21 |
|
@@ -40,7 +43,7 @@ public function __construct( |
40 | 43 | */ |
41 | 44 | public function getNodeTypes(): array |
42 | 45 | { |
43 | | - return [Property::class, Param::class]; |
| 46 | + return [Property::class, ClassMethod::class]; |
44 | 47 | } |
45 | 48 |
|
46 | 49 | public function getRuleDefinition(): RuleDefinition |
@@ -81,30 +84,60 @@ public function __construct() |
81 | 84 | } |
82 | 85 |
|
83 | 86 | /** |
84 | | - * @param Property|Param $node |
| 87 | + * @param Property|ClassMethod $node |
85 | 88 | */ |
86 | 89 | public function refactor(Node $node): ?Node |
87 | 90 | { |
88 | | - if (! $this->visibilityManipulator->isReadonly($node)) { |
| 91 | + if ($node instanceof Property) { |
| 92 | + if (! $this->visibilityManipulator->isReadonly($node)) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + $this->addPhpDocTag($node); |
| 97 | + $this->visibilityManipulator->removeReadonly($node); |
| 98 | + return $node; |
| 99 | + } |
| 100 | + |
| 101 | + if (! $this->isName($node, MethodName::CONSTRUCT)) { |
89 | 102 | return null; |
90 | 103 | } |
91 | 104 |
|
92 | | - $this->addPhpDocTag($node); |
| 105 | + $hasChangedDoc = false; |
| 106 | + $hasChanged = false; |
| 107 | + foreach ($node->params as $param) { |
| 108 | + if (! $this->visibilityManipulator->isReadonly($param)) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + if ($this->addPhpDocTag($param)) { |
| 113 | + $hasChangedDoc = true; |
| 114 | + } |
| 115 | + |
| 116 | + $this->visibilityManipulator->removeReadonly($param); |
| 117 | + $hasChanged = true; |
| 118 | + } |
93 | 119 |
|
94 | | - $this->visibilityManipulator->removeReadonly($node); |
| 120 | + if (! $hasChanged) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + if ($hasChangedDoc) { |
| 125 | + $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); |
| 126 | + } |
95 | 127 |
|
96 | 128 | return $node; |
97 | 129 | } |
98 | 130 |
|
99 | | - private function addPhpDocTag(Property|Param $node): void |
| 131 | + private function addPhpDocTag(Property|Param $node): bool |
100 | 132 | { |
101 | 133 | $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
102 | 134 |
|
103 | 135 | if ($phpDocInfo->hasByName(self::TAGNAME)) { |
104 | | - return; |
| 136 | + return false; |
105 | 137 | } |
106 | 138 |
|
107 | 139 | $phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@' . self::TAGNAME, new GenericTagValueNode(''))); |
108 | 140 | $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 141 | + return true; |
109 | 142 | } |
110 | 143 | } |
0 commit comments