From 489250134366faa372d46c6e53939a623eba7929 Mon Sep 17 00:00:00 2001 From: AndrolGenhald Date: Wed, 15 Dec 2021 11:31:54 -0600 Subject: [PATCH] Rename `$fake_var_id` to `$fake_var_descriminator`. --- .../ArrayFilterReturnTypeProvider.php | 11 ++++--- .../ArrayMapReturnTypeProvider.php | 33 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php index 9eed10591b3..6cc324213dd 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php @@ -186,7 +186,7 @@ static function ($keyed_type) { if ($array_arg && $mapping_function_ids) { $assertions = []; - $fake_var_id = mt_rand(); + $fake_var_discriminator = mt_rand(); ArrayMapReturnTypeProvider::getReturnTypeFromMappingIds( $statements_source, $mapping_function_ids, @@ -194,7 +194,7 @@ static function ($keyed_type) { $function_call_arg, array_slice($call_args, 0, 1), $assertions, - $fake_var_id + $fake_var_discriminator ); $array_var_id = ExpressionIdentifier::getArrayVarId( @@ -203,11 +203,12 @@ static function ($keyed_type) { $statements_source ); - if (isset($assertions[$array_var_id . "[\$__fake_{$fake_var_id}_offset_var__]"])) { + if (isset($assertions[$array_var_id . "[\$__fake_{$fake_var_discriminator}_offset_var__]"])) { $changed_var_ids = []; $assertions = [ - '$inner_type' => $assertions[$array_var_id . "[\$__fake_{$fake_var_id}_offset_var__]"] + '$inner_type' => + $assertions["{$array_var_id}[\$__fake_{$fake_var_discriminator}_offset_var__]"], ]; $reconciled_types = Reconciler::reconcileKeyedTypes( @@ -227,7 +228,7 @@ static function ($keyed_type) { } } - ArrayMapReturnTypeProvider::cleanContext($context, $fake_var_id); + ArrayMapReturnTypeProvider::cleanContext($context, $fake_var_discriminator); } } elseif (($function_call_arg->value instanceof PhpParser\Node\Expr\Closure || $function_call_arg->value instanceof PhpParser\Node\Expr\ArrowFunction) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMapReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMapReturnTypeProvider.php index 3b43aae5fbf..4437ff4627f 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMapReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMapReturnTypeProvider.php @@ -342,7 +342,8 @@ private static function executeFakeCall( /** * @param non-empty-array $mapping_function_ids * @param list $array_args - * @param int|null $fake_var_id Set the fake variable id to a known value and don't clear it from the context + * @param int|null $fake_var_discriminator Set the fake variable id to a known value with the discriminator + * as a substring, and don't clear it from the context. * @param-out array>>|null $assertions */ public static function getReturnTypeFromMappingIds( @@ -352,7 +353,7 @@ public static function getReturnTypeFromMappingIds( PhpParser\Node\Arg $function_call_arg, array $array_args, ?array &$assertions = null, - ?int $fake_var_id = null + ?int $fake_var_discriminator = null ): Union { $mapping_return_type = null; @@ -363,8 +364,8 @@ public static function getReturnTypeFromMappingIds( foreach ($mapping_function_ids as $mapping_function_id) { $mapping_function_id_parts = explode('&', $mapping_function_id); - if ($fake_var_id === null) { - $fake_var_id = mt_rand(); + if ($fake_var_discriminator === null) { + $fake_var_discriminator = mt_rand(); $clean_context = true; } @@ -376,7 +377,7 @@ public static function getReturnTypeFromMappingIds( new VirtualArrayDimFetch( $array_arg->value, new VirtualVariable( - "__fake_{$fake_var_id}_offset_var__", + "__fake_{$fake_var_discriminator}_offset_var__", $array_arg->value->getAttributes() ), $array_arg->value->getAttributes() @@ -401,7 +402,7 @@ public static function getReturnTypeFromMappingIds( if ($is_instance) { $fake_method_call = new VirtualMethodCall( new VirtualVariable( - "__fake_{$fake_var_id}_method_call_var__", + "__fake_{$fake_var_discriminator}_method_call_var__", $function_call_arg->getAttributes() ), new VirtualIdentifier( @@ -427,11 +428,9 @@ public static function getReturnTypeFromMappingIds( } } - $context->vars_in_scope["\$__fake_{$fake_var_id}_offset_var__"] = Type::getMixed(); - $context->vars_in_scope["\$__fake_{$fake_var_id}_method_call_var__"] = $lhs_instance_type - ?: new Union([ - new TNamedObject($callable_fq_class_name) - ]); + $context->vars_in_scope["\$__fake_{$fake_var_discriminator}_offset_var__"] = Type::getMixed(); + $context->vars_in_scope["\$__fake_{$fake_var_discriminator}_method_call_var__"] = + $lhs_instance_type ?: new Union([new TNamedObject($callable_fq_class_name)]); $fake_method_return_type = self::executeFakeCall( $statements_source, @@ -453,7 +452,7 @@ public static function getReturnTypeFromMappingIds( $function_call_arg->getAttributes() ); - $context->vars_in_scope["\$__fake_{$fake_var_id}_offset_var__"] = Type::getMixed(); + $context->vars_in_scope["\$__fake_{$fake_var_discriminator}_offset_var__"] = Type::getMixed(); $fake_method_return_type = self::executeFakeCall( $statements_source, @@ -474,7 +473,7 @@ public static function getReturnTypeFromMappingIds( $function_call_arg->getAttributes() ); - $context->vars_in_scope["\$__fake_{$fake_var_id}_offset_var__"] = Type::getMixed(); + $context->vars_in_scope["\$__fake_{$fake_var_discriminator}_offset_var__"] = Type::getMixed(); $fake_function_return_type = self::executeFakeCall( $statements_source, @@ -488,10 +487,10 @@ public static function getReturnTypeFromMappingIds( } if ($clean_context) { - self::cleanContext($context, $fake_var_id); + self::cleanContext($context, $fake_var_discriminator); } - $fake_var_id = null; + $fake_var_discriminator = null; $mapping_return_type = Type::combineUnionTypes( $function_id_return_type, @@ -503,10 +502,10 @@ public static function getReturnTypeFromMappingIds( return $mapping_return_type; } - public static function cleanContext(Context $context, int $fake_var_id): void + public static function cleanContext(Context $context, int $fake_var_discriminator): void { foreach ($context->vars_in_scope as $var_in_scope => $_) { - if (str_contains($var_in_scope, "__fake_{$fake_var_id}_")) { + if (str_contains($var_in_scope, "__fake_{$fake_var_discriminator}_")) { unset($context->vars_in_scope[$var_in_scope]); } }