Skip to content

Commit 86435c0

Browse files
SanderMullerclaudestaabm
authored
Sort enum-case unions by class and case name instead of describe() (#5929)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent 040c808 commit 86435c0

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,12 @@ parameters:
17731773
count: 2
17741774
path: src/Type/UnionTypeHelper.php
17751775

1776+
-
1777+
rawMessage: 'Doing instanceof PHPStan\Type\Enum\EnumCaseObjectType is error-prone and deprecated. Use Type::getEnumCases() instead.'
1778+
identifier: phpstanApi.instanceofType
1779+
count: 2
1780+
path: src/Type/UnionTypeHelper.php
1781+
17761782
-
17771783
rawMessage: 'Doing instanceof PHPStan\Type\IntegerType is error-prone and deprecated. Use Type::isInteger() instead.'
17781784
identifier: phpstanApi.instanceofType

src/Type/UnionTypeHelper.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Type\Constant\ConstantFloatType;
88
use PHPStan\Type\Constant\ConstantIntegerType;
99
use PHPStan\Type\Constant\ConstantStringType;
10+
use PHPStan\Type\Enum\EnumCaseObjectType;
1011
use function count;
1112
use function strcasecmp;
1213
use function usort;
@@ -96,6 +97,20 @@ public static function sortTypes(array $types): array
9697
return self::compareStrings($a->getValue(), $b->getValue());
9798
}
9899

100+
if ($a instanceof EnumCaseObjectType && $b instanceof EnumCaseObjectType) {
101+
return self::compareStrings(
102+
$a->getClassName() . '::' . $a->getEnumCaseName(),
103+
$b->getClassName() . '::' . $b->getEnumCaseName(),
104+
);
105+
}
106+
107+
if (
108+
($a instanceof CallableType || $a instanceof ClosureType)
109+
&& ($b instanceof CallableType || $b instanceof ClosureType)
110+
) {
111+
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
112+
}
113+
99114
if ($a->isConstantArray()->yes() && $b->isConstantArray()->yes()) {
100115
if ($a->isIterableAtLeastOnce()->no()) {
101116
if ($b->isIterableAtLeastOnce()->no()) {
@@ -110,13 +125,6 @@ public static function sortTypes(array $types): array
110125
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
111126
}
112127

113-
if (
114-
($a instanceof CallableType || $a instanceof ClosureType)
115-
&& ($b instanceof CallableType || $b instanceof ClosureType)
116-
) {
117-
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
118-
}
119-
120128
if ($a->isString()->yes() && $b->isString()->yes()) {
121129
return self::compareStrings($a->describe(VerbosityLevel::precise()), $b->describe(VerbosityLevel::precise()));
122130
}

0 commit comments

Comments
 (0)