From a8fd349e07f13e13a912149daf0b2e0b0848975b Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 28 Dec 2022 23:29:33 +0100 Subject: [PATCH 1/2] fix #7809 --- .../Type/Comparator/KeyedArrayComparator.php | 10 ++++++---- tests/FunctionCallTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php b/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php index 1607f1c48..9eba87810 100644 --- a/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php +++ b/src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php @@ -75,7 +75,7 @@ class KeyedArrayComparator $property_type_comparison = new TypeComparisonResult(); if (!$input_property_type->isNever()) { - if (!UnionTypeComparator::isContainedBy( + $is_input_containedby_container = UnionTypeComparator::isContainedBy( $codebase, $input_property_type, $container_property_type, @@ -83,9 +83,8 @@ class KeyedArrayComparator $input_property_type->ignore_falsable_issues, $property_type_comparison, $allow_interface_equality, - ) - && !$property_type_comparison->type_coerced_from_scalar - ) { + ); + if (!$is_input_containedby_container && !$property_type_comparison->type_coerced_from_scalar) { $inverse_property_type_comparison = new TypeComparisonResult(); if ($atomic_comparison_result) { @@ -111,6 +110,9 @@ class KeyedArrayComparator $all_types_contain = false; } else { + if (!$is_input_containedby_container) { + $all_types_contain = false; + } if ($atomic_comparison_result) { $atomic_comparison_result->to_string_cast = $atomic_comparison_result->to_string_cast === true diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index 71afb79e9..fe52fb49a 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -2644,6 +2644,20 @@ class FunctionCallTest extends TestCase foo();', 'error_message' => 'InvalidReturnType', ], + 'DontAcceptArrayWithShapesNotContained' => [ + 'code' => ' $mayBeInt]); + ', + ], ]; } From a29072908755c5dc7f29bc88fc12ca63cb9122c1 Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 28 Dec 2022 23:33:28 +0100 Subject: [PATCH 2/2] fix test --- tests/FunctionCallTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index fe52fb49a..434fbb2fb 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -2657,6 +2657,7 @@ class FunctionCallTest extends TestCase takesArrayShapeWithZeroOrPositiveInt(["bar" => $mayBeInt]); ', + 'error_message' => 'InvalidArgument', ], ]; }