Skip to content

Commit

Permalink
Skip already correct param type (#1688)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
sabbelasichon and actions-user committed Jan 17, 2022
1 parent 94e7ac8 commit f09e54b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php80\Rector\FunctionLike\UnionTypesRector\Fixture;

use Ds\Set;
use Reflection;
use stdClass;

interface A
{
/**
* @param Set<stdClass>|Set<Reflection> $list
*/
public function execute(Set $list) : void;
}

?>
38 changes: 33 additions & 5 deletions rules/Php80/Rector/FunctionLike/UnionTypesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
}

0 comments on commit f09e54b

Please sign in to comment.