Skip to content

Commit

Permalink
remove isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Dec 20, 2021
1 parent de71e27 commit 2b4b02e
Show file tree
Hide file tree
Showing 31 changed files with 54 additions and 66 deletions.
Expand Up @@ -433,9 +433,7 @@ public static function checkIteratorType(
}

// if it's an empty array, we cannot iterate over it
if ($iterator_atomic_type instanceof TArray
&& $iterator_atomic_type->type_params[1]->isEmpty()
) {
if ($iterator_atomic_type instanceof TArray && $iterator_atomic_type->isEmptyArray()) {
$always_non_empty_array = false;
$has_valid_iterator = true;
continue;
Expand Down
Expand Up @@ -166,7 +166,7 @@ public static function analyze(
);

if (isset($case_vars_in_scope_reconciled[$switch_var_id])
&& $case_vars_in_scope_reconciled[$switch_var_id]->isEmpty()
&& $case_vars_in_scope_reconciled[$switch_var_id]->isNever()
) {
$all_options_matched = true;
}
Expand Down
Expand Up @@ -587,7 +587,7 @@ private static function handleNonReturningCase(
if (!$case->cond
&& $switch_var_id
&& isset($case_context->vars_in_scope[$switch_var_id])
&& $case_context->vars_in_scope[$switch_var_id]->isEmpty()
&& $case_context->vars_in_scope[$switch_var_id]->isNever()
) {
if (IssueBuffer::accepts(
new ParadoxicalCondition(
Expand Down
Expand Up @@ -548,7 +548,7 @@ private static function handleUnpackedArray(
&& count($unpacked_atomic_type->type_params) === 2
)) {
/** @psalm-suppress PossiblyUndefinedArrayOffset provably true, but Psalm can’t see it */
if ($unpacked_atomic_type->type_params[1]->isEmpty()) {
if ($unpacked_atomic_type->type_params[1]->isNever()) {
continue;
}
$array_creation_info->can_create_objectlike = false;
Expand Down Expand Up @@ -582,7 +582,7 @@ private static function handleUnpackedArray(
)
);
} elseif ($unpacked_atomic_type instanceof TList) {
if ($unpacked_atomic_type->type_param->isEmpty()) {
if ($unpacked_atomic_type->type_param->isNever()) {
continue;
}
$array_creation_info->can_create_objectlike = false;
Expand Down
Expand Up @@ -737,7 +737,7 @@ private static function analyzeNestedArrayAssignment(
return;
}

if ($child_stmt_var_type->isEmpty()) {
if ($child_stmt_var_type->isNever()) {
$child_stmt_var_type = Type::getEmptyArray();
$statements_analyzer->node_data->setType($child_stmt->var, $child_stmt_var_type);
}
Expand Down
Expand Up @@ -1032,7 +1032,7 @@ public static function assignByRefParam(
$context->vars_in_scope[$var_id] = $by_ref_out_type;

if (!($stmt_type = $statements_analyzer->node_data->getType($stmt))
|| $stmt_type->isEmpty()
|| $stmt_type->isNever()
) {
$statements_analyzer->node_data->setType($stmt, clone $by_ref_type);
}
Expand All @@ -1047,7 +1047,7 @@ public static function assignByRefParam(

$stmt_type = $statements_analyzer->node_data->getType($stmt);

if (!$stmt_type || $stmt_type->isEmpty()) {
if (!$stmt_type || $stmt_type->isNever()) {
$statements_analyzer->node_data->setType($stmt, clone $by_ref_type);
}

Expand Down
Expand Up @@ -75,9 +75,9 @@ public static function analyze(
$right_type = $nodes->getType($right);
$config = Config::getInstance();

if ($left_type && $left_type->isEmpty()) {
if ($left_type && $left_type->isNever()) {
$left_type = $right_type;
} elseif ($right_type && $right_type->isEmpty()) {
} elseif ($right_type && $right_type->isNever()) {
$right_type = $left_type;
}

Expand Down
Expand Up @@ -1360,7 +1360,7 @@ private static function coerceValueAfterGatekeeperArgument(
&& $input_atomic_type->value === $param_atomic_type->value
) {
foreach ($input_atomic_type->type_params as $i => $type_param) {
if ($type_param->isEmpty() && isset($param_atomic_type->type_params[$i])) {
if ($type_param->isNever() && isset($param_atomic_type->type_params[$i])) {
$input_type_changed = true;

$input_atomic_type->type_params[$i] = clone $param_atomic_type->type_params[$i];
Expand Down
Expand Up @@ -1175,7 +1175,7 @@ private static function evaluateArbitraryParam(
);

foreach ($context->vars_in_scope[$var_id]->getAtomicTypes() as $type) {
if ($type instanceof TArray && $type->type_params[1]->isEmpty()) {
if ($type instanceof TArray && $type->isEmptyArray()) {
$context->vars_in_scope[$var_id]->removeType('array');
$context->vars_in_scope[$var_id]->addType(
new TArray(
Expand Down
Expand Up @@ -391,14 +391,13 @@ private static function getReturnTypeFromCallMapWithArgs(
foreach ($atomic_types['array']->properties as $property) {
// empty, never and possibly undefined can't count for min value
if (!$property->possibly_undefined
&& !$property->isEmpty()
&& !$property->isNever()
) {
$min++;
}

//empty and never can't count for max value because we know keys are undefined
if (!$property->isEmpty() && !$property->isNever()) {
//never can't count for max value because we know keys are undefined
if (!$property->isNever()) {
$max++;
}
}
Expand All @@ -417,8 +416,7 @@ private static function getReturnTypeFromCallMapWithArgs(
}

if ($atomic_types['array'] instanceof TArray
&& $atomic_types['array']->type_params[0]->isEmpty()
&& $atomic_types['array']->type_params[1]->isEmpty()
&& $atomic_types['array']->isEmptyArray()
) {
return Type::getInt(false, 0);
}
Expand Down
Expand Up @@ -1105,10 +1105,9 @@ private static function handleArrayAccessOnArray(

if ($in_assignment
&& $type instanceof TArray
&& $type->type_params[0]->isEmpty()
&& $type->type_params[1]->isEmpty()
&& $type->isEmptyArray()
) {
$from_empty_array = $type->type_params[0]->isEmpty() && $type->type_params[1]->isEmpty();
$from_empty_array = $type->isEmptyArray();

if (count($key_values) === 1) {
$from_mixed_array = $type->type_params[1]->isMixed();
Expand Down Expand Up @@ -1240,12 +1239,12 @@ private static function handleArrayAccessOnTArray(
): void {
// if we're assigning to an empty array with a key offset, refashion that array
if ($in_assignment) {
if ($type->type_params[0]->isEmpty()) {
if ($type->isEmptyArray()) {
$type->type_params[0] = $offset_type->isMixed()
? Type::getArrayKey()
: $offset_type;
}
} elseif (!$type->type_params[0]->isEmpty()) {
} elseif (!$type->isEmptyArray()) {
$expected_offset_type = $type->type_params[0]->hasMixed()
? new Union([new TArrayKey])
: $type->type_params[0];
Expand Down Expand Up @@ -1366,7 +1365,7 @@ private static function handleArrayAccessOnTArray(
$type->type_params[1]
);

if ($array_access_type->isEmpty()
if ($array_access_type->isNever()
&& !$array_type->hasMixed()
&& !$in_assignment
&& !$context->inside_isset
Expand Down
Expand Up @@ -118,7 +118,7 @@ public static function analyze(
return true;
}

if ($stmt_var_type->isEmpty()) {
if ($stmt_var_type->isNever()) {
if (IssueBuffer::accepts(
new MixedPropertyFetch(
'Cannot fetch property on empty var ' . $stmt_var_id,
Expand Down
Expand Up @@ -697,7 +697,7 @@ private static function handleUnpackedArray(
$array_creation_info->property_types[$new_offset] = $property_value;
}
} elseif ($unpacked_atomic_type instanceof TArray) {
if ($unpacked_atomic_type->type_params[1]->isEmpty()) {
if ($unpacked_atomic_type->isEmptyArray()) {
continue;
}
$array_creation_info->can_create_objectlike = false;
Expand All @@ -719,7 +719,7 @@ private static function handleUnpackedArray(
)
);
} elseif ($unpacked_atomic_type instanceof TList) {
if ($unpacked_atomic_type->type_param->isEmpty()) {
if ($unpacked_atomic_type->type_param->isNever()) {
continue;
}
$array_creation_info->can_create_objectlike = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Codebase/ConstantTypeResolver.php
Expand Up @@ -199,7 +199,7 @@ public static function resolve(
$visited_constant_ids + [$c_id => true]
);

if ($spread_array instanceof TArray && $spread_array->type_params[1]->isEmpty()) {
if ($spread_array instanceof TArray && $spread_array->isEmptyArray()) {
continue;
}

Expand Down
Expand Up @@ -109,7 +109,7 @@ static function ($keyed_type) use ($statements_source, $context) {
$first_arg_array->properties
),
static function ($keyed_type) {
return !$keyed_type->isEmpty();
return !$keyed_type->isNever();
}
);

Expand Down
Expand Up @@ -154,7 +154,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
}
}
} else {
if (!$unpacked_type_part->type_params[0]->isEmpty()) {
if (!$unpacked_type_part->isEmptyArray()) {
foreach ($generic_properties as $key => $keyed_type) {
$generic_properties[$key] = Type::combineUnionTypes(
$keyed_type,
Expand All @@ -169,7 +169,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
}

if ($unpacked_type_part instanceof TArray) {
if ($unpacked_type_part->type_params[1]->isEmpty()) {
if ($unpacked_type_part->isEmptyArray()) {
continue;
}

Expand Down
Expand Up @@ -80,7 +80,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
throw new UnexpectedValueException('This should never happen');
}

if ($value_type->isEmpty()) {
if ($value_type->isNever()) {
$value_type = Type::getFalse();
} elseif (($function_id !== 'reset' && $function_id !== 'end') || !$definitely_has_items) {
$value_type->addType(new TFalse);
Expand Down
Expand Up @@ -55,7 +55,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
if ($first_arg_array instanceof TArray) {
$value_type = clone $first_arg_array->type_params[1];

if ($value_type->isEmpty()) {
if ($first_arg_array->isEmptyArray()) {
return Type::getNull();
}

Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php
Expand Up @@ -102,7 +102,7 @@ public static function isContainedBy(

if ($container_type_part instanceof TList
&& $input_type_part instanceof TArray
&& $input_type_part->type_params[1]->isEmpty()
&& $input_type_part->isEmptyArray()
) {
return !$container_type_part instanceof TNonEmptyList;
}
Expand Down Expand Up @@ -225,15 +225,15 @@ function ($i) {
continue;
}

if ($input_param->isEmpty()
if ($input_param->isNever()
&& $container_type_part instanceof TNonEmptyArray
) {
return false;
}

$param_comparison_result = new TypeComparisonResult();

if (!$input_param->isEmpty()) {
if (!$input_param->isNever()) {
if (!UnionTypeComparator::isContainedBy(
$codebase,
$input_param,
Expand Down
Expand Up @@ -475,7 +475,7 @@ public static function isContainedBy(

$array_comparison_result = new TypeComparisonResult();

if (!$input_param->isEmpty()) {
if (!$input_param->isNever()) {
if (!UnionTypeComparator::isContainedBy(
$codebase,
$input_param,
Expand Down
Expand Up @@ -94,7 +94,7 @@ public static function isContainedBy(

$container_param = $container_type_part->type_params[$i];

if ($input_param->isEmpty()) {
if ($input_param->isNever()) {
if ($atomic_comparison_result) {
if (!$atomic_comparison_result->replacement_atomic_type) {
$atomic_comparison_result->replacement_atomic_type = clone $input_type_part;
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php
Expand Up @@ -42,7 +42,7 @@ public static function isContainedBy(

$property_type_comparison = new TypeComparisonResult();

if (!$input_property_type->isEmpty()) {
if (!$input_property_type->isNever()) {
if (!UnionTypeComparator::isContainedBy(
$codebase,
$input_property_type,
Expand Down Expand Up @@ -127,7 +127,7 @@ public static function isContainedByObjectWithProperties(

$property_type_comparison = new TypeComparisonResult();

if (!$input_property_type->isEmpty()
if (!$input_property_type->isNever()
&& !UnionTypeComparator::isContainedBy(
$codebase,
$input_property_type,
Expand Down
9 changes: 1 addition & 8 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Expand Up @@ -514,11 +514,6 @@ private static function reconcileIsset(
}

if ($inside_loop) {
if ($existing_var_type->hasType('empty')) {
$existing_var_type->removeType('empty');
$existing_var_type->addType(new TMixed(true));
}

if ($existing_var_type->hasType('never')) {
$existing_var_type->removeType('never');
$existing_var_type->addType(new TMixed(true));
Expand Down Expand Up @@ -1940,9 +1935,7 @@ private static function reconcileList(
}
}

if ($type->type_params[0]->isEmpty()
|| $type->type_params[1]->isEmpty()
) {
if ($type->isEmptyArray()) {
//we allow an empty array to pass as a list. We keep the type as empty array though (more precise)
$array_types[] = $type;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php
Expand Up @@ -1099,7 +1099,7 @@ function (TemplateBound $bound_a, TemplateBound $bound_b) {
if ($current_depth === null) {
$current_depth = $template_bound->appearance_depth;
} elseif ($current_depth !== $template_bound->appearance_depth && $current_type) {
if (!$current_type->isEmpty()
if (!$current_type->isNever()
&& ($had_invariant || $last_arg_offset === $template_bound->arg_offset)
) {
// escape switches when matching on invariant generic params
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Type/TypeCombiner.php
Expand Up @@ -549,7 +549,7 @@ private static function scrapeTypeProperties(
$combination->array_always_filled = false;
}

if (!$type->type_params[1]->isEmpty()) {
if (!$type->isEmptyArray()) {
$combination->all_arrays_lists = false;
$combination->all_arrays_class_string_maps = false;
}
Expand Down Expand Up @@ -1363,11 +1363,11 @@ private static function handleKeyedArrayEntries(
}

if (!$combination->array_type_params
|| $combination->array_type_params[1]->isEmpty()
|| $combination->array_type_params[1]->isNever()
) {
if (!$overwrite_empty_array
&& ($combination->array_type_params
&& ($combination->array_type_params[1]->isEmpty()
&& ($combination->array_type_params[1]->isNever()
|| $combination->array_type_params[1]->isMixed()))
) {
foreach ($combination->objectlike_entries as $objectlike_entry) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/TypeVisitor/ContainsLiteralVisitor.php
Expand Up @@ -30,7 +30,7 @@ protected function enterNode(TypeNode $type): ?int
return NodeVisitor::STOP_TRAVERSAL;
}

if ($type instanceof TArray && $type->type_params[1]->isEmpty()) {
if ($type instanceof TArray && $type->isEmptyArray()) {
$this->contains_literal = true;
return NodeVisitor::STOP_TRAVERSAL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/GenericTrait.php
Expand Up @@ -99,7 +99,7 @@ public function toNamespacedString(

$value_type = $this->type_params[1];

if ($value_type->isMixed() || $value_type->isEmpty()) {
if ($value_type->isMixed() || $value_type->isNever()) {
return $base_value;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Psalm/Type/Atomic/TArray.php
Expand Up @@ -92,4 +92,9 @@ public function getAssertionString(bool $exact = false): string

return $this->toNamespacedString(null, [], null, false);
}

public function isEmptyArray(): bool
{
return $this->type_params[1]->isNever();
}
}

0 comments on commit 2b4b02e

Please sign in to comment.