Skip to content

Commit

Permalink
Also add better message for MixedArgumentTypeCoercion
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 28, 2021
1 parent 54ac13b commit 93743d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Expand Up @@ -934,12 +934,30 @@ public static function verifyType(

if ($union_comparison_results->type_coerced && !$input_type->hasMixed()) {
if ($union_comparison_results->type_coerced_from_mixed) {
$origin_locations = [];

if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
foreach ($input_type->parent_nodes as $parent_node) {
$origin_locations = array_merge(
$origin_locations,
$statements_analyzer->data_flow_graph->getOriginLocations($parent_node)
);
}
}

$origin_location = count($origin_locations) === 1 ? reset($origin_locations) : null;

if ($origin_location && $origin_location->getHash() === $arg_location->getHash()) {
$origin_location = null;
}

if (IssueBuffer::accepts(
new MixedArgumentTypeCoercion(
'Argument ' . ($argument_offset + 1) . $method_identifier . ' expects ' . $param_type->getId() .
', parent type ' . $input_type->getId() . ' provided',
$arg_location,
$cased_method_id
$cased_method_id,
$origin_location
),
$statements_analyzer->getSuppressedIssues()
)) {
Expand Down
Expand Up @@ -41,7 +41,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$value_type = null;
$definitely_has_items = false;

while ($atomic_type = array_shift($atomic_types)) {
while ($atomic_type = \array_shift($atomic_types)) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$atomic_types = \array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
continue;
Expand Down
16 changes: 16 additions & 0 deletions tests/UnusedVariableTest.php
Expand Up @@ -3298,6 +3298,22 @@ function foo($arr) : void {
}',
'error_message' => 'MixedAssignment - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:42 - Unable to determine the type that $key is being assigned to. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:3:34'
],
'warnAboutMixedArgumentTypeCoercionSource' => [
'<?php
/** @param array<string> $arr */
function takesArrayOfString(array $arr) : void {
foreach ($arr as $a) {
echo $a;
}
}
/** @param mixed $a */
function takesArray($a) : void {
$arr = [$a];
takesArrayOfString($arr);
}',
'error_message' => 'MixedArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:44 - Argument 1 of takesArrayOfString expects array<array-key, string>, parent type array{mixed} provided. Consider improving the type at src' . DIRECTORY_SEPARATOR . 'somefile.php:10:41'
],
];
}
}

0 comments on commit 93743d1

Please sign in to comment.