Skip to content

Commit

Permalink
Merge pull request #9441 from aboyton/divide-zero
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Mar 2, 2023
2 parents 06266c2 + e8bd52e commit e3078cd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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) {
// 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

0 comments on commit e3078cd

Please sign in to comment.