Skip to content

Commit

Permalink
Merge pull request #7490 from AndrolGenhald/bugfix/dont-crash-on-unre…
Browse files Browse the repository at this point in the history
…solvable-constant

Fix crash due to unresolvable constant.
  • Loading branch information
orklah committed Jan 26, 2022
2 parents 1cc9d1c + a208623 commit e5a7e00
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
Expand Up @@ -831,7 +831,11 @@ public static function checkReturnType(
$classlike_storage->name ?? null,
$parent_class,
true,
true
true,
false,
false,
false,
true,
);
} catch (UnresolvableConstantException $e) {
IssueBuffer::maybeAdd(
Expand Down
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -1025,7 +1025,9 @@ private function processParams(
true,
false,
false,
true
true,
false,
true,
);
} catch (UnresolvableConstantException $e) {
if ($function_param->type_location !== null) {
Expand Down
78 changes: 52 additions & 26 deletions src/Psalm/Internal/Type/TypeExpander.php
Expand Up @@ -66,7 +66,8 @@ public static function expandUnion(
bool $evaluate_conditional_types = false,
bool $final = false,
bool $expand_generic = false,
bool $expand_templates = false
bool $expand_templates = false,
bool $throw_on_unresolvable_constant = false
): Union {
$return_type = clone $return_type;

Expand All @@ -85,7 +86,8 @@ public static function expandUnion(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($return_type_part instanceof TTypeAlias || count($parts) > 1) {
Expand Down Expand Up @@ -134,7 +136,8 @@ public static function expandAtomic(
bool $evaluate_conditional_types = false,
bool $final = false,
bool $expand_generic = false,
bool $expand_templates = false
bool $expand_templates = false,
bool $throw_on_unresolvable_constant = false
): array {
if ($return_type instanceof TNamedObject
|| $return_type instanceof TTemplateParam
Expand All @@ -152,7 +155,8 @@ public static function expandAtomic(
$evaluate_class_constants,
$evaluate_conditional_types,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($extra_type instanceof TNamedObject && $extra_type->extra_types) {
Expand Down Expand Up @@ -197,7 +201,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($new_as_type instanceof TNamedObject) {
Expand All @@ -215,7 +220,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($expand_templates) {
Expand Down Expand Up @@ -324,7 +330,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$recursively_fleshed_out_types = array_merge(
Expand All @@ -349,7 +356,9 @@ public static function expandAtomic(
}

if ($evaluate_class_constants) {
if (!$codebase->classOrInterfaceExists($return_type->fq_classlike_name)) {
if ($throw_on_unresolvable_constant
&& !$codebase->classOrInterfaceExists($return_type->fq_classlike_name)
) {
throw new UnresolvableConstantException($return_type->fq_classlike_name, $return_type->const_name);
}

Expand Down Expand Up @@ -379,7 +388,7 @@ public static function expandAtomic(
return array_values($const_type_atomic->type_params[1]->getAtomicTypes());
}
}
} else {
} elseif ($throw_on_unresolvable_constant) {
throw new UnresolvableConstantException($return_type->fq_classlike_name, $return_type->const_name);
}
}
Expand All @@ -405,7 +414,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$new_value_type = reset($new_value_type);
Expand Down Expand Up @@ -437,7 +447,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$potential_ints = [];
Expand All @@ -464,7 +475,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}

Expand All @@ -484,7 +496,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
} elseif ($return_type instanceof TKeyedArray) {
Expand All @@ -499,7 +512,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
} elseif ($return_type instanceof TList) {
Expand All @@ -513,7 +527,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}

Expand All @@ -529,7 +544,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
}
Expand All @@ -550,7 +566,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
}
Expand All @@ -566,7 +583,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
}
Expand Down Expand Up @@ -683,7 +701,8 @@ private static function expandConditional(
bool $evaluate_conditional_types = false,
bool $final = false,
bool $expand_generic = false,
bool $expand_templates = false
bool $expand_templates = false,
bool $throw_on_unresolvable_constant = false
): array {
$new_as_type = self::expandUnion(
$codebase,
Expand All @@ -695,7 +714,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$return_type->as_type = $new_as_type;
Expand All @@ -715,7 +735,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if (count($candidate) === 1) {
Expand All @@ -737,7 +758,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$if_conditional_return_types = array_merge(
Expand All @@ -759,7 +781,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$else_conditional_return_types = array_merge(
Expand Down Expand Up @@ -852,7 +875,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$return_type->if_type = self::expandUnion(
Expand All @@ -865,7 +889,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$return_type->else_type = self::expandUnion(
Expand All @@ -878,7 +903,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

return [$return_type];
Expand Down

0 comments on commit e5a7e00

Please sign in to comment.