Skip to content
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

Merge param descriptions when adding or updating types #9441

Merged
merged 1 commit into from Mar 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
weirdan marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}
Expand Down
52 changes: 52 additions & 0 deletions tests/FileManipulation/ParamTypeManipulationTest.php
Expand Up @@ -26,6 +26,25 @@ function foo(string $s): string {
'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 {
Expand Down Expand Up @@ -139,6 +158,39 @@ function callsWithString($a): void {
'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 {
Expand Down