2121use PHPStan \Type \TypeTraverser ;
2222use PHPStan \Type \TypeUtils ;
2323use PHPStan \Type \UnionType ;
24- use function array_filter ;
24+ use function array_map ;
2525use function count ;
2626use function strtolower ;
2727use function strtoupper ;
2828use const CASE_LOWER ;
29+ use const CASE_UPPER ;
2930
3031final class ArrayChangeKeyCaseFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
3132{
@@ -48,7 +49,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4849 $ caseType = $ scope ->getType ($ functionCall ->getArgs ()[1 ]->value );
4950 $ scalarValues = $ caseType ->getConstantScalarValues ();
5051 if (count ($ scalarValues ) === 1 ) {
51- $ case = $ scalarValues [0 ];
52+ $ case = ( int ) $ scalarValues [0 ];
5253 } else {
5354 $ case = null ;
5455 }
@@ -61,8 +62,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6162 $ newConstantArrayBuilder = ConstantArrayTypeBuilder::createEmpty ();
6263 foreach ($ constantArray ->getKeyTypes () as $ i => $ keyType ) {
6364 $ 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+ );
6674 }
6775
6876 $ newConstantArrayBuilder ->setOffsetValueType (
@@ -87,8 +95,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
8795 return $ traverse ($ type );
8896 }
8997
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+ );
92106 }
93107
94108 if ($ type ->isString ()->yes ()) {
@@ -131,12 +145,12 @@ private function mapConstantString(ConstantStringType $type, ?int $case): Type
131145 return new ConstantStringType (strtolower ($ type ->getValue ()));
132146 } elseif ($ case === CASE_UPPER ) {
133147 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- );
139148 }
149+
150+ return TypeCombinator::union (
151+ new ConstantStringType (strtolower ($ type ->getValue ())),
152+ new ConstantStringType (strtoupper ($ type ->getValue ())),
153+ );
140154 }
141155
142156}
0 commit comments