Skip to content

Commit b3c25b8

Browse files
Fix string types sorting
1 parent c6a852a commit b3c25b8

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Type/UnionTypeHelper.php

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public static function sortTypes(array $types): array
116116
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
117117
}
118118

119+
if ($a->isString()->yes() && $b->isString()->yes()) {
120+
return self::compareStrings($a->describe(VerbosityLevel::value()), $b->describe(VerbosityLevel::value()));
121+
}
122+
119123
return self::compareStrings($a->describe(VerbosityLevel::typeOnly()), $b->describe(VerbosityLevel::typeOnly()));
120124
});
121125
return $types;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace StringUnion;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param literal-string $s1
11+
* @param numeric-string $s2
12+
*/
13+
public function sayHello(string $s1, string $s2): void
14+
{
15+
$a = random_int(0, 1) ? $s1 : $s2;
16+
assertType('literal-string|numeric-string', $a);
17+
$b = random_int(0, 1) ? $s2 : $s1;
18+
assertType('literal-string|numeric-string', $b);
19+
}
20+
}

tests/PHPStan/Type/TypeCombinatorTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,14 @@ public function dataUnion(): iterable
19601960
UnionType::class,
19611961
'string|false',
19621962
],
1963+
[
1964+
[
1965+
new IntersectionType([new StringType(), new AccessoryNumericStringType()]),
1966+
new IntersectionType([new StringType(), new AccessoryLiteralStringType()]),
1967+
],
1968+
UnionType::class,
1969+
'literal-string|numeric-string',
1970+
],
19631971
[
19641972
[
19651973
TemplateTypeFactory::create(

0 commit comments

Comments
 (0)