-
-
Notifications
You must be signed in to change notification settings - Fork 24
Make handle @param duplicates and word boundaries correct #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,25 +37,27 @@ public function work(string $docContent, Tokens $tokens, int $position): string | |
|
||
$paramNames = $this->getParamNames($docContent); | ||
|
||
$missArgumentNames = []; | ||
// remove correct params | ||
foreach ($argumentNames as $key => $argumentName) { | ||
if (in_array($argumentName, $paramNames, true)) { | ||
$paramPosition = array_search($argumentName, $paramNames, true); | ||
unset($paramNames[$paramPosition]); | ||
unset($argumentNames[$key]); | ||
} else { | ||
$missArgumentNames[$key] = $argumentName; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make |
||
} | ||
} | ||
|
||
// nothing to edit, all arguments are correct or there are no more @param annotations | ||
if ($argumentNames === []) { | ||
if ($missArgumentNames === []) { | ||
return $docContent; | ||
} | ||
|
||
if ($paramNames === []) { | ||
return $docContent; | ||
} | ||
|
||
return $this->fixTypos($argumentNames, $paramNames, $docContent); | ||
return $this->fixTypos($argumentNames, $missArgumentNames, $paramNames, $docContent); | ||
} | ||
|
||
/** | ||
|
@@ -93,20 +95,36 @@ private function getAnnotationsOfType(string $docContent, string $type): array | |
|
||
/** | ||
* @param string[] $argumentNames | ||
* @param string[] $missArgumentNames | ||
* @param string[] $paramNames | ||
*/ | ||
private function fixTypos(array $argumentNames, array $paramNames, string $docContent): string | ||
private function fixTypos(array $argumentNames, array $missArgumentNames, array $paramNames, string $docContent): string | ||
{ | ||
foreach ($argumentNames as $key => $argumentName) { | ||
// A table of permuted params. initialized by $argumentName instead of $paramNames is correct | ||
$replacedParams = array_fill_keys($argumentNames, false); | ||
|
||
foreach ($missArgumentNames as $key => $argumentName) { | ||
// 1. the same position | ||
if (! isset($paramNames[$key])) { | ||
continue; | ||
} | ||
|
||
$typoName = $paramNames[$key]; | ||
$replacePattern = '#@param(.*?)' . preg_quote($typoName, '#') . '#'; | ||
$replacePattern = '#@param(.*?)(' . preg_quote($typoName, '#') . '\b)#'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add https://www.php.net/manual/en/regexp.reference.escape.php#:~:text=%5Cb-,word%20boundary,-%5CB |
||
|
||
$docContent = Strings::replace($docContent, $replacePattern, static function ($matched) use ($argumentName, &$replacedParams) { | ||
$paramName = $matched[2]; | ||
|
||
// 2. If the PHPDoc $paramName is one of the existing $argumentNames and has not already been replaced, it will be deferred | ||
if (isset($replacedParams[$paramName]) && ! $replacedParams[$paramName]) { | ||
$replacedParams[$paramName] = true; | ||
|
||
return $matched[0]; | ||
} | ||
|
||
$docContent = Strings::replace($docContent, $replacePattern, '@param$1' . $argumentName); | ||
// 3. Otherwise, replace $paramName with $argumentName in the @param line | ||
return sprintf('@param%s%s', $matched[1], $argumentName); | ||
}); | ||
} | ||
|
||
return $docContent; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
An existing @param is incorrectly duplicated. | ||
<?php | ||
|
||
/** | ||
* @param string $one | ||
* @param string $one | ||
*/ | ||
function someFunction($one, $two): void | ||
{ | ||
} | ||
?> | ||
----- | ||
An existing @param is incorrectly duplicated. | ||
<?php | ||
|
||
/** | ||
* @param string $one | ||
* @param string $two | ||
*/ | ||
function someFunction($one, $two): void | ||
{ | ||
} | ||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Wrong @param name is a substring of another param. | ||
<?php | ||
|
||
/** | ||
* @param string $foooo | ||
* @param string $fooooo | ||
* @param string $foooooo | ||
*/ | ||
function someFunction($foo, $fooo): void | ||
{ | ||
} | ||
?> | ||
----- | ||
Wrong @param name is a substring of another param. | ||
<?php | ||
|
||
/** | ||
* @param string $foo | ||
* @param string $fooo | ||
* @param string $foooooo | ||
*/ | ||
function someFunction($foo, $fooo): void | ||
{ | ||
} | ||
?> |
Uh oh!
There was an error while loading. Please reload this page.