Skip to content

Commit

Permalink
Fixed ConstantArrayType::isSuperTypeOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 28, 2020
1 parent d67dae3 commit 1753f2f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ 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();
}

Expand All @@ -244,7 +247,12 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
foreach ($this->keyTypes as $i => $keyType) {
$hasOffset = $type->hasOffsetValueType($keyType);
if ($hasOffset->no()) {
return TrinaryLogic::createNo();
if (!$this->isOptionalKey($i)) {
return TrinaryLogic::createNo();
}

$results[] = TrinaryLogic::createMaybe();
continue;
}
$results[] = $this->valueTypes[$i]->isSuperTypeOf($type->getOffsetValueType($keyType));
}
Expand Down
48 changes: 48 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,54 @@ public function dataIsSuperTypeOf(): iterable
]),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2),
new ConstantArrayType([], []),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0]),
new ConstantArrayType([], []),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0, 1]),
new ConstantArrayType([], []),
TrinaryLogic::createMaybe(),
];

yield [
new ConstantArrayType([], []),
new ConstantArrayType([
new ConstantStringType('foo'),
new ConstantStringType('bar'),
], [
new IntegerType(),
new IntegerType(),
], 2, [0, 1]),
TrinaryLogic::createMaybe(),
];
}

/**
Expand Down

0 comments on commit 1753f2f

Please sign in to comment.