21
21
use PHPStan \Type \TypeTraverser ;
22
22
use PHPStan \Type \TypeUtils ;
23
23
use PHPStan \Type \UnionType ;
24
- use function array_filter ;
24
+ use function array_map ;
25
25
use function count ;
26
26
use function strtolower ;
27
27
use function strtoupper ;
28
28
use const CASE_LOWER ;
29
+ use const CASE_UPPER ;
29
30
30
31
final class ArrayChangeKeyCaseFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
31
32
{
@@ -48,7 +49,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
48
49
$ caseType = $ scope ->getType ($ functionCall ->getArgs ()[1 ]->value );
49
50
$ scalarValues = $ caseType ->getConstantScalarValues ();
50
51
if (count ($ scalarValues ) === 1 ) {
51
- $ case = $ scalarValues [0 ];
52
+ $ case = ( int ) $ scalarValues [0 ];
52
53
} else {
53
54
$ case = null ;
54
55
}
@@ -61,8 +62,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
61
62
$ newConstantArrayBuilder = ConstantArrayTypeBuilder::createEmpty ();
62
63
foreach ($ constantArray ->getKeyTypes () as $ i => $ keyType ) {
63
64
$ valueType = $ constantArray ->getOffsetValueType ($ keyType );
64
- if ($ keyType instanceof ConstantStringType) {
65
- $ keyType = $ this ->mapConstantString ($ keyType , $ case );
65
+
66
+ $ constantStrings = $ keyType ->getConstantStrings ();
67
+ if (count ($ constantStrings ) > 0 ) {
68
+ $ keyType = TypeCombinator::union (
69
+ ...array_map (
70
+ fn (ConstantStringType $ type ): Type => $ this ->mapConstantString ($ type , $ case ),
71
+ $ constantStrings ,
72
+ ),
73
+ );
66
74
}
67
75
68
76
$ newConstantArrayBuilder ->setOffsetValueType (
@@ -87,8 +95,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
87
95
return $ traverse ($ type );
88
96
}
89
97
90
- if ($ type instanceof ConstantStringType) {
91
- return $ this ->mapConstantString ($ type , $ case );
98
+ $ constantStrings = $ type ->getConstantStrings ();
99
+ if (count ($ constantStrings ) > 0 ) {
100
+ return TypeCombinator::union (
101
+ ...array_map (
102
+ fn (ConstantStringType $ type ): Type => $ this ->mapConstantString ($ type , $ case ),
103
+ $ constantStrings ,
104
+ ),
105
+ );
92
106
}
93
107
94
108
if ($ type ->isString ()->yes ()) {
@@ -131,12 +145,12 @@ private function mapConstantString(ConstantStringType $type, ?int $case): Type
131
145
return new ConstantStringType (strtolower ($ type ->getValue ()));
132
146
} elseif ($ case === CASE_UPPER ) {
133
147
return new ConstantStringType (strtoupper ($ type ->getValue ()));
134
- } else {
135
- return TypeCombinator::union (
136
- new ConstantStringType (strtolower ($ type ->getValue ())),
137
- new ConstantStringType (strtoupper ($ type ->getValue ())),
138
- );
139
148
}
149
+
150
+ return TypeCombinator::union (
151
+ new ConstantStringType (strtolower ($ type ->getValue ())),
152
+ new ConstantStringType (strtoupper ($ type ->getValue ())),
153
+ );
140
154
}
141
155
142
156
}
0 commit comments