Skip to content

Commit

Permalink
[DeadCode] Clean up TypeHasher on Union Type (#5792)
Browse files Browse the repository at this point in the history
* [DeadCode] Clean up TypeHasher on Union Type

* fix phpstan
  • Loading branch information
samsonasik authored Apr 3, 2024
1 parent ef333de commit 52aa64f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector
class MultipleTypes
{
/**
* @var int|string|bool
* @var bool|int|string
*/
public $value;
public function set()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Fixture;

class UnionUnsorted
{
/**
* @param bool|int|string $param
*/
function foo(string|int|bool $param)
{

}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Fixture;

class UnionUnsorted
{
function foo(string|int|bool $param)
{

}
}

?>
15 changes: 13 additions & 2 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Rector\DeadCode\PhpDoc;

use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
Expand Down Expand Up @@ -44,6 +46,10 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
return false;
}

if ($paramTagValueNode->type instanceof UnionTypeNode && $param->type instanceof FullyQualified) {
return false;
}

if ($param->type instanceof Name && $this->nodeNameResolver->isName($param->type, 'object')) {
return $paramTagValueNode->type instanceof IdentifierTypeNode && (string) $paramTagValueNode->type === 'object';
}
Expand All @@ -64,10 +70,15 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
return true;
}

if ($this->mixedArrayTypeNodeAnalyzer->hasMixedArrayType($paramTagValueNode->type)) {
return $this->isAllowedBracketAwareUnion($paramTagValueNode->type);
}

private function isAllowedBracketAwareUnion(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode): bool
{
if ($this->mixedArrayTypeNodeAnalyzer->hasMixedArrayType($bracketsAwareUnionTypeNode)) {
return false;
}

return ! $this->genericTypeNodeAnalyzer->hasGenericType($paramTagValueNode->type);
return ! $this->genericTypeNodeAnalyzer->hasGenericType($bracketsAwareUnionTypeNode);
}
}
1 change: 0 additions & 1 deletion rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
Expand Down
30 changes: 0 additions & 30 deletions src/NodeTypeResolver/PHPStan/TypeHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\NodeTypeResolver\PHPStan;

use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ConstantType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IterableType;
Expand All @@ -14,7 +13,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
Expand Down Expand Up @@ -49,10 +47,6 @@ public function createTypeHash(Type $type): string
return $type::class;
}

if ($type instanceof UnionType) {
return $this->createUnionTypeHash($type);
}

$type = $this->normalizeObjectType($type);

// normalize iterable
Expand Down Expand Up @@ -84,30 +78,6 @@ private function resolveUniqueTypeWithClassNameHash(TypeWithClassName $typeWithC
return $typeWithClassName->getClassName();
}

private function createUnionTypeHash(UnionType $unionType): string
{
$booleanType = new BooleanType();
if ($booleanType->isSuperTypeOf($unionType)->yes()) {
return $booleanType->describe(VerbosityLevel::precise());
}

$normalizedUnionType = clone $unionType;

// change alias to non-alias
TypeTraverser::map(
$normalizedUnionType,
static function (Type $type, callable $callable): Type {
if (! $type instanceof AliasedObjectType && ! $type instanceof ShortenedObjectType) {
return $callable($type);
}

return new FullyQualifiedObjectType($type->getFullyQualifiedName());
}
);

return $normalizedUnionType->describe(VerbosityLevel::precise());
}

private function normalizeObjectType(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $currentType, callable $traverseCallback): Type {
Expand Down

0 comments on commit 52aa64f

Please sign in to comment.