forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionTypeHelper.php
More file actions
133 lines (113 loc) · 3.13 KB
/
Copy pathUnionTypeHelper.php
File metadata and controls
133 lines (113 loc) · 3.13 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
<?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
{
$subTypeClasses = [];
foreach ($types as $type) {
$subTypeClasses[] = $type->getReferencedClasses();
}
return array_merge(...$subTypeClasses);
}
/**
* @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 strcasecmp($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 strcasecmp($a->getValue(), $b->getValue());
}
if ($a instanceof ConstantArrayType && $b instanceof ConstantArrayType) {
if ($a->isEmpty()) {
if ($b->isEmpty()) {
return 0;
}
return -1;
} elseif ($b->isEmpty()) {
return 1;
}
return strcasecmp($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
}
return strcasecmp($a->describe(VerbosityLevel::typeOnly()), $b->describe(VerbosityLevel::typeOnly()));
});
return $types;
}
}