1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Throw TypeParseTreeException if template type not recognised

This commit is contained in:
Brown 2020-04-04 09:26:19 -04:00
parent c0e573ab6a
commit eef4feea0b

View File

@ -753,48 +753,51 @@ abstract class Type
if ($parse_tree instanceof ParseTree\ConditionalTree) {
$template_param_name = $parse_tree->condition->param_name;
if (isset($template_type_map[$template_param_name])) {
$first_class = array_keys($template_type_map[$template_param_name])[0];
$conditional_type = self::getTypeFromTree(
$parse_tree->condition->children[0],
null,
$template_type_map
);
$if_type = self::getTypeFromTree(
$parse_tree->children[0],
null,
$template_type_map
);
$else_type = self::getTypeFromTree(
$parse_tree->children[1],
null,
$template_type_map
);
if ($conditional_type instanceof Type\Atomic) {
$conditional_type = new Type\Union([$conditional_type]);
}
if ($if_type instanceof Type\Atomic) {
$if_type = new Type\Union([$if_type]);
}
if ($else_type instanceof Type\Atomic) {
$else_type = new Type\Union([$else_type]);
}
return new Atomic\TConditional(
$template_param_name,
$first_class,
$template_type_map[$template_param_name][$first_class][0],
$conditional_type,
$if_type,
$else_type
);
if (!isset($template_type_map[$template_param_name])) {
throw new TypeParseTreeException('Unrecognized template \'' . $template_param_name . '\'');
}
$first_class = array_keys($template_type_map[$template_param_name])[0];
$conditional_type = self::getTypeFromTree(
$parse_tree->condition->children[0],
null,
$template_type_map
);
$if_type = self::getTypeFromTree(
$parse_tree->children[0],
null,
$template_type_map
);
$else_type = self::getTypeFromTree(
$parse_tree->children[1],
null,
$template_type_map
);
if ($conditional_type instanceof Type\Atomic) {
$conditional_type = new Type\Union([$conditional_type]);
}
if ($if_type instanceof Type\Atomic) {
$if_type = new Type\Union([$if_type]);
}
if ($else_type instanceof Type\Atomic) {
$else_type = new Type\Union([$else_type]);
}
return new Atomic\TConditional(
$template_param_name,
$first_class,
$template_type_map[$template_param_name][$first_class][0],
$conditional_type,
$if_type,
$else_type
);
}
if (!$parse_tree instanceof ParseTree\Value) {