Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
klimick committed Aug 15, 2023
1 parent 1539518 commit 2857438
Show file tree
Hide file tree
Showing 19 changed files with 29 additions and 1,660 deletions.
8 changes: 0 additions & 8 deletions psalm-baseline.xml
Expand Up @@ -374,14 +374,6 @@
<code><![CDATA[$array->properties[0]]]></code>
</PossiblyUndefinedIntArrayOffset>
</file>
<file src="src/Psalm/Internal/Type/Comparator/CallableTypeComparator.php">
<LessSpecificReturnStatement>
<code>$callable</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code>TCallable|TClosure|null</code>
</MoreSpecificReturnType>
</file>
<file src="src/Psalm/Internal/Type/SimpleAssertionReconciler.php">
<PossiblyUndefinedIntArrayOffset>
<code><![CDATA[$array_atomic_type->properties[0]]]></code>
Expand Down
Expand Up @@ -898,7 +898,7 @@ public static function analyzeAssignmentRef(
PhpParser\Node\Expr\AssignRef $stmt,
Context $context
): bool {
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context, false, null, false, null, true);
ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context, false, null, false, true);

$lhs_var_id = ExpressionIdentifier::getExtendedVarId(
$stmt->var,
Expand Down
Expand Up @@ -195,19 +195,7 @@ public static function analyze(
$toggled_class_exists = true;
}

$high_order_template_result = null;
$high_order_callable_info = $param
? HighOrderFunctionArgHandler::getCallableArgInfo($context, $arg->value, $statements_analyzer, $param)
: null;

if ($param && $high_order_callable_info) {
$high_order_template_result = HighOrderFunctionArgHandler::remapLowerBounds(
$statements_analyzer,
$template_result ?? new TemplateResult([], []),
$high_order_callable_info,
$param->type ?? Type::getMixed(),
);
} elseif (($arg->value instanceof PhpParser\Node\Expr\Closure
if (($arg->value instanceof PhpParser\Node\Expr\Closure
|| $arg->value instanceof PhpParser\Node\Expr\ArrowFunction)
&& $param
&& !$arg->value->getDocComment()
Expand All @@ -227,32 +215,14 @@ public static function analyze(
$was_inside_call = $context->inside_call;
$context->inside_call = true;

if (ExpressionAnalyzer::analyze(
$statements_analyzer,
$arg->value,
$context,
false,
null,
false,
$high_order_template_result,
) === false) {
if (ExpressionAnalyzer::analyze($statements_analyzer, $arg->value, $context) === false) {
$context->inside_call = $was_inside_call;

return false;
}

$context->inside_call = $was_inside_call;

if ($high_order_callable_info && $high_order_template_result) {
HighOrderFunctionArgHandler::enhanceCallableArgType(
$context,
$arg->value,
$statements_analyzer,
$high_order_callable_info,
$high_order_template_result,
);
}

if (($argument_offset === 0 && $method_id === 'array_filter' && count($args) === 2)
|| ($argument_offset > 0 && $method_id === 'array_map' && count($args) >= 2)
) {
Expand All @@ -266,28 +236,6 @@ public static function analyze(
);
}

$inferred_arg_type = $statements_analyzer->node_data->getType($arg->value);

if (null !== $inferred_arg_type
&& null !== $template_result
&& null !== $param
&& null !== $param->type
&& !$arg->unpack
) {
$codebase = $statements_analyzer->getCodebase();

TemplateStandinTypeReplacer::fillTemplateResult(
$param->type,
$template_result,
$codebase,
$statements_analyzer,
$inferred_arg_type,
$argument_offset,
$context->self,
$context->calling_method_id ?: $context->calling_function_id,
);
}

if ($toggled_class_exists) {
$context->inside_class_exists = false;
}
Expand Down Expand Up @@ -468,28 +416,13 @@ private static function handleClosureArg(
if ($replaced_type_part instanceof TCallable
|| $replaced_type_part instanceof TClosure
) {
if (isset($replaced_type_part->params[$closure_param_offset]->type)) {
$replaced_param_type = $replaced_type_part->params[$closure_param_offset]->type;

if ($replaced_param_type->hasTemplate()) {
$replaced_param_type = TypeExpander::expandUnion(
$codebase,
$replaced_param_type,
null,
null,
null,
true,
false,
false,
true,
true,
);
}

if (isset($replaced_type_part->params[$closure_param_offset]->type)
&& !$replaced_type_part->params[$closure_param_offset]->type->hasTemplate()
) {
if ($param_storage->type && !$param_type_inferred) {
$type_match_found = UnionTypeComparator::isContainedBy(
$codebase,
$replaced_param_type,
$replaced_type_part->params[$closure_param_offset]->type,
$param_storage->type,
);

Expand All @@ -500,7 +433,7 @@ private static function handleClosureArg(

$newly_inferred_type = Type::combineUnionTypes(
$newly_inferred_type,
$replaced_param_type,
$replaced_type_part->params[$closure_param_offset]->type,
$codebase,
);
}
Expand Down
Expand Up @@ -83,8 +83,7 @@ class FunctionCallAnalyzer extends CallAnalyzer
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\FuncCall $stmt,
Context $context,
?TemplateResult $template_result = null
Context $context
): bool {
$function_name = $stmt->name;

Expand Down Expand Up @@ -167,23 +166,14 @@ public static function analyze(
$set_inside_conditional = true;
}

if (!$template_result) {
$template_result = new TemplateResult([], []);
}

if (!$is_first_class_callable) {
if (isset($function_call_info->function_storage->template_types)) {
$template_result->template_types += $function_call_info->function_storage->template_types ?: [];
}

ArgumentsAnalyzer::analyze(
$statements_analyzer,
$stmt->getArgs(),
$function_call_info->function_params,
$function_call_info->function_id,
$function_call_info->allow_named_args,
$context,
$template_result,
);
}

Expand All @@ -209,8 +199,6 @@ public static function analyze(
}
}

$already_inferred_lower_bounds = $template_result->lower_bounds;

$template_result = new TemplateResult([], []);

// do this here to allow closure param checks
Expand All @@ -235,11 +223,6 @@ public static function analyze(
$function_call_info->function_id,
);

$template_result->lower_bounds = array_merge(
$template_result->lower_bounds,
$already_inferred_lower_bounds,
);

if ($function_name instanceof PhpParser\Node\Name && $function_call_info->function_id) {
$stmt_type = FunctionCallReturnTypeFetcher::fetch(
$statements_analyzer,
Expand Down

0 comments on commit 2857438

Please sign in to comment.