1212use PHPStan \PhpDocParser \Ast \Type \GenericTypeNode ;
1313use PHPStan \PhpDocParser \Ast \Type \IdentifierTypeNode ;
1414use PHPStan \Type \Type ;
15+ use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfo ;
1516use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
1617use Rector \Comments \NodeDocBlock \DocBlockUpdater ;
1718use Rector \Rector \AbstractRector ;
1819use Rector \TypeDeclarationDocblocks \NodeFinder \ArrayDimFetchFinder ;
20+ use Rector \TypeDeclarationDocblocks \TagNodeAnalyzer \UsefulArrayTagNodeAnalyzer ;
1921use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
2022use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2123
@@ -27,7 +29,8 @@ final class AddParamArrayDocblockFromDimFetchAccessRector extends AbstractRector
2729 public function __construct (
2830 private readonly PhpDocInfoFactory $ phpDocInfoFactory ,
2931 private readonly ArrayDimFetchFinder $ arrayDimFetchFinder ,
30- private readonly DocBlockUpdater $ docBlockUpdater
32+ private readonly DocBlockUpdater $ docBlockUpdater ,
33+ private readonly UsefulArrayTagNodeAnalyzer $ usefulArrayTagNodeAnalyzer
3134 ) {
3235 }
3336
@@ -105,7 +108,7 @@ public function refactor(Node $node): ?Node
105108 $ paramTagValueNode = $ phpDocInfo ->getParamTagValueByName ($ paramName );
106109
107110 // already defined, lets skip it
108- if ($ paramTagValueNode instanceof ParamTagValueNode ) {
111+ if ($ this -> usefulArrayTagNodeAnalyzer -> isUsefulArrayTag ( $ paramTagValueNode) ) {
109112 continue ;
110113 }
111114
@@ -130,15 +133,13 @@ public function refactor(Node $node): ?Node
130133 }
131134
132135 if ($ this ->isOnlyStringType ($ keyTypes )) {
133- $ paramTagValueNode = $ this ->createParamTagValueNode ($ paramName , 'string ' );
134- $ phpDocInfo ->addTagValueNode ($ paramTagValueNode );
136+ $ this ->createParamTagValueNode ($ phpDocInfo , $ paramName , 'string ' , $ paramTagValueNode );
135137 $ hasChanged = true ;
136138 continue ;
137139 }
138140
139141 if ($ this ->isOnlyIntegerType ($ keyTypes )) {
140- $ paramTagValueNode = $ this ->createParamTagValueNode ($ paramName , 'int ' );
141- $ phpDocInfo ->addTagValueNode ($ paramTagValueNode );
142+ $ this ->createParamTagValueNode ($ phpDocInfo , $ paramName , 'int ' , $ paramTagValueNode );
142143 $ hasChanged = true ;
143144 continue ;
144145 }
@@ -186,13 +187,22 @@ private function isOnlyIntegerType(array $keyTypes): bool
186187 return true ;
187188 }
188189
189- private function createParamTagValueNode (string $ paramName , string $ keyTypeValue ): ParamTagValueNode
190- {
190+ private function createParamTagValueNode (
191+ PhpDocInfo $ phpDocInfo ,
192+ string $ paramName ,
193+ string $ keyTypeValue ,
194+ ?ParamTagValueNode $ paramTagValueNode
195+ ): void {
191196 $ arrayGenericTypeNode = new GenericTypeNode (new IdentifierTypeNode ('array ' ), [
192197 new IdentifierTypeNode ($ keyTypeValue ),
193198 new IdentifierTypeNode ('mixed ' ),
194199 ]);
195200
196- return new ParamTagValueNode ($ arrayGenericTypeNode , false , '$ ' . $ paramName , '' , false );
201+ if ($ paramTagValueNode instanceof ParamTagValueNode) {
202+ $ paramTagValueNode ->type = $ arrayGenericTypeNode ;
203+ } else {
204+ $ paramTagValueNode = new ParamTagValueNode ($ arrayGenericTypeNode , false , '$ ' . $ paramName , '' , false );
205+ $ phpDocInfo ->addTagValueNode ($ paramTagValueNode );
206+ }
197207 }
198208}
0 commit comments