2020-03-12 04:15:15 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\Call;
|
|
|
|
|
|
|
|
use Psalm\Codebase;
|
2020-05-30 22:55:18 +02:00
|
|
|
use Psalm\Internal\Type\TypeExpander;
|
2020-03-12 04:15:15 +01:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
|
|
|
use Psalm\Type;
|
2020-05-30 22:55:18 +02:00
|
|
|
use Psalm\Type\Atomic;
|
2020-03-12 04:15:15 +01:00
|
|
|
use Psalm\Type\Atomic\TGenericObject;
|
2020-05-30 22:55:18 +02:00
|
|
|
use Psalm\Type\Atomic\TScalarClassConstant;
|
|
|
|
|
2020-03-12 04:15:15 +01:00
|
|
|
use function array_merge;
|
|
|
|
use function array_search;
|
|
|
|
use function array_keys;
|
|
|
|
|
|
|
|
class ClassTemplateParamCollector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param lowercase-string $method_name
|
2020-11-27 17:43:23 +01:00
|
|
|
* @return array<string, array<string, Type\Union>>|null
|
2020-03-12 04:15:15 +01:00
|
|
|
*/
|
|
|
|
public static function collect(
|
|
|
|
Codebase $codebase,
|
|
|
|
ClassLikeStorage $class_storage,
|
2020-05-27 05:29:37 +02:00
|
|
|
ClassLikeStorage $static_class_storage,
|
2020-09-07 01:36:47 +02:00
|
|
|
?string $method_name = null,
|
|
|
|
?Type\Atomic $lhs_type_part = null,
|
2020-11-29 23:40:52 +01:00
|
|
|
bool $self_call = false
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?array {
|
2020-05-27 05:29:37 +02:00
|
|
|
$static_fq_class_name = $static_class_storage->name;
|
2020-03-12 04:15:15 +01:00
|
|
|
|
|
|
|
$non_trait_class_storage = $class_storage->is_trait
|
|
|
|
? $static_class_storage
|
|
|
|
: $class_storage;
|
|
|
|
|
|
|
|
$template_types = $class_storage->template_types;
|
|
|
|
|
|
|
|
$candidate_class_storages = [$class_storage];
|
|
|
|
|
2020-11-29 21:05:32 +01:00
|
|
|
if ($static_class_storage->template_extended_params
|
2020-03-12 04:15:15 +01:00
|
|
|
&& $method_name
|
|
|
|
&& !empty($non_trait_class_storage->overridden_method_ids[$method_name])
|
|
|
|
&& isset($class_storage->methods[$method_name])
|
|
|
|
&& (!isset($non_trait_class_storage->methods[$method_name]->return_type)
|
|
|
|
|| $class_storage->methods[$method_name]->inherited_return_type)
|
|
|
|
) {
|
|
|
|
foreach ($non_trait_class_storage->overridden_method_ids[$method_name] as $overridden_method_id) {
|
|
|
|
$overridden_storage = $codebase->methods->getStorage($overridden_method_id);
|
|
|
|
|
|
|
|
if (!$overridden_storage->return_type) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($overridden_storage->return_type->isNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fq_overridden_class = $overridden_method_id->fq_class_name;
|
|
|
|
|
|
|
|
$overridden_class_storage = $codebase->classlike_storage_provider->get($fq_overridden_class);
|
|
|
|
|
|
|
|
$overridden_template_types = $overridden_class_storage->template_types;
|
|
|
|
|
|
|
|
if (!$template_types) {
|
|
|
|
$template_types = $overridden_template_types;
|
|
|
|
} elseif ($overridden_template_types) {
|
|
|
|
foreach ($overridden_template_types as $template_name => $template_map) {
|
|
|
|
if (isset($template_types[$template_name])) {
|
|
|
|
$template_types[$template_name] = array_merge(
|
|
|
|
$template_types[$template_name],
|
|
|
|
$template_map
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$template_types[$template_name] = $template_map;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$candidate_class_storages[] = $overridden_class_storage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$template_types) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class_template_params = [];
|
2020-11-29 21:05:32 +01:00
|
|
|
$e = $static_class_storage->template_extended_params;
|
2020-03-12 04:15:15 +01:00
|
|
|
|
|
|
|
if ($lhs_type_part instanceof TGenericObject) {
|
2020-12-29 13:24:33 +01:00
|
|
|
if ($class_storage === $static_class_storage && $class_storage->template_types) {
|
2020-03-12 04:15:15 +01:00
|
|
|
$i = 0;
|
|
|
|
|
2020-12-29 13:24:33 +01:00
|
|
|
foreach ($class_storage->template_types as $type_name => $_) {
|
2020-03-12 04:15:15 +01:00
|
|
|
if (isset($lhs_type_part->type_params[$i])) {
|
2020-12-29 13:24:33 +01:00
|
|
|
$class_template_params[$type_name][$class_storage->name]
|
|
|
|
= $lhs_type_part->type_params[$i];
|
2020-03-12 04:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($template_types as $type_name => $_) {
|
|
|
|
if (isset($class_template_params[$type_name])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($class_storage !== $static_class_storage
|
|
|
|
&& isset($e[$class_storage->name][$type_name])
|
|
|
|
) {
|
|
|
|
$input_type_extends = $e[$class_storage->name][$type_name];
|
|
|
|
|
|
|
|
$output_type_extends = null;
|
|
|
|
|
|
|
|
foreach ($input_type_extends->getAtomicTypes() as $type_extends_atomic) {
|
|
|
|
if ($type_extends_atomic instanceof Type\Atomic\TTemplateParam) {
|
|
|
|
if (isset($static_class_storage->template_types[$type_extends_atomic->param_name])) {
|
|
|
|
$mapped_offset = array_search(
|
|
|
|
$type_extends_atomic->param_name,
|
2020-11-29 03:05:31 +01:00
|
|
|
array_keys($static_class_storage->template_types),
|
|
|
|
true
|
2020-03-12 04:15:15 +01:00
|
|
|
);
|
|
|
|
|
2020-11-29 03:05:31 +01:00
|
|
|
if ($mapped_offset !== false
|
|
|
|
&& isset($lhs_type_part->type_params[$mapped_offset])
|
|
|
|
) {
|
|
|
|
$candidate_type = $lhs_type_part->type_params[$mapped_offset];
|
2020-03-12 04:15:15 +01:00
|
|
|
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
$output_type_extends = $candidate_type;
|
|
|
|
} else {
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
$candidate_type,
|
|
|
|
$output_type_extends
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif (isset(
|
|
|
|
$static_class_storage
|
2020-11-29 21:05:32 +01:00
|
|
|
->template_extended_params
|
2020-03-12 04:15:15 +01:00
|
|
|
[$type_extends_atomic->defining_class]
|
|
|
|
[$type_extends_atomic->param_name]
|
|
|
|
)) {
|
|
|
|
$mapped_offset = array_search(
|
|
|
|
$type_extends_atomic->param_name,
|
2020-11-29 21:05:32 +01:00
|
|
|
array_keys(
|
|
|
|
$static_class_storage->template_extended_params
|
|
|
|
[$type_extends_atomic->defining_class]
|
|
|
|
),
|
2020-11-29 15:26:39 +01:00
|
|
|
true
|
2020-03-12 04:15:15 +01:00
|
|
|
);
|
|
|
|
|
2020-11-29 03:05:31 +01:00
|
|
|
if ($mapped_offset !== false
|
|
|
|
&& isset($lhs_type_part->type_params[$mapped_offset])
|
|
|
|
) {
|
|
|
|
$candidate_type = $lhs_type_part->type_params[$mapped_offset];
|
2020-03-12 04:15:15 +01:00
|
|
|
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
$output_type_extends = $candidate_type;
|
|
|
|
} else {
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
$candidate_type,
|
|
|
|
$output_type_extends
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$output_type_extends) {
|
|
|
|
$output_type_extends = new Type\Union([$type_extends_atomic]);
|
|
|
|
} else {
|
|
|
|
$output_type_extends = Type::combineUnionTypes(
|
|
|
|
new Type\Union([$type_extends_atomic]),
|
|
|
|
$output_type_extends
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:40:52 +01:00
|
|
|
if (!$self_call || $static_fq_class_name !== $class_storage->name) {
|
2020-11-27 17:43:23 +01:00
|
|
|
$class_template_params[$type_name][$class_storage->name]
|
|
|
|
= $output_type_extends ?: Type::getMixed();
|
2020-03-12 04:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:40:52 +01:00
|
|
|
if ((!$self_call || $static_fq_class_name !== $class_storage->name)
|
2020-03-12 04:15:15 +01:00
|
|
|
&& !isset($class_template_params[$type_name])
|
|
|
|
) {
|
2020-11-27 17:43:23 +01:00
|
|
|
$class_template_params[$type_name] = [$class_storage->name => Type::getMixed()];
|
2020-03-12 04:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($template_types as $type_name => $type_map) {
|
2020-11-27 17:43:23 +01:00
|
|
|
foreach ($type_map as $type) {
|
2020-03-12 04:15:15 +01:00
|
|
|
foreach ($candidate_class_storages as $candidate_class_storage) {
|
|
|
|
if ($candidate_class_storage !== $static_class_storage
|
|
|
|
&& isset($e[$candidate_class_storage->name][$type_name])
|
|
|
|
&& !isset($class_template_params[$type_name][$candidate_class_storage->name])
|
|
|
|
) {
|
2020-11-27 17:43:23 +01:00
|
|
|
$class_template_params[$type_name][$candidate_class_storage->name] = new Type\Union(
|
|
|
|
self::expandType(
|
|
|
|
$codebase,
|
|
|
|
$e[$candidate_class_storage->name][$type_name],
|
|
|
|
$e,
|
|
|
|
$static_class_storage->name,
|
|
|
|
$static_class_storage->template_types
|
2020-03-12 04:15:15 +01:00
|
|
|
)
|
2020-11-27 17:43:23 +01:00
|
|
|
);
|
2020-03-12 04:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 23:40:52 +01:00
|
|
|
if (!$self_call) {
|
2020-03-12 04:15:15 +01:00
|
|
|
if (!isset($class_template_params[$type_name])) {
|
2020-11-27 17:43:23 +01:00
|
|
|
$class_template_params[$type_name][$class_storage->name] = $type;
|
2020-03-12 04:15:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $class_template_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-29 21:05:32 +01:00
|
|
|
* @param array<string, array<string, Type\Union>> $e
|
2020-03-12 04:15:15 +01:00
|
|
|
* @return non-empty-list<Type\Atomic>
|
|
|
|
*/
|
|
|
|
private static function expandType(
|
2020-05-30 22:55:18 +02:00
|
|
|
Codebase $codebase,
|
2020-03-12 04:15:15 +01:00
|
|
|
Type\Union $input_type_extends,
|
|
|
|
array $e,
|
|
|
|
string $static_fq_class_name,
|
|
|
|
?array $static_template_types
|
|
|
|
) : array {
|
|
|
|
$output_type_extends = [];
|
|
|
|
|
|
|
|
foreach ($input_type_extends->getAtomicTypes() as $type_extends_atomic) {
|
|
|
|
if ($type_extends_atomic instanceof Type\Atomic\TTemplateParam
|
|
|
|
&& ($static_fq_class_name !== $type_extends_atomic->defining_class
|
|
|
|
|| !isset($static_template_types[$type_extends_atomic->param_name]))
|
|
|
|
&& isset($e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name])
|
|
|
|
) {
|
|
|
|
$output_type_extends = array_merge(
|
|
|
|
$output_type_extends,
|
|
|
|
self::expandType(
|
2020-05-30 22:55:18 +02:00
|
|
|
$codebase,
|
2020-03-12 04:15:15 +01:00
|
|
|
$e[$type_extends_atomic->defining_class][$type_extends_atomic->param_name],
|
|
|
|
$e,
|
|
|
|
$static_fq_class_name,
|
|
|
|
$static_template_types
|
|
|
|
)
|
|
|
|
);
|
2020-05-30 22:55:18 +02:00
|
|
|
} elseif ($type_extends_atomic instanceof TScalarClassConstant) {
|
|
|
|
$expanded = TypeExpander::expandAtomic(
|
|
|
|
$codebase,
|
|
|
|
$type_extends_atomic,
|
|
|
|
$type_extends_atomic->fq_classlike_name,
|
|
|
|
$type_extends_atomic->fq_classlike_name,
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($expanded instanceof Atomic) {
|
|
|
|
$output_type_extends[] = $expanded;
|
|
|
|
} else {
|
|
|
|
foreach ($expanded as $type) {
|
|
|
|
$output_type_extends[] = $type;
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 04:15:15 +01:00
|
|
|
} else {
|
|
|
|
$output_type_extends[] = $type_extends_atomic;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output_type_extends;
|
|
|
|
}
|
|
|
|
}
|