Skip to content

Commit

Permalink
Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Oct 7, 2024
1 parent 461b22d commit ab4fee0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
54 changes: 35 additions & 19 deletions src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
Expand Down Expand Up @@ -58,17 +61,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$newConstantArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
$valueType = $constantArray->getOffsetValueType($keyType);
if ($keyType->isString()->yes()) {
if (!isset($case)) {
$keyType = TypeCombinator::union(
new ConstantStringType(strtolower((string) $keyType->getValue())),
new ConstantStringType(strtoupper((string) $keyType->getValue())),
);
} elseif ($case === CASE_LOWER) {
$keyType = new ConstantStringType(strtolower((string) $keyType->getValue()));
} else {
$keyType = new ConstantStringType(strtoupper((string) $keyType->getValue()));
}
if ($keyType instanceof ConstantStringType) {

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 64 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.
$keyType = $this->mapConstantString($keyType, $case);

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 65 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.
}

$newConstantArrayBuilder->setOffsetValueType(
Expand All @@ -88,22 +82,30 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
} else {
$keysType = $arrayType->getIterableKeyType();

$keysType = TypeTraverser::map($keysType, static function (Type $type, callable $traverse) use ($case): Type {
$keysType = TypeTraverser::map($keysType, function (Type $type, callable $traverse) use ($case): Type {
if ($type instanceof UnionType) {
return $traverse($type);

Check failure on line 87 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 87 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.
}

if ($type instanceof ConstantStringType) {
return $this->mapConstantString($type, $case);

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.

Check failure on line 91 in src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #2 $case of method PHPStan\Type\Php\ArrayChangeKeyCaseFunctionReturnTypeExtension::mapConstantString() expects int|null, bool|float|int|string|null given.
}

if ($type->isString()->yes()) {
if ($case === CASE_LOWER) {
return TypeCombinator::intersect($type, new AccessoryLowercaseStringType());
} elseif ($type->isLowercaseString()->yes()) {
return TypeCombinator::intersect(
new StringType(),
...array_filter(
TypeUtils::getAccessoryTypes($type),
static fn (Type $accessory): bool => !$accessory instanceof AccessoryLowercaseStringType,
),
);
$types = [new StringType()];
if ($type->isNonFalsyString()->yes()) {
$types[] = new AccessoryNonFalsyStringType();
} elseif ($type->isNonEmptyString()->yes()) {
$types[] = new AccessoryNonEmptyStringType();
}
if ($type->isNumericString()->yes()) {
$types[] = new AccessoryNumericStringType();
}

return TypeCombinator::intersect(...$types);
}
}

Expand All @@ -123,4 +125,18 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $newArrayType;
}

private function mapConstantString(ConstantStringType $type, ?int $case): Type
{
if ($case === CASE_LOWER) {
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())),
);
}
}

}
23 changes: 15 additions & 8 deletions tests/PHPStan/Analyser/nsrt/array-change-key-case.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
class HelloWorld
{
/**
* @param array<string> $arr1
* @param array<string, string> $arr2
* @param array<string|int, string> $arr3
* @param array<int, string> $arr4
* @param array<lowercase-string, string> $arr5
* @param array<string> $arr1
* @param array<string, string> $arr2
* @param array<string|int, string> $arr3
* @param array<int, string> $arr4
* @param array<lowercase-string, string> $arr5
* @param array<lowercase-string&non-falsy-string, string> $arr6
* @param array{foo: 1, bar?: 2} $arr7
* @param list<string> $list
* @param non-empty-array<string> $nonEmpty
* @param array{foo: 1, bar?: 2} $arr7
* @param array<'foo'|'bar', string> $arr8
* @param list<string> $list
* @param non-empty-array<string> $nonEmpty
*/
public function sayHello(
array $arr1,
Expand All @@ -25,6 +26,7 @@ public function sayHello(
array $arr5,
array $arr6,
array $arr7,
array $arr8,
array $list,
array $nonEmpty,
int $case
Expand Down Expand Up @@ -64,6 +66,11 @@ public function sayHello(
assertType('array{FOO: 1, BAR?: 2}', array_change_key_case($arr7, CASE_UPPER));
assertType("non-empty-array<'BAR'|'bar'|'FOO'|'foo', 1|2>", array_change_key_case($arr7, $case));

assertType("array<'bar'|'foo', string>", array_change_key_case($arr8));
assertType("array<'bar'|'foo', string>", array_change_key_case($arr8, CASE_LOWER));
assertType("array<'BAR'|'FOO', string>", array_change_key_case($arr8, CASE_UPPER));
assertType("array<'BAR'|'bar'|'FOO'|'foo', string>", array_change_key_case($arr8, $case));

assertType('list<string>', array_change_key_case($list));
assertType('list<string>', array_change_key_case($list, CASE_LOWER));
assertType('list<string>', array_change_key_case($list, CASE_UPPER));
Expand Down

0 comments on commit ab4fee0

Please sign in to comment.