Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 27, 2024
1 parent 436e29f commit cc805de
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,35 @@ public static function checkArgumentMatches(
}

$param_type = $function_param->type;


$param_types = [];
if ($function_param->is_variadic
&& $param_type
&& $param_type->hasArray()
) {
$array_type = $param_type->getArray();

if ($array_type instanceof TKeyedArray && $array_type->is_list) {
$param_type = $array_type->getGenericValueType();
} elseif ($array_type instanceof TArray) {
$param_type = $array_type->type_params[1];
foreach ($param_type->getArrays() as $array_type) {
if ($array_type instanceof TKeyedArray && $array_type->is_list) {
$param_types []= $array_type->getGenericValueType();
} elseif ($array_type instanceof TArray) {
$param_types []= $array_type->type_params[1];
}
}
} elseif ($param_type) {
$param_types = [$param_type];
}

if ($param_type && !$param_type->hasMixed()) {
IssueBuffer::maybeAdd(
new MixedArgument(
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
. ' cannot be mixed, expecting ' . $param_type,
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
foreach ($param_types as $param_type) {
if ($param_type->hasMixed()) {
IssueBuffer::maybeAdd(
new MixedArgument(
'Argument ' . ($argument_offset + 1) . ' of ' . $cased_method_id
. ' cannot be mixed, expecting ' . $param_type,
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
}
}
}

Expand Down Expand Up @@ -472,62 +477,62 @@ private static function checkFunctionLikeTypeMatches(
}

if ($arg_value_type->hasArray()) {
$unpacked_atomic_array = $arg_value_type->getArray();
$arg_key_allowed = true;
foreach ($arg_value_type->getArrays() as $unpacked_atomic_array) {
if ($unpacked_atomic_array instanceof TKeyedArray) {
if (!$allow_named_args && !$unpacked_atomic_array->getGenericKeyType()->isInt()) {
$arg_key_allowed = false;
}

if ($unpacked_atomic_array instanceof TKeyedArray) {
if (!$allow_named_args && !$unpacked_atomic_array->getGenericKeyType()->isInt()) {
$arg_key_allowed = false;
}

if ($function_param->is_variadic) {
$arg_value_type = $unpacked_atomic_array->getGenericValueType();
} elseif ($codebase->analysis_php_version_id >= 8_00_00
if ($function_param->is_variadic) {
$arg_value_type = $unpacked_atomic_array->getGenericValueType();
} elseif ($codebase->analysis_php_version_id >= 8_00_00
&& $allow_named_args
&& isset($unpacked_atomic_array->properties[$function_param->name])
) {
$arg_value_type = $unpacked_atomic_array->properties[$function_param->name];
} elseif ($unpacked_atomic_array->is_list
) {
$arg_value_type = $unpacked_atomic_array->properties[$function_param->name];
} elseif ($unpacked_atomic_array->is_list
&& isset($unpacked_atomic_array->properties[$unpacked_argument_offset])
) {
$arg_value_type = $unpacked_atomic_array->properties[$unpacked_argument_offset];
} elseif ($unpacked_atomic_array->fallback_params) {
$arg_value_type = $unpacked_atomic_array->fallback_params[1];
} elseif ($function_param->is_optional && $function_param->default_type) {
if ($function_param->default_type instanceof Union) {
$arg_value_type = $function_param->default_type;
} else {
$arg_value_type_atomic = ConstantTypeResolver::resolve(
$codebase->classlikes,
$function_param->default_type,
$statements_analyzer,
);
) {
$arg_value_type = $unpacked_atomic_array->properties[$unpacked_argument_offset];
} elseif ($unpacked_atomic_array->fallback_params) {
$arg_value_type = $unpacked_atomic_array->fallback_params[1];
} elseif ($function_param->is_optional && $function_param->default_type) {
if ($function_param->default_type instanceof Union) {
$arg_value_type = $function_param->default_type;
} else {
$arg_value_type_atomic = ConstantTypeResolver::resolve(
$codebase->classlikes,
$function_param->default_type,
$statements_analyzer,
);

$arg_value_type = new Union([$arg_value_type_atomic]);
$arg_value_type = new Union([$arg_value_type_atomic]);
}
} else {
$arg_value_type = Type::getMixed();
}
} else {
} elseif ($unpacked_atomic_array instanceof TClassStringMap) {
$arg_value_type = Type::getMixed();
} else {
if (!$allow_named_args && !$unpacked_atomic_array->type_params[0]->isInt()) {
$arg_key_allowed = false;
}
$arg_value_type = $unpacked_atomic_array->type_params[1];
}
} elseif ($unpacked_atomic_array instanceof TClassStringMap) {
$arg_value_type = Type::getMixed();
} else {
if (!$allow_named_args && !$unpacked_atomic_array->type_params[0]->isInt()) {
$arg_key_allowed = false;
}
$arg_value_type = $unpacked_atomic_array->type_params[1];
}

if (!$arg_key_allowed) {
IssueBuffer::maybeAdd(
new NamedArgumentNotAllowed(
'Method ' . $cased_method_id
if (!$arg_key_allowed) {
IssueBuffer::maybeAdd(
new NamedArgumentNotAllowed(
'Method ' . $cased_method_id
. ' called with named unpacked array ' . $unpacked_atomic_array->getId()
. ' (array with string keys)',
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
new CodeLocation($statements_analyzer->getSource(), $arg->value),
$cased_method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
}
}
} else {
$non_iterable = false;
Expand Down Expand Up @@ -1246,9 +1251,8 @@ private static function verifyExplicitParam(
}
} elseif ($param_type_part instanceof TCallable) {
$can_be_callable_like_array = false;
if ($param_type->hasArray()) {
$param_array_type = $param_type->getArray();


foreach ($param_type->getArrays() as $param_array_type) {
$row_type = null;
if ($param_array_type instanceof TArray) {
$row_type = $param_array_type->type_params[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,56 +722,53 @@ public static function checkArgumentsMatch(

if (($arg_value_type = $statements_analyzer->node_data->getType($arg->value))
&& $arg_value_type->hasArray()) {
/**
* @var TArray|TKeyedArray
*/
$array_type = $arg_value_type->getArray();

if ($array_type instanceof TKeyedArray) {
$array_type = $array_type->getGenericArrayType();
$key_types = $array_type->type_params[0]->getAtomicTypes();

foreach ($key_types as $key_type) {
if (!$key_type instanceof TLiteralString
|| ($function_storage && !$function_storage->allow_named_arg_calls)) {
continue;
}
foreach ($arg_value_type->getArrays() as $array_type) {
if ($array_type instanceof TKeyedArray) {
$array_type = $array_type->getGenericArrayType();
$key_types = $array_type->type_params[0]->getAtomicTypes();

foreach ($key_types as $key_type) {
if (!$key_type instanceof TLiteralString
|| ($function_storage && !$function_storage->allow_named_arg_calls)) {
continue;
}

$param_found = false;

foreach ($function_params as $candidate_param) {
if ($candidate_param->name === $key_type->value || $candidate_param->is_variadic) {
if ($candidate_param->name === $key_type->value) {
if (isset($matched_args[$candidate_param->name])) {
IssueBuffer::maybeAdd(
new InvalidNamedArgument(
'Parameter $' . $key_type->value . ' has already been used in '
. ($cased_method_id ?: $method_id),
new CodeLocation($statements_analyzer, $arg),
(string)$method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
$param_found = false;

foreach ($function_params as $candidate_param) {
if ($candidate_param->name === $key_type->value || $candidate_param->is_variadic) {
if ($candidate_param->name === $key_type->value) {
if (isset($matched_args[$candidate_param->name])) {
IssueBuffer::maybeAdd(
new InvalidNamedArgument(
'Parameter $' . $key_type->value . ' has already been used in '
. ($cased_method_id ?: $method_id),
new CodeLocation($statements_analyzer, $arg),
(string)$method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
}

$matched_args[$candidate_param->name] = true;
}

$matched_args[$candidate_param->name] = true;
$param_found = true;
break;
}

$param_found = true;
break;
}
}

if (!$param_found) {
IssueBuffer::maybeAdd(
new InvalidNamedArgument(
'Parameter $' . $key_type->value . ' does not exist on function '
if (!$param_found) {
IssueBuffer::maybeAdd(
new InvalidNamedArgument(
'Parameter $' . $key_type->value . ' does not exist on function '
. ($cased_method_id ?: $method_id),
new CodeLocation($statements_analyzer, $arg),
(string)$method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
new CodeLocation($statements_analyzer, $arg),
(string)$method_id,
),
$statements_analyzer->getSuppressedIssues(),
);
}
}
}
}
Expand Down
Loading

0 comments on commit cc805de

Please sign in to comment.