From 64996f464e6e992bd5de06c1ba155520708d5508 Mon Sep 17 00:00:00 2001 From: Brown Date: Wed, 6 Nov 2019 16:59:08 -0500 Subject: [PATCH] Prevent empty array workaround to #2308 --- src/Psalm/Internal/Analyzer/TypeAnalyzer.php | 5 ++++- tests/Template/ClassTemplateExtendsTest.php | 18 +++++++++++++++--- tests/Template/ClassTemplateTest.php | 19 ++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php index d88f9f77b..50f562f05 100644 --- a/src/Psalm/Internal/Analyzer/TypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/TypeAnalyzer.php @@ -814,7 +814,10 @@ class TypeAnalyzer $allow_float_int_equality, $atomic_comparison_result )) { - if ($allow_interface_equality || $input_type_part instanceof TArray) { + if ($allow_interface_equality + || ($input_type_part instanceof TArray + && !$input_type_part->type_params[1]->isEmpty()) + ) { return true; } } diff --git a/tests/Template/ClassTemplateExtendsTest.php b/tests/Template/ClassTemplateExtendsTest.php index 62bbb596b..b6ee7a298 100644 --- a/tests/Template/ClassTemplateExtendsTest.php +++ b/tests/Template/ClassTemplateExtendsTest.php @@ -677,9 +677,21 @@ class ClassTemplateExtendsTest extends TestCase class Collection3 extends Collection2{} - $a = new Collection1(["a" => "b"]); - $a = new Collection2(["a" => "b"]); - $a = new Collection3(["a" => "b"]);', + foreach ((new Collection1(["a" => "b"])) as $a) {} + + /** @psalm-suppress MixedAssignment */ + foreach ((new Collection2(["a" => "b"])) as $a) {} + + /** @psalm-suppress MixedAssignment */ + foreach ((new Collection3(["a" => "b"])) as $a) {} + + foreach ((new Collection1([])) as $i) {} + + /** @psalm-suppress MixedAssignment */ + foreach ((new Collection2([])) as $i) {} + + /** @psalm-suppress MixedAssignment */ + foreach ((new Collection3([])) as $i) {}', ], 'iterateOverExtendedArrayObjectWithoutParam' => [ ' 'MixedArgumentTypeCoercion' ], - 'preventUseWithMoreSpecificParam' => [ + 'preventUseWithMoreSpecificParamInt' => [ ' 'InvalidArgument' ], + 'preventUseWithMoreSpecificParamEmptyArray' => [ + ' $col + */ + function usesCollection(Collection $col): void { + $col->add([]); + }', + 'error_message' => 'InvalidArgument' + ], ]; } }