/
UnionTypeHelper.php
189 lines (160 loc) · 4.23 KB
/
UnionTypeHelper.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use function array_merge;
use function count;
use function strcasecmp;
use function usort;
use const PHP_INT_MIN;
class UnionTypeHelper
{
/**
* @param Type[] $types
* @return string[]
*/
public static function getReferencedClasses(array $types): array
{
$referencedClasses = [];
foreach ($types as $type) {
$referencedClasses[] = $type->getReferencedClasses();
}
return array_merge(...$referencedClasses);
}
/**
* @param Type[] $types
* @return list<ArrayType>
*/
public static function getArrays(array $types): array
{
$arrays = [];
foreach ($types as $type) {
$arrays[] = $type->getArrays();
}
return array_merge(...$arrays);
}
/**
* @param Type[] $types
* @return list<ConstantArrayType>
*/
public static function getConstantArrays(array $types): array
{
$constantArrays = [];
foreach ($types as $type) {
$constantArrays[] = $type->getConstantArrays();
}
return array_merge(...$constantArrays);
}
/**
* @param Type[] $types
* @return list<ConstantStringType>
*/
public static function getConstantStrings(array $types): array
{
$strings = [];
foreach ($types as $type) {
$strings[] = $type->getConstantStrings();
}
if ($strings === []) {
return [];
}
return array_merge(...$strings);
}
/**
* @param Type[] $types
* @return Type[]
*/
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()));
}
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;
}
}