Skip to content

Commit

Permalink
Make handle @param duplicates and word boundaries correct
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed Apr 30, 2023
1 parent 96f6eda commit 821b31c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

// 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);
}

/**
Expand Down Expand Up @@ -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)#';

$docContent = Strings::replace($docContent, $replacePattern, 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 "@param{$matched[1]}$argumentName";
});
}

return $docContent;
Expand Down
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
{
}
?>

0 comments on commit 821b31c

Please sign in to comment.