Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 19, 2023
1 parent 5088dc0 commit 3e6294e
Showing 1 changed file with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ private static function coerceValueAfterGatekeeperArgument(
return;
}

$var_id = ExpressionIdentifier::getVarId(
$input_expr,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
);

if (!$var_id) {
return;
}

if (!$input_type_changed && $param_type->from_docblock && !$input_type->hasMixed()) {
$types = $input_type->getAtomicTypes();
foreach ($param_type->getAtomicTypes() as $param_atomic_type) {
Expand Down Expand Up @@ -1382,74 +1392,67 @@ private static function coerceValueAfterGatekeeperArgument(
$input_type = new Union($types);
}

$var_id = ExpressionIdentifier::getVarId(
$input_expr,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
);

if ($var_id) {
$was_cloned = false;

if ($input_type->isNullable() && !$param_type->isNullable()) {
$input_type = $input_type->getBuilder();
$was_cloned = true;
$input_type->removeType('null');
$input_type = $input_type->freeze();
}
$was_cloned = false;

if ($input_type->getId() === $param_type->getId()) {
if ($input_type->from_docblock) {
$input_type = $input_type->setFromDocblock(false);
}
} elseif ($input_type->hasMixed() && $signature_param_type) {
$was_cloned = true;
$parent_nodes = $input_type->parent_nodes;
$by_ref = $input_type->by_ref;
$input_type = $signature_param_type->setProperties([
'ignore_nullable_issues' => $signature_param_type->isNullable(),
'parent_nodes' => $parent_nodes,
'by_ref' => $by_ref,
]);
}
if ($input_type->isNullable() && !$param_type->isNullable()) {
$input_type = $input_type->getBuilder();
$was_cloned = true;
$input_type->removeType('null');
$input_type = $input_type->freeze();
}

if ($context->inside_conditional && !isset($context->assigned_var_ids[$var_id])) {
$context->assigned_var_ids[$var_id] = 0;
if ($input_type->getId() === $param_type->getId()) {
if ($input_type->from_docblock) {
$input_type = $input_type->setFromDocblock(false);
}
} elseif ($input_type->hasMixed() && $signature_param_type) {
$was_cloned = true;
$parent_nodes = $input_type->parent_nodes;
$by_ref = $input_type->by_ref;
$input_type = $signature_param_type->setProperties([
'ignore_nullable_issues' => $signature_param_type->isNullable(),
'parent_nodes' => $parent_nodes,
'by_ref' => $by_ref,
]);
}

if ($was_cloned) {
$context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer);
}
if ($context->inside_conditional && !isset($context->assigned_var_ids[$var_id])) {
$context->assigned_var_ids[$var_id] = 0;
}

if ($unpack) {
if ($unpacked_atomic_array instanceof TArray) {
$unpacked_atomic_array = $unpacked_atomic_array->setTypeParams([
$unpacked_atomic_array->type_params[0],
$input_type,
]);
if ($was_cloned) {
$context->removeVarFromConflictingClauses($var_id, null, $statements_analyzer);
}

$context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]);
} elseif ($unpacked_atomic_array instanceof TKeyedArray
&& $unpacked_atomic_array->is_list
) {
if ($unpacked_atomic_array->isNonEmpty()) {
$unpacked_atomic_array = Type::getNonEmptyListAtomic($input_type);
} else {
$unpacked_atomic_array = Type::getListAtomic($input_type);
}
if ($unpack) {
if ($unpacked_atomic_array instanceof TArray) {
$unpacked_atomic_array = $unpacked_atomic_array->setTypeParams([
$unpacked_atomic_array->type_params[0],
$input_type,
]);

$context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]);
$context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]);
} elseif ($unpacked_atomic_array instanceof TKeyedArray
&& $unpacked_atomic_array->is_list
) {
if ($unpacked_atomic_array->isNonEmpty()) {
$unpacked_atomic_array = Type::getNonEmptyListAtomic($input_type);
} else {
$context->vars_in_scope[$var_id] = new Union([
new TArray([
Type::getInt(),
$input_type,
]),
]);
$unpacked_atomic_array = Type::getListAtomic($input_type);
}

$context->vars_in_scope[$var_id] = new Union([$unpacked_atomic_array]);
} else {
$context->vars_in_scope[$var_id] = $input_type;
$context->vars_in_scope[$var_id] = new Union([
new TArray([
Type::getInt(),
$input_type,
]),
]);
}
} else {
$context->vars_in_scope[$var_id] = $input_type;
}
}

Expand Down

0 comments on commit 3e6294e

Please sign in to comment.