From f09e54b79cdc481f2033482fd206f6308642b2e0 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Mon, 17 Jan 2022 10:01:28 +0100 Subject: [PATCH] Skip already correct param type (#1688) Co-authored-by: GitHub Action --- .../Fixture/skip_already_correct_type.php.inc | 17 +++++++++ .../Rector/FunctionLike/UnionTypesRector.php | 38 ++++++++++++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_already_correct_type.php.inc diff --git a/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_already_correct_type.php.inc b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_already_correct_type.php.inc new file mode 100644 index 00000000000..db05051d406 --- /dev/null +++ b/rules-tests/Php80/Rector/FunctionLike/UnionTypesRector/Fixture/skip_already_correct_type.php.inc @@ -0,0 +1,17 @@ +|Set $list + */ + public function execute(Set $list) : void; +} + +?> diff --git a/rules/Php80/Rector/FunctionLike/UnionTypesRector.php b/rules/Php80/Rector/FunctionLike/UnionTypesRector.php index 71ffd43e4cc..c8168eda6d0 100644 --- a/rules/Php80/Rector/FunctionLike/UnionTypesRector.php +++ b/rules/Php80/Rector/FunctionLike/UnionTypesRector.php @@ -5,9 +5,12 @@ namespace Rector\Php80\Rector\FunctionLike; use PhpParser\Node; +use PhpParser\Node\ComplexType; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; +use PhpParser\Node\Identifier; use PhpParser\Node\Name; +use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; @@ -155,11 +158,7 @@ private function refactorParamTypes( TypeKind::PARAM() ); - if (! $phpParserUnionType instanceof PhpParserUnionType) { - continue; - } - - if ($param->type instanceof PhpParserUnionType) { + if ($this->shouldSkipParamTypeRefactor($param->type, $phpParserUnionType)) { continue; } @@ -242,4 +241,33 @@ private function filterOutDuplicatedArrayTypes(UnionType $unionType): UnionType return $this->typeFactory->createMixedPassedOrUnionType($singleArrayTypes); } + + private function shouldSkipParamTypeRefactor( + Name|Identifier|ComplexType|null $type, + Name|ComplexType|Node|null $phpParserUnionType + ): bool { + if (! $phpParserUnionType instanceof PhpParserUnionType) { + return true; + } + + if ($type instanceof PhpParserUnionType) { + return true; + } + + if (count($phpParserUnionType->types) > 1) { + return false; + } + + $firstType = $phpParserUnionType->types[0]; + + if (! $firstType instanceof FullyQualified) { + return false; + } + + if (! $type instanceof FullyQualified) { + return false; + } + + return $type->toString() === $firstType->toString(); + } }