From ba8234dd2caca77c537b108b53724d53c1f03369 Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Tue, 25 Jan 2022 17:03:13 -0600 Subject: [PATCH 1/2] Fix crash due to unresolvable constant. --- .../FunctionLike/ReturnTypeAnalyzer.php | 6 +- .../Analyzer/FunctionLikeAnalyzer.php | 4 +- src/Psalm/Internal/Type/TypeExpander.php | 78 ++++++++++++------- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php index 6f68fc02250..76e23c93eae 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php @@ -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( diff --git a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php index 551e7e8b73c..d329e7f8cc9 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php @@ -1025,7 +1025,9 @@ private function processParams( true, false, false, - true + true, + false, + true, ); } catch (UnresolvableConstantException $e) { if ($function_param->type_location !== null) { diff --git a/src/Psalm/Internal/Type/TypeExpander.php b/src/Psalm/Internal/Type/TypeExpander.php index 230dd5d5592..d9b787d9c6e 100644 --- a/src/Psalm/Internal/Type/TypeExpander.php +++ b/src/Psalm/Internal/Type/TypeExpander.php @@ -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; @@ -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) { @@ -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 @@ -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) { @@ -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) { @@ -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) { @@ -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( @@ -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); } @@ -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); } } @@ -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); @@ -437,7 +447,8 @@ public static function expandAtomic( $evaluate_conditional_types, $final, $expand_generic, - $expand_templates + $expand_templates, + $throw_on_unresolvable_constant, ); $potential_ints = []; @@ -464,7 +475,8 @@ public static function expandAtomic( $evaluate_conditional_types, $final, $expand_generic, - $expand_templates + $expand_templates, + $throw_on_unresolvable_constant, ); } @@ -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) { @@ -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) { @@ -513,7 +527,8 @@ public static function expandAtomic( $evaluate_conditional_types, $final, $expand_generic, - $expand_templates + $expand_templates, + $throw_on_unresolvable_constant, ); } @@ -529,7 +544,8 @@ public static function expandAtomic( $evaluate_conditional_types, $final, $expand_generic, - $expand_templates + $expand_templates, + $throw_on_unresolvable_constant, ); } } @@ -550,7 +566,8 @@ public static function expandAtomic( $evaluate_conditional_types, $final, $expand_generic, - $expand_templates + $expand_templates, + $throw_on_unresolvable_constant, ); } } @@ -566,7 +583,8 @@ public static function expandAtomic( $evaluate_conditional_types, $final, $expand_generic, - $expand_templates + $expand_templates, + $throw_on_unresolvable_constant, ); } } @@ -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, @@ -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; @@ -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) { @@ -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( @@ -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( @@ -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( @@ -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( @@ -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]; From a208623fa72cb04241b2b32ad985e312bbcc0bea Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Tue, 25 Jan 2022 17:09:51 -0600 Subject: [PATCH 2/2] Fix trailing commas for PHP < 8.0. --- src/Psalm/Internal/Type/TypeExpander.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Type/TypeExpander.php b/src/Psalm/Internal/Type/TypeExpander.php index d9b787d9c6e..5901ced6e19 100644 --- a/src/Psalm/Internal/Type/TypeExpander.php +++ b/src/Psalm/Internal/Type/TypeExpander.php @@ -67,7 +67,7 @@ public static function expandUnion( bool $final = false, bool $expand_generic = false, bool $expand_templates = false, - bool $throw_on_unresolvable_constant = false, + bool $throw_on_unresolvable_constant = false ): Union { $return_type = clone $return_type; @@ -137,7 +137,7 @@ public static function expandAtomic( bool $final = false, bool $expand_generic = false, bool $expand_templates = false, - bool $throw_on_unresolvable_constant = false, + bool $throw_on_unresolvable_constant = false ): array { if ($return_type instanceof TNamedObject || $return_type instanceof TTemplateParam @@ -702,7 +702,7 @@ private static function expandConditional( bool $final = false, bool $expand_generic = false, bool $expand_templates = false, - bool $throw_on_unresolvable_constant = false, + bool $throw_on_unresolvable_constant = false ): array { $new_as_type = self::expandUnion( $codebase,