-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathTypeHasher.php
More file actions
85 lines (68 loc) · 2.93 KB
/
Copy pathTypeHasher.php
File metadata and controls
85 lines (68 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
namespace Rector\NodeTypeResolver\PHPStan;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
final class TypeHasher
{
public function areTypesEqual(Type $firstType, Type $secondType): bool
{
return $this->createTypeHash($firstType) === $this->createTypeHash($secondType);
}
public function createTypeHash(Type $type): string
{
if ($type instanceof MixedType) {
return $type->describe(VerbosityLevel::precise()) . $type->isExplicitMixed();
}
if ($type instanceof ArrayType) {
return $this->createTypeHash($type->getIterableValueType()) . $this->createTypeHash(
$type->getIterableKeyType()
) . $type->getItemType()->describe(VerbosityLevel::precise()) . '[]';
}
if ($type instanceof GenericObjectType) {
return $type->describe(VerbosityLevel::precise());
}
if ($type instanceof TypeWithClassName) {
return $this->resolveUniqueTypeWithClassNameHash($type);
}
if ($type->isConstantValue()->yes()) {
return $type::class;
}
$type = $this->normalizeObjectType($type);
return $type->describe(VerbosityLevel::value());
}
private function resolveUniqueTypeWithClassNameHash(TypeWithClassName $typeWithClassName): string
{
if ($typeWithClassName instanceof ShortenedObjectType) {
return $typeWithClassName->getFullyQualifiedName();
}
if ($typeWithClassName instanceof AliasedObjectType) {
return $typeWithClassName->getFullyQualifiedName();
}
return $typeWithClassName->getClassName();
}
private function normalizeObjectType(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $currentType, callable $traverseCallback): Type {
if ($currentType instanceof ShortenedObjectType) {
return new FullyQualifiedObjectType($currentType->getFullyQualifiedName());
}
if ($currentType instanceof AliasedObjectType) {
return new FullyQualifiedObjectType($currentType->getFullyQualifiedName());
}
if ($currentType instanceof ObjectType && ! $currentType instanceof GenericObjectType && $currentType->getClassName() !== 'Iterator' && $currentType->getClassName() !== 'iterable') {
return new FullyQualifiedObjectType($currentType->getClassName());
}
return $traverseCallback($currentType);
});
}
}