@@ -37,25 +37,27 @@ public function work(string $docContent, Tokens $tokens, int $position): string
37
37
38
38
$ paramNames = $ this ->getParamNames ($ docContent );
39
39
40
+ $ missArgumentNames = [];
40
41
// remove correct params
41
42
foreach ($ argumentNames as $ key => $ argumentName ) {
42
43
if (in_array ($ argumentName , $ paramNames , true )) {
43
44
$ paramPosition = array_search ($ argumentName , $ paramNames , true );
44
45
unset($ paramNames [$ paramPosition ]);
45
- unset($ argumentNames [$ key ]);
46
+ } else {
47
+ $ missArgumentNames [$ key ] = $ argumentName ;
46
48
}
47
49
}
48
50
49
51
// nothing to edit, all arguments are correct or there are no more @param annotations
50
- if ($ argumentNames === []) {
52
+ if ($ missArgumentNames === []) {
51
53
return $ docContent ;
52
54
}
53
55
54
56
if ($ paramNames === []) {
55
57
return $ docContent ;
56
58
}
57
59
58
- return $ this ->fixTypos ($ argumentNames , $ paramNames , $ docContent );
60
+ return $ this ->fixTypos ($ argumentNames , $ missArgumentNames , $ paramNames , $ docContent );
59
61
}
60
62
61
63
/**
@@ -93,20 +95,36 @@ private function getAnnotationsOfType(string $docContent, string $type): array
93
95
94
96
/**
95
97
* @param string[] $argumentNames
98
+ * @param string[] $missArgumentNames
96
99
* @param string[] $paramNames
97
100
*/
98
- private function fixTypos (array $ argumentNames , array $ paramNames , string $ docContent ): string
101
+ private function fixTypos (array $ argumentNames , array $ missArgumentNames , array $ paramNames , string $ docContent ): string
99
102
{
100
- foreach ($ argumentNames as $ key => $ argumentName ) {
103
+ // A table of permuted params. initialized by $argumentName instead of $paramNames is correct
104
+ $ replacedParams = array_fill_keys ($ argumentNames , false );
105
+
106
+ foreach ($ missArgumentNames as $ key => $ argumentName ) {
101
107
// 1. the same position
102
108
if (! isset ($ paramNames [$ key ])) {
103
109
continue ;
104
110
}
105
111
106
112
$ typoName = $ paramNames [$ key ];
107
- $ replacePattern = '#@param(.*?) ' . preg_quote ($ typoName , '# ' ) . '# ' ;
113
+ $ replacePattern = '#@param(.*?)( ' . preg_quote ($ typoName , '# ' ) . '\b)# ' ;
114
+
115
+ $ docContent = Strings::replace ($ docContent , $ replacePattern , static function ($ matched ) use ($ argumentName , &$ replacedParams ) {
116
+ $ paramName = $ matched [2 ];
117
+
118
+ // 2. If the PHPDoc $paramName is one of the existing $argumentNames and has not already been replaced, it will be deferred
119
+ if (isset ($ replacedParams [$ paramName ]) && ! $ replacedParams [$ paramName ]) {
120
+ $ replacedParams [$ paramName ] = true ;
121
+
122
+ return $ matched [0 ];
123
+ }
108
124
109
- $ docContent = Strings::replace ($ docContent , $ replacePattern , '@param$1 ' . $ argumentName );
125
+ // 3. Otherwise, replace $paramName with $argumentName in the @param line
126
+ return sprintf ('@param%s%s ' , $ matched [1 ], $ argumentName );
127
+ });
110
128
}
111
129
112
130
return $ docContent ;
0 commit comments