Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Oct 7, 2024
1 parent 40c0b2c commit 61bdd79
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use function array_filter;
use function array_map;
use function count;
use function strtolower;
use function strtoupper;
use const CASE_LOWER;
use const CASE_UPPER;

final class ArrayChangeKeyCaseFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand All @@ -48,7 +49,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$caseType = $scope->getType($functionCall->getArgs()[1]->value);
$scalarValues = $caseType->getConstantScalarValues();
if (count($scalarValues) === 1) {
$case = $scalarValues[0];
$case = (int) $scalarValues[0];
} else {
$case = null;
}
Expand All @@ -61,8 +62,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$newConstantArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
$valueType = $constantArray->getOffsetValueType($keyType);
if ($keyType instanceof ConstantStringType) {
$keyType = $this->mapConstantString($keyType, $case);

$constantStrings = $keyType->getConstantStrings();
if (count($constantStrings) > 0) {
$keyType = TypeCombinator::union(
...array_map(
fn (ConstantStringType $type): Type => $this->mapConstantString($type, $case),
$constantStrings,
),
);
}

$newConstantArrayBuilder->setOffsetValueType(
Expand All @@ -87,8 +95,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $traverse($type);
}

if ($type instanceof ConstantStringType) {
return $this->mapConstantString($type, $case);
$constantStrings = $type->getConstantStrings();
if (count($constantStrings) > 0) {
return TypeCombinator::union(
...array_map(
fn (ConstantStringType $type): Type => $this->mapConstantString($type, $case),
$constantStrings,
),
);
}

if ($type->isString()->yes()) {
Expand Down Expand Up @@ -131,12 +145,12 @@ private function mapConstantString(ConstantStringType $type, ?int $case): Type
return new ConstantStringType(strtolower($type->getValue()));
} elseif ($case === CASE_UPPER) {
return new ConstantStringType(strtoupper($type->getValue()));
} else {
return TypeCombinator::union(
new ConstantStringType(strtolower($type->getValue())),
new ConstantStringType(strtoupper($type->getValue())),
);
}

return TypeCombinator::union(
new ConstantStringType(strtolower($type->getValue())),
new ConstantStringType(strtoupper($type->getValue())),
);
}

}

0 comments on commit 61bdd79

Please sign in to comment.