Skip to content

Commit

Permalink
fix bug with class-string-map
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Mar 12, 2024
1 parent 7e7d965 commit 1a715f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ private static function checkFunctionLikeTypeMatches(
$invalid_string_key = false;
$possibly_matches = false;
foreach ($arg_value_type->getAtomicTypes() as $atomic_type) {
if (!$atomic_type->isIterable($codebase)) {
if (!$atomic_type->isIterable($codebase) && !$atomic_type instanceof TClassStringMap) {
$non_iterable = true;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,6 @@ public static function checkArgumentsMatch(
&& $arg_value_type->hasArray()) {
$named_args_was_used_before_array = $named_args_was_used;

/**
* @var TArray|TKeyedArray
*/
$array_type = $arg_value_type->getArray();

if ($array_type instanceof TCallableArray ||
Expand Down Expand Up @@ -821,7 +818,7 @@ public static function checkArgumentsMatch(

$args_provided_min += $array_type->count ? ($array_type->count - 1) : 0;
} else {
if ($array_type->type_params[1]->isNever()) {
if (isset($array_type->type_params[1]) && $array_type->type_params[1]->isNever()) {
$args_provided_max--;
} else {
$args_provided_max = $function_param_count + 10_00_00_00_00;
Expand All @@ -832,9 +829,16 @@ public static function checkArgumentsMatch(
$has_unpacked_non_keyed_array = true;
}

$key_types = $array_type->type_params[0]->getAtomicTypes();
$key_types = Type::getArrayKey();
if ($array_type instanceof Type\Atomic\TClassStringMap) {
$key_types = Type::getNonFalsyString();
} elseif ($array_type instanceof TCallableKeyedArray) {
$key_types = Type::getInt();
} elseif (isset($array_type->type_params[0])) {
$key_types = $array_type->type_params[0];
}

if ($array_type->type_params[0]->isString()) {
if ($key_types->isString()) {
$named_args_was_used = true;
} elseif ($named_args_was_used) {
IssueBuffer::maybeAdd(
Expand All @@ -845,7 +849,7 @@ public static function checkArgumentsMatch(
),
$statements_analyzer->getSuppressedIssues(),
);
} elseif (!$array_type->type_params[0]->isInt()) {
} elseif (!$key_types->isInt()) {
// e.g. array-key or a mix of literal strings with int or just int|string
IssueBuffer::maybeAdd(
new PossiblyInvalidArgument(
Expand All @@ -859,7 +863,7 @@ public static function checkArgumentsMatch(
$named_args_was_used = true;
}

foreach ($key_types as $key_type) {
foreach ($key_types->getAtomicTypes() as $key_type) {
if ($function_storage && !$function_storage->allow_named_arg_calls) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Template/ClassStringMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function takesVariadic(...$args): void {
function foo(array $arr) : void {
takesVariadic(...$arr);
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
'assignClassStringMapInConstruct' => [
'code' => '<?php
Expand Down

0 comments on commit 1a715f9

Please sign in to comment.