diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 55c4b3b9a..d54a81f03 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -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 }} diff --git a/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php b/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php index 983ca7719..1ee08a961 100644 --- a/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php +++ b/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php @@ -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); diff --git a/src/TokenRunner/DocBlock/MalformWorker/ParamNameTypoMalformWorker.php b/src/TokenRunner/DocBlock/MalformWorker/ParamNameTypoMalformWorker.php index 750e232ac..675bdc93d 100644 --- a/src/TokenRunner/DocBlock/MalformWorker/ParamNameTypoMalformWorker.php +++ b/src/TokenRunner/DocBlock/MalformWorker/ParamNameTypoMalformWorker.php @@ -37,17 +37,19 @@ 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; } @@ -55,7 +57,7 @@ public function work(string $docContent, Tokens $tokens, int $position): string 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)#'; + + $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; diff --git a/tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong11.php.inc b/tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong11.php.inc new file mode 100644 index 000000000..a8aad0230 --- /dev/null +++ b/tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong11.php.inc @@ -0,0 +1,23 @@ +An existing @param is incorrectly duplicated. + +----- +An existing @param is incorrectly duplicated. + diff --git a/tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong12.php.inc b/tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong12.php.inc new file mode 100644 index 000000000..e6e8abff7 --- /dev/null +++ b/tests/Fixer/Commenting/ParamReturnAndVarTagMalformsFixer/Fixture/wrong12.php.inc @@ -0,0 +1,25 @@ +Wrong @param name is a substring of another param. + +----- +Wrong @param name is a substring of another param. +