Skip to content

Commit

Permalink
Refactor parent type params mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
klimick committed May 2, 2024
1 parent fadb3a0 commit 697d8a9
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ class_storage: self::getDeclaringClassStorage($codebase, $method_storage->defini
$method_storage->template_types ?? [],
),
lower_bounds: array_merge(
self::withParent($codebase, $context, $stmt, $class_lower_bounds),
self::isParentCall($stmt, $context)
? self::mapSelfBoundsToParentBounds(
self_class_storage: $codebase->classlike_storage_provider->get($context->self),
self_bounds: $class_lower_bounds,
)
: $class_lower_bounds,
self::getIfThisIsTypeLowerBounds($statements_analyzer, $method_storage, $lhs_type_part),
),
);
Expand All @@ -106,36 +111,41 @@ private static function getDeclaringClassStorage(
}

/**
* @param array<string, non-empty-array<string, Union>> $lower_bounds
* @return array<string, non-empty-array<string, Union>>
* @psalm-assert-if-true string $context->self
*/
private static function withParent(Codebase $codebase, Context $context, CallLike $stmt, array $lower_bounds): array
private static function isParentCall(CallLike $stmt, Context $context): bool
{
$parent_call = $stmt instanceof StaticCall
return $context->self !== null
&& $stmt instanceof StaticCall
&& $stmt->class instanceof Name
&& $stmt->class->getParts() === ['parent'];
}

$template_extended_params = $parent_call && $context->self !== null
? $codebase->classlike_storage_provider->get($context->self)->template_extended_params ?? []
: [];
/**
* @param array<string, non-empty-array<string, Union>> $self_bounds
* @return array<string, non-empty-array<string, Union>>
*/
private static function mapSelfBoundsToParentBounds(ClassLikeStorage $self_class_storage, array $self_bounds): array
{
$parent_bounds = $self_bounds;

foreach ($template_extended_params as $template_fq_class_name => $extended_types) {
foreach ($self_class_storage->template_extended_params ?? [] as $template_fq_class_name => $extended_types) {
foreach ($extended_types as $type_key => $extended_type) {
if (isset($lower_bounds[$type_key][$template_fq_class_name])) {
$lower_bounds[$type_key][$template_fq_class_name] = $extended_type;
if (isset($parent_bounds[$type_key][$template_fq_class_name])) {
$parent_bounds[$type_key][$template_fq_class_name] = $extended_type;
continue;
}

foreach ($extended_type->getAtomicTypes() as $t) {
$lower_bounds[$type_key][$template_fq_class_name] =
$t instanceof TTemplateParam && isset($lower_bounds[$t->param_name][$t->defining_class])
? $lower_bounds[$t->param_name][$t->defining_class]
$parent_bounds[$type_key][$template_fq_class_name] =
$t instanceof TTemplateParam && isset($parent_bounds[$t->param_name][$t->defining_class])
? $parent_bounds[$t->param_name][$t->defining_class]
: $extended_type;
}
}
}

return $lower_bounds;
return $parent_bounds;
}

/**
Expand Down

0 comments on commit 697d8a9

Please sign in to comment.