Skip to content

Commit

Permalink
Refactor CallableTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
klimick committed Jan 28, 2024
1 parent 5c4b960 commit 5c91088
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions src/Psalm/Type/Atomic/CallableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public function replaceGenericCallableWithContextualType(Atomic $container_calla
* @param TCallable|TClosure $container_callable
* @return static
*/
public function fillTemplateResult(
public function promoteAnonymousTemplates(
Atomic $container_callable,
Codebase $codebase,
TemplateResult $template_result
Expand All @@ -347,34 +347,48 @@ public function fillTemplateResult(
);
}

if ($container_callable->return_type !== null) {
/** @psalm-suppress ImpureMethodCall */
TemplateStandinTypeReplacer::fillTemplateResult(
$container_callable->return_type,
$template_result,
$codebase,
null,
$this->return_type,
);
}

return $this;
}

/**
* @return static
*/
public function resolveTemplateCollisions(TemplateResult $template_result, Codebase $codebase): Atomic
public function resolveAnonymousTemplateCollisions(TemplateResult $template_result, Codebase $codebase): Atomic
{
$input_type_map = $this->getTemplateMap();
$incremented = [];

foreach ($template_result->lower_bounds as $type_map) {
foreach ($type_map as $bounds) {
foreach ($bounds as $bound) {
foreach ($bound->type->getTemplateTypes() as $template) {
if (!isset($input_type_map[$template->param_name]['anonymous-fn'])) {
continue;
}

if (isset($incremented[$template->param_name])) {
continue;
}

$param_offset = isset($template_result->used_anonymous_template_names[$template->param_name])
? $template_result->used_anonymous_template_names[$template->param_name] + 1
: 1;

$template_result->used_anonymous_template_names[$template->param_name] = $param_offset;
$incremented[$template->param_name] = true;
}
}
}
}

$collisions = [];

foreach ($this->templates ?? [] as $t) {
if (!isset($template_result->used_anonymous_template_names[$t->param_name])) {
$template_result->used_anonymous_template_names[$t->param_name] = 1;
continue;
}

$param_offset = $template_result->used_anonymous_template_names[$t->param_name]++;
$param_offset = $template_result->used_anonymous_template_names[$t->param_name];

$collisions[$t->param_name][$t->defining_class] = new Union([
$t->replaceParamName($t->param_name.$param_offset),
Expand Down Expand Up @@ -405,18 +419,18 @@ protected function replaceCallableTemplateTypesWithStandins(
&& $input_type->templates !== null
&& $this->templates === null
) {
$input_type = null !== $template_result->contextual_template_result
? $input_type->replaceGenericCallableWithContextualType(
$this->replaceTemplateTypesWithArgTypes($template_result->contextual_template_result, $codebase),
$codebase,
)
: $input_type
->resolveTemplateCollisions($template_result, $codebase)
->fillTemplateResult(
$input_type = $template_result->contextual_template_result === null
? $input_type
->resolveAnonymousTemplateCollisions($template_result, $codebase)
->promoteAnonymousTemplates($this, $codebase, $template_result)
->replaceGenericCallableWithContextualType(
$this->replaceTemplateTypesWithArgTypes($template_result, $codebase),
$codebase,
$template_result,
);
)
: $input_type->replaceGenericCallableWithContextualType(
$this->replaceTemplateTypesWithArgTypes($template_result->contextual_template_result, $codebase),
$codebase,
);
}

$replaced = false;
Expand Down

0 comments on commit 5c91088

Please sign in to comment.