Repository navigation
Expand file tree
/
Copy pathUnionTypeHelper.php
More file actions
139 lines (117 loc) · 3.5 KB
/
Copy pathUnionTypeHelper.php
File metadata and controls
139 lines (117 loc) · 3.5 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use function count;
use function strcasecmp;
use function usort;
use const PHP_INT_MIN;
final class UnionTypeHelper
{
/**
* @template T of Type
* @param list<T> $types
* @return list<T>
*/
public static function sortTypes(array $types): array
{
if (count($types) > 1024) {
return $types;
}
usort($types, static function (Type $a, Type $b): int {
if ($a instanceof NullType) {
return 1;
} elseif ($b instanceof NullType) {
return -1;
}
if ($a instanceof AccessoryType) {
if ($b instanceof AccessoryType) {
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
}
return 1;
}
if ($b instanceof AccessoryType) {
return -1;
}
$aIsBool = $a instanceof ConstantBooleanType;
$bIsBool = $b instanceof ConstantBooleanType;
if ($aIsBool && !$bIsBool) {
return 1;
} elseif ($bIsBool && !$aIsBool) {
return -1;
}
if ($a instanceof ConstantScalarType && !$b instanceof ConstantScalarType) {
return -1;
} elseif (!$a instanceof ConstantScalarType && $b instanceof ConstantScalarType) {
return 1;
}
if (
(
$a instanceof ConstantIntegerType
|| $a instanceof ConstantFloatType
)
&& (
$b instanceof ConstantIntegerType
|| $b instanceof ConstantFloatType
)
) {
$cmp = $a->getValue() <=> $b->getValue();
if ($cmp !== 0) {
return $cmp;
}
if ($a instanceof ConstantIntegerType && $b instanceof ConstantFloatType) {
return -1;
}
if ($b instanceof ConstantIntegerType && $a instanceof ConstantFloatType) {
return 1;
}
return 0;
}
if ($a instanceof IntegerRangeType && $b instanceof IntegerRangeType) {
return ($a->getMin() ?? PHP_INT_MIN) <=> ($b->getMin() ?? PHP_INT_MIN);
}
if ($a instanceof IntegerRangeType && $b instanceof IntegerType) {
return 1;
}
if ($b instanceof IntegerRangeType && $a instanceof IntegerType) {
return -1;
}
if ($a instanceof ConstantStringType && $b instanceof ConstantStringType) {
return self::compareStrings($a->getValue(), $b->getValue());
}
if ($a->isConstantArray()->yes() && $b->isConstantArray()->yes()) {
if ($a->isIterableAtLeastOnce()->no()) {
if ($b->isIterableAtLeastOnce()->no()) {
return 0;
}
return -1;
} elseif ($b->isIterableAtLeastOnce()->no()) {
return 1;
}
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
}
if (
($a instanceof CallableType || $a instanceof ClosureType)
&& ($b instanceof CallableType || $b instanceof ClosureType)
) {
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
}
if ($a->isString()->yes() && $b->isString()->yes()) {
return self::compareStrings($a->describe(VerbosityLevel::precise()), $b->describe(VerbosityLevel::precise()));
}
return self::compareStrings($a->describe(VerbosityLevel::typeOnly()), $b->describe(VerbosityLevel::typeOnly()));
});
return $types;
}
private static function compareStrings(string $a, string $b): int
{
$cmp = strcasecmp($a, $b);
if ($cmp !== 0) {
return $cmp;
}
return $a <=> $b;
}
}