Skip to content

Commit

Permalink
Rework
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 10, 2024
1 parent e1df8e3 commit cdbbbb5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
25 changes: 12 additions & 13 deletions src/Type/Php/ArrayChangeKeyCaseFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,20 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if ($type->isString()->yes()) {
$types = [new StringType()];
if ($type->isNonFalsyString()->yes()) {
$types[] = new AccessoryNonFalsyStringType();
} elseif ($type->isNonEmptyString()->yes()) {
$types[] = new AccessoryNonEmptyStringType();
}
if ($type->isNumericString()->yes()) {
$types[] = new AccessoryNumericStringType();
}
if ($case === CASE_LOWER) {
return TypeCombinator::intersect($type, new AccessoryLowercaseStringType());
} elseif ($type->isLowercaseString()->yes()) {
$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);
$types[] = new AccessoryLowercaseStringType();
}

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

return $type;
Expand Down
34 changes: 24 additions & 10 deletions tests/PHPStan/Analyser/nsrt/array-change-key-case.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class HelloWorld
* @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 array<'foo'|'bar', string> $arr8
* @param array<non-empty-string, string> $arr7
* @param array<literal-string, string> $arr8
* @param array{foo: 1, bar?: 2} $arr9
* @param array<'foo'|'bar', string> $arr10
* @param list<string> $list
* @param non-empty-array<string> $nonEmpty
*/
Expand All @@ -27,6 +29,8 @@ public function sayHello(
array $arr6,
array $arr7,
array $arr8,
array $arr9,
array $arr10,
array $list,
array $nonEmpty,
int $case
Expand Down Expand Up @@ -61,15 +65,25 @@ public function sayHello(
assertType('array<non-falsy-string, string>', array_change_key_case($arr6, CASE_UPPER));
assertType('array<non-falsy-string, string>', array_change_key_case($arr6, $case));

assertType('array{foo: 1, bar?: 2}', array_change_key_case($arr7));
assertType('array{foo: 1, bar?: 2}', array_change_key_case($arr7, CASE_LOWER));
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<lowercase-string&non-empty-string, string>', array_change_key_case($arr7));
assertType('array<lowercase-string&non-empty-string, string>', array_change_key_case($arr7, CASE_LOWER));
assertType('array<non-empty-string, string>', array_change_key_case($arr7, CASE_UPPER));
assertType('array<non-empty-string, string>', 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('array<lowercase-string, string>', array_change_key_case($arr8));
assertType('array<lowercase-string, string>', array_change_key_case($arr8, CASE_LOWER));
assertType('array<string, string>', array_change_key_case($arr8, CASE_UPPER));
assertType('array<string, string>', array_change_key_case($arr8, $case));

assertType('array{foo: 1, bar?: 2}', array_change_key_case($arr9));
assertType('array{foo: 1, bar?: 2}', array_change_key_case($arr9, CASE_LOWER));
assertType('array{FOO: 1, BAR?: 2}', array_change_key_case($arr9, CASE_UPPER));
assertType("non-empty-array<'BAR'|'bar'|'FOO'|'foo', 1|2>", array_change_key_case($arr9, $case));

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

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

0 comments on commit cdbbbb5

Please sign in to comment.