diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index 80456403bc6..060ed42d1a7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -1344,6 +1344,16 @@ private static function coerceValueAfterGatekeeperArgument( return; } + $var_id = ExpressionIdentifier::getVarId( + $input_expr, + $statements_analyzer->getFQCLN(), + $statements_analyzer, + ); + + if (!$var_id) { + return; + } + if (!$input_type_changed && $param_type->from_docblock && !$input_type->hasMixed()) { $types = $input_type->getAtomicTypes(); foreach ($param_type->getAtomicTypes() as $param_atomic_type) { @@ -1382,74 +1392,67 @@ private static function coerceValueAfterGatekeeperArgument( $input_type = new Union($types); } - $var_id = ExpressionIdentifier::getVarId( - $input_expr, - $statements_analyzer->getFQCLN(), - $statements_analyzer, - ); - if ($var_id) { - $was_cloned = false; - - if ($input_type->isNullable() && !$param_type->isNullable()) { - $input_type = $input_type->getBuilder(); - $was_cloned = true; - $input_type->removeType('null'); - $input_type = $input_type->freeze(); - } + $was_cloned = false; - if ($input_type->getId() === $param_type->getId()) { - if ($input_type->from_docblock) { - $input_type = $input_type->setFromDocblock(false); - } - } elseif ($input_type->hasMixed() && $signature_param_type) { - $was_cloned = true; - $parent_nodes = $input_type->parent_nodes; - $by_ref = $input_type->by_ref; - $input_type = $signature_param_type->setProperties([ - 'ignore_nullable_issues' => $signature_param_type->isNullable(), - 'parent_nodes' => $parent_nodes, - 'by_ref' => $by_ref, - ]); - } + if ($input_type->isNullable() && !$param_type->isNullable()) { + $input_type = $input_type->getBuilder(); + $was_cloned = true; + $input_type->removeType('null'); + $input_type = $input_type->freeze(); + } - if ($context->inside_conditional && !isset($context->assigned_var_ids[$var_id])) { - $context->assigned_var_ids[$var_id] = 0; + if ($input_type->getId() === $param_type->getId()) { + if ($input_type->from_docblock) { + $input_type = $input_type->setFromDocblock(false); } + } elseif ($input_type->hasMixed() && $signature_param_type) { + $was_cloned = true; + $parent_nodes = $input_type->parent_nodes; + $by_ref = $input_type->by_ref; + $input_type = $signature_param_type->setProperties([ + 'ignore_nullable_issues' => $signature_param_type->isNullable(), + 'parent_nodes' => $parent_nodes, + 'by_ref' => $by_ref, + ]); + } - if ($was_cloned) { - $context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer); - } + if ($context->inside_conditional && !isset($context->assigned_var_ids[$var_id])) { + $context->assigned_var_ids[$var_id] = 0; + } - if ($unpack) { - if ($unpacked_atomic_array instanceof TArray) { - $unpacked_atomic_array = $unpacked_atomic_array->setTypeParams([ - $unpacked_atomic_array->type_params[0], - $input_type, - ]); + if ($was_cloned) { + $context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer); + } - $context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]); - } elseif ($unpacked_atomic_array instanceof TKeyedArray - && $unpacked_atomic_array->is_list - ) { - if ($unpacked_atomic_array->isNonEmpty()) { - $unpacked_atomic_array = Type::getNonEmptyListAtomic($input_type); - } else { - $unpacked_atomic_array = Type::getListAtomic($input_type); - } + if ($unpack) { + if ($unpacked_atomic_array instanceof TArray) { + $unpacked_atomic_array = $unpacked_atomic_array->setTypeParams([ + $unpacked_atomic_array->type_params[0], + $input_type, + ]); - $context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]); + $context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]); + } elseif ($unpacked_atomic_array instanceof TKeyedArray + && $unpacked_atomic_array->is_list + ) { + if ($unpacked_atomic_array->isNonEmpty()) { + $unpacked_atomic_array = Type::getNonEmptyListAtomic($input_type); } else { - $context->vars_in_scope[$var_id] = new Union([ - new TArray([ - Type::getInt(), - $input_type, - ]), - ]); + $unpacked_atomic_array = Type::getListAtomic($input_type); } + + $context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]); } else { - $context->vars_in_scope[$var_id] = $input_type; + $context->vars_in_scope[$var_id] = new Union([ + new TArray([ + Type::getInt(), + $input_type, + ]), + ]); } + } else { + $context->vars_in_scope[$var_id] = $input_type; } }