From ed6bc0b93499b5c7463ba05aefb1377b9eb9d1e0 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 8 Aug 2024 09:46:11 +0200 Subject: [PATCH] Fix ConstantArrayType::isSuperTypeOf() for empty array --- src/Type/Constant/ConstantArrayType.php | 9 +--- tests/PHPStan/Type/TypeCombinatorTest.php | 50 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 7bd109897e..9afb19daf5 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -364,14 +364,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic { if ($type instanceof self) { if (count($this->keyTypes) === 0) { - if (count($type->keyTypes) > 0) { - if (count($type->optionalKeys) > 0) { - return TrinaryLogic::createMaybe(); - } - return TrinaryLogic::createNo(); - } - - return TrinaryLogic::createYes(); + return $type->isIterableAtLeastOnce()->negate(); } $results = []; diff --git a/tests/PHPStan/Type/TypeCombinatorTest.php b/tests/PHPStan/Type/TypeCombinatorTest.php index b50517c033..fc9cf59e5f 100644 --- a/tests/PHPStan/Type/TypeCombinatorTest.php +++ b/tests/PHPStan/Type/TypeCombinatorTest.php @@ -4252,6 +4252,56 @@ public function dataIntersect(): iterable ClosureType::class, 'pure-Closure', ]; + + $xy = new ConstantArrayType([ + new ConstantIntegerType(0), + ], [ + new ConstantStringType('xy'), + ]); + $abxy = new ConstantArrayType([ + new ConstantIntegerType(0), + new ConstantIntegerType(1), + ], [ + new ConstantStringType('ab'), + new ConstantStringType('xy'), + ], [2], [1]); + + yield [ + [ + new UnionType([ + new ConstantArrayType([], []), + $xy, + $abxy, + ]), + new UnionType([ + $xy, + $abxy, + ]), + ], + UnionType::class, + "array{'xy'}|array{0: 'ab', 1?: 'xy'}", + ]; + + yield [ + [ + new ConstantArrayType([], []), + new UnionType([ + $xy, + $abxy, + ]), + ], + NeverType::class, + '*NEVER*=implicit', + ]; + + yield [ + [ + new ConstantArrayType([], []), + $abxy, + ], + NeverType::class, + '*NEVER*=implicit', + ]; } /**