Skip to content

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

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ jobs:
# composer install cache - https://github.com/ramsey/composer-install
- uses: "ramsey/composer-install@v2"

# Override code from symplify/coding-standard shipped with ECS
- run: |
rm -rf vendor/symplify/easy-coding-standard/vendor/symplify/coding-standard/src
ln -s $PWD/src vendor/symplify/easy-coding-standard/vendor/symplify/coding-standard/

- run: ${{ matrix.actions.run }}
3 changes: 0 additions & 3 deletions src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ final class BlockFinder
public function findInTokensByEdge(Tokens $tokens, int $position): ?BlockInfo
{
$token = $tokens[$position];
if (! $token instanceof Token) {
return null;
}

if ($token->isGivenKind(T_ATTRIBUTE)) {
return $this->createAttributeBlockInfo($tokens, $position);
Expand Down
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make $argumentNames a list to hold all arguments and add the wrong argument to another argument instead of unset.

}
}

// 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)#';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$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;
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
{
}
?>