diff --git a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php index 9608c2d4194..9b1762dba8b 100644 --- a/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php +++ b/src/Psalm/Internal/FileManipulation/FunctionDocblockManipulator.php @@ -17,7 +17,9 @@ use function array_key_exists; use function array_merge; use function array_reduce; +use function array_slice; use function count; +use function implode; use function is_string; use function ltrim; use function preg_match; @@ -332,7 +334,29 @@ private function getDocblock(): string foreach ($parsed_docblock->tags['param'] as &$param_block) { $doc_parts = CommentAnalyzer::splitDocLine($param_block); + // If there's no type + if (($doc_parts[0] ?? null) === '$' . $param_name) { + // If the parameter has a description add that back + if (count($doc_parts) > 1) { + $new_param_block .= " ". implode(" ", array_slice($doc_parts, 1)); + } + + if ($param_block !== $new_param_block) { + $modified_docblock = true; + } + + $param_block = $new_param_block; + $found_in_params = true; + break; + } + + // If there is a type if (($doc_parts[1] ?? null) === '$' . $param_name) { + // If the parameter has a description add that back + if (count($doc_parts) > 2) { + $new_param_block .= " ". implode(" ", array_slice($doc_parts, 2)); + } + if ($param_block !== $new_param_block) { $modified_docblock = true; } diff --git a/tests/FileManipulation/ParamTypeManipulationTest.php b/tests/FileManipulation/ParamTypeManipulationTest.php index 9d00e1d6971..78bfb8f18c2 100644 --- a/tests/FileManipulation/ParamTypeManipulationTest.php +++ b/tests/FileManipulation/ParamTypeManipulationTest.php @@ -26,6 +26,25 @@ function foo(string $s): string { 'issues_to_fix' => ['MismatchingDocblockParamType'], 'safe_types' => true, ], + 'fixMismatchingDocblockWithDescriptionParamType70' => [ + 'input' => ' ' '7.0', + 'issues_to_fix' => ['MismatchingDocblockParamType'], + 'safe_types' => true, + ], 'fixNamespacedMismatchingDocblockParamsType70' => [ 'input' => ' ['MissingParamType'], 'safe_types' => true, ], + 'noStringParamTypeWithDocblockAndDescriptionCall' => [ + 'input' => 'fooFoo($ab); + }', + 'output' => 'fooFoo($ab); + }', + 'php_version' => '7.1', + 'issues_to_fix' => ['MissingParamType'], + 'safe_types' => true, + ], 'noStringParamType56' => [ 'input' => '