Skip to content

Commit

Permalink
Rename $fake_var_id to $fake_var_descriminator.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrolGenhald committed Dec 15, 2021
1 parent 03ccb9b commit 4892501
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Expand Up @@ -186,15 +186,15 @@ 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,
$context,
$function_call_arg,
array_slice($call_args, 0, 1),
$assertions,
$fake_var_id
$fake_var_discriminator
);

$array_var_id = ExpressionIdentifier::getArrayVarId(
Expand All @@ -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(
Expand All @@ -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)
Expand Down
Expand Up @@ -342,7 +342,8 @@ private static function executeFakeCall(
/**
* @param non-empty-array<int, string> $mapping_function_ids
* @param list<PhpParser\Node\Arg> $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<string, array<array<int, string>>>|null $assertions
*/
public static function getReturnTypeFromMappingIds(
Expand All @@ -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;

Expand All @@ -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;
}

Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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]);
}
}
Expand Down

0 comments on commit 4892501

Please sign in to comment.