1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Merge param descriptions when adding or updating types

This commit is contained in:
Andrew Boyton 2023-03-02 14:54:38 +11:00
parent 06266c220b
commit e8bd52eba6
2 changed files with 76 additions and 0 deletions

View File

@ -17,7 +17,9 @@ use Psalm\Internal\Scanner\ParsedDocblock;
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 @@ class FunctionDocblockManipulator
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;
}

View File

@ -26,6 +26,25 @@ class ParamTypeManipulationTest extends FileManipulationTestCase
'issues_to_fix' => ['MismatchingDocblockParamType'],
'safe_types' => true,
],
'fixMismatchingDocblockWithDescriptionParamType70' => [
'input' => '<?php
/**
* @param int $s the string
*/
function foo(string $s): string {
return "hello";
}',
'output' => '<?php
/**
* @param string $s the string
*/
function foo(string $s): string {
return "hello";
}',
'php_version' => '7.0',
'issues_to_fix' => ['MismatchingDocblockParamType'],
'safe_types' => true,
],
'fixNamespacedMismatchingDocblockParamsType70' => [
'input' => '<?php
namespace Foo\Bar {
@ -139,6 +158,39 @@ class ParamTypeManipulationTest extends FileManipulationTestCase
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
'noStringParamTypeWithDocblockAndDescriptionCall' => [
'input' => '<?php
class C {
/**
* @param $ab the string you pass in
*/
public function fooFoo($ab): void {}
}
/**
* @param string $ab
*/
function callsWithString($ab): void {
(new C)->fooFoo($ab);
}',
'output' => '<?php
class C {
/**
* @param string $ab the string you pass in
*/
public function fooFoo($ab): void {}
}
/**
* @param string $ab
*/
function callsWithString($ab): void {
(new C)->fooFoo($ab);
}',
'php_version' => '7.1',
'issues_to_fix' => ['MissingParamType'],
'safe_types' => true,
],
'noStringParamType56' => [
'input' => '<?php
class C {