Skip to content

Commit

Permalink
Fix ConstantArrayType::isSuperTypeOf() for empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 8, 2024
1 parent 6321600 commit ed6bc0b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}

/**
Expand Down

0 comments on commit ed6bc0b

Please sign in to comment.