From 077d4ce163f9d1fc6ee384501b0ede0698de9cb3 Mon Sep 17 00:00:00 2001 From: orklah Date: Fri, 17 Dec 2021 00:42:14 +0100 Subject: [PATCH] remove isEmpty --- .../Analyzer/Statements/Block/ForeachAnalyzer.php | 4 +--- .../Analyzer/Statements/Block/SwitchAnalyzer.php | 2 +- .../Analyzer/Statements/Block/SwitchCaseAnalyzer.php | 2 +- .../Analyzer/Statements/Expression/ArrayAnalyzer.php | 4 ++-- .../Expression/Assignment/ArrayAssignmentAnalyzer.php | 2 +- .../Statements/Expression/AssignmentAnalyzer.php | 4 ++-- .../Expression/BinaryOp/ArithmeticOpAnalyzer.php | 4 ++-- .../Statements/Expression/Call/ArgumentAnalyzer.php | 2 +- .../Statements/Expression/Call/ArgumentsAnalyzer.php | 2 +- .../Expression/Call/FunctionCallReturnTypeFetcher.php | 8 +++----- .../Expression/Fetch/ArrayFetchAnalyzer.php | 11 +++++------ .../Fetch/InstancePropertyFetchAnalyzer.php | 2 +- .../Statements/Expression/SimpleTypeInferer.php | 4 ++-- src/Psalm/Internal/Codebase/ConstantTypeResolver.php | 2 +- .../ArrayFilterReturnTypeProvider.php | 2 +- .../ArrayMergeReturnTypeProvider.php | 4 ++-- .../ArrayPointerAdjustmentReturnTypeProvider.php | 2 +- .../ReturnTypeProvider/ArrayPopReturnTypeProvider.php | 2 +- .../Internal/Type/Comparator/ArrayTypeComparator.php | 6 +++--- .../Internal/Type/Comparator/AtomicTypeComparator.php | 2 +- .../Type/Comparator/GenericTypeComparator.php | 2 +- .../Internal/Type/Comparator/KeyedArrayComparator.php | 4 ++-- src/Psalm/Internal/Type/SimpleAssertionReconciler.php | 9 +-------- .../Internal/Type/TemplateStandinTypeReplacer.php | 2 +- src/Psalm/Internal/Type/TypeCombiner.php | 6 +++--- .../Internal/TypeVisitor/ContainsLiteralVisitor.php | 2 +- src/Psalm/Type/Atomic/GenericTrait.php | 2 +- src/Psalm/Type/Atomic/TArray.php | 5 +++++ src/Psalm/Type/Reconciler.php | 6 +++--- src/Psalm/Type/Union.php | 5 ----- tests/ArrayAssignmentTest.php | 6 +++--- 31 files changed, 54 insertions(+), 66 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php index 7dcc2b0adda..852abdc8f62 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php @@ -435,9 +435,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; diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php index fd492eba78d..c6e79105081 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchAnalyzer.php @@ -171,7 +171,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; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php index d8802a1cd47..3e97c915ba0 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php @@ -591,7 +591,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( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php index bfee36f3ac7..d40ce1ef028 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php @@ -549,7 +549,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; @@ -583,7 +583,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; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php index a2eb2c60f0a..7652589699b 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php @@ -739,7 +739,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); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index 80a32d1b20b..9f9814669fc 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -1028,7 +1028,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); } @@ -1043,7 +1043,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); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php index 5128a652fc6..037f426ec20 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/ArithmeticOpAnalyzer.php @@ -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; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index 84b777c7e20..c3a937bf081 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -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]; diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php index 1404cb97a45..a585baf22f3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php @@ -1207,7 +1207,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( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php index 6374906a477..ac4bb4729b2 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php @@ -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++; } } @@ -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); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 6a7ffd07794..09a30c4262b 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -1108,10 +1108,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(); @@ -1243,12 +1242,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]; @@ -1369,7 +1368,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 diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php index 35fb44b1acd..775c86ceaf9 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/InstancePropertyFetchAnalyzer.php @@ -120,7 +120,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, diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php index 5a1bb9975ed..0db311700b3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/SimpleTypeInferer.php @@ -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; @@ -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; diff --git a/src/Psalm/Internal/Codebase/ConstantTypeResolver.php b/src/Psalm/Internal/Codebase/ConstantTypeResolver.php index 093eacdb895..dfc1a7f0d4e 100644 --- a/src/Psalm/Internal/Codebase/ConstantTypeResolver.php +++ b/src/Psalm/Internal/Codebase/ConstantTypeResolver.php @@ -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; } diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php index 6cc324213dd..fa11cf9f74d 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayFilterReturnTypeProvider.php @@ -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(); } ); diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php index 578560b635c..1e038602bc9 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php @@ -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, @@ -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; } diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPointerAdjustmentReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPointerAdjustmentReturnTypeProvider.php index 66d39525603..d0563dbaaca 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPointerAdjustmentReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPointerAdjustmentReturnTypeProvider.php @@ -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); diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPopReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPopReturnTypeProvider.php index 25dd2bd2ccc..594918e2b37 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPopReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayPopReturnTypeProvider.php @@ -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(); } diff --git a/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php b/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php index e2857d52bab..a090faee460 100644 --- a/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php +++ b/src/Psalm/Internal/Type/Comparator/ArrayTypeComparator.php @@ -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; } @@ -225,7 +225,7 @@ function ($i) { continue; } - if ($input_param->isEmpty() + if ($input_param->isNever() && $container_type_part instanceof TNonEmptyArray ) { return false; @@ -233,7 +233,7 @@ function ($i) { $param_comparison_result = new TypeComparisonResult(); - if (!$input_param->isEmpty()) { + if (!$input_param->isNever()) { if (!UnionTypeComparator::isContainedBy( $codebase, $input_param, diff --git a/src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php b/src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php index 1ca68ee6a9e..9b10148edb9 100644 --- a/src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php +++ b/src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php @@ -471,7 +471,7 @@ public static function isContainedBy( $array_comparison_result = new TypeComparisonResult(); - if (!$input_param->isEmpty()) { + if (!$input_param->isNever()) { if (!UnionTypeComparator::isContainedBy( $codebase, $input_param, diff --git a/src/Psalm/Internal/Type/Comparator/GenericTypeComparator.php b/src/Psalm/Internal/Type/Comparator/GenericTypeComparator.php index dd7e648c3fd..c63f56dd8c2 100644 --- a/src/Psalm/Internal/Type/Comparator/GenericTypeComparator.php +++ b/src/Psalm/Internal/Type/Comparator/GenericTypeComparator.php @@ -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; diff --git a/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php b/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php index bb42838b7e8..7c372e438b8 100644 --- a/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php +++ b/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php @@ -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, @@ -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, diff --git a/src/Psalm/Internal/Type/SimpleAssertionReconciler.php b/src/Psalm/Internal/Type/SimpleAssertionReconciler.php index a116ddb5253..c943f623581 100644 --- a/src/Psalm/Internal/Type/SimpleAssertionReconciler.php +++ b/src/Psalm/Internal/Type/SimpleAssertionReconciler.php @@ -513,11 +513,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)); @@ -1932,9 +1927,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; } diff --git a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php index 1dc6033b60e..af4ad25914a 100644 --- a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php @@ -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 diff --git a/src/Psalm/Internal/Type/TypeCombiner.php b/src/Psalm/Internal/Type/TypeCombiner.php index 53cb97d2a2e..330ce7b8f7e 100644 --- a/src/Psalm/Internal/Type/TypeCombiner.php +++ b/src/Psalm/Internal/Type/TypeCombiner.php @@ -548,7 +548,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; } @@ -1326,11 +1326,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) { diff --git a/src/Psalm/Internal/TypeVisitor/ContainsLiteralVisitor.php b/src/Psalm/Internal/TypeVisitor/ContainsLiteralVisitor.php index e7d3f066031..7c619489711 100644 --- a/src/Psalm/Internal/TypeVisitor/ContainsLiteralVisitor.php +++ b/src/Psalm/Internal/TypeVisitor/ContainsLiteralVisitor.php @@ -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; } diff --git a/src/Psalm/Type/Atomic/GenericTrait.php b/src/Psalm/Type/Atomic/GenericTrait.php index cbeb416155e..15d8715334e 100644 --- a/src/Psalm/Type/Atomic/GenericTrait.php +++ b/src/Psalm/Type/Atomic/GenericTrait.php @@ -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; } diff --git a/src/Psalm/Type/Atomic/TArray.php b/src/Psalm/Type/Atomic/TArray.php index 05e31a4762f..6aac66efc6d 100644 --- a/src/Psalm/Type/Atomic/TArray.php +++ b/src/Psalm/Type/Atomic/TArray.php @@ -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(); + } } diff --git a/src/Psalm/Type/Reconciler.php b/src/Psalm/Type/Reconciler.php index f8bf3c370a8..be5e2e2e146 100644 --- a/src/Psalm/Type/Reconciler.php +++ b/src/Psalm/Type/Reconciler.php @@ -249,7 +249,7 @@ public static function reconcileKeyedTypes( throw new UnexpectedValueException('$result_type should not be null'); } - if (!$did_type_exist && $result_type->isEmpty()) { + if (!$did_type_exist && $result_type->isNever()) { continue; } @@ -1018,7 +1018,7 @@ private static function adjustTKeyedArrayType( foreach ($existing_types[$base_key]->getAtomicTypes() as $base_atomic_type) { if ($base_atomic_type instanceof TKeyedArray || ($base_atomic_type instanceof TArray - && !$base_atomic_type->type_params[1]->isEmpty()) + && !$base_atomic_type->isEmptyArray()) || $base_atomic_type instanceof TList || $base_atomic_type instanceof TClassStringMap ) { @@ -1035,7 +1035,7 @@ private static function adjustTKeyedArrayType( null ); - if (!$previous_key_type->isEmpty()) { + if (!$previous_key_type->isNever()) { $base_atomic_type->previous_key_type = $previous_key_type; } $base_atomic_type->previous_value_type = $previous_value_type; diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index b64c4ff0e5c..3fdb6f01a02 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -1050,11 +1050,6 @@ public function isGenerator(): bool && ($single_type->value === 'Generator'); } - public function isEmpty(): bool - { - return $this->isNever(); - } - public function substitute(Union $old_type, ?Union $new_type = null): void { if ($this->hasMixed() && !$this->isEmptyMixed()) { diff --git a/tests/ArrayAssignmentTest.php b/tests/ArrayAssignmentTest.php index f5a2bd54026..e9104c2e1f9 100644 --- a/tests/ArrayAssignmentTest.php +++ b/tests/ArrayAssignmentTest.php @@ -159,7 +159,7 @@ class B {} $foo = []; $foo[][] = "hello";', 'assertions' => [ - '$foo' => 'non-empty-list>', + '$foo' => 'non-empty-list>', ], ], 'implicit3dIntArrayCreation' => [ @@ -167,7 +167,7 @@ class B {} $foo = []; $foo[][][] = "hello";', 'assertions' => [ - '$foo' => 'non-empty-list>>', + '$foo' => 'non-empty-list>>', ], ], 'implicit4dIntArrayCreation' => [ @@ -175,7 +175,7 @@ class B {} $foo = []; $foo[][][][] = "hello";', 'assertions' => [ - '$foo' => 'non-empty-list>>>', + '$foo' => 'non-empty-list>>>', ], ], 'implicitIndexedIntArrayCreation' => [