Skip to content

Commit 1753f2f

Browse files
committed
Fixed ConstantArrayType::isSuperTypeOf()
1 parent d67dae3 commit 1753f2f

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Type/Constant/ConstantArrayType.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
234234
if ($type instanceof self) {
235235
if (count($this->keyTypes) === 0) {
236236
if (count($type->keyTypes) > 0) {
237+
if (count($type->optionalKeys) > 0) {
238+
return TrinaryLogic::createMaybe();
239+
}
237240
return TrinaryLogic::createNo();
238241
}
239242

@@ -244,7 +247,12 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
244247
foreach ($this->keyTypes as $i => $keyType) {
245248
$hasOffset = $type->hasOffsetValueType($keyType);
246249
if ($hasOffset->no()) {
247-
return TrinaryLogic::createNo();
250+
if (!$this->isOptionalKey($i)) {
251+
return TrinaryLogic::createNo();
252+
}
253+
254+
$results[] = TrinaryLogic::createMaybe();
255+
continue;
248256
}
249257
$results[] = $this->valueTypes[$i]->isSuperTypeOf($type->getOffsetValueType($keyType));
250258
}

tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,54 @@ public function dataIsSuperTypeOf(): iterable
449449
]),
450450
TrinaryLogic::createNo(),
451451
];
452+
453+
yield [
454+
new ConstantArrayType([
455+
new ConstantStringType('foo'),
456+
new ConstantStringType('bar'),
457+
], [
458+
new IntegerType(),
459+
new IntegerType(),
460+
], 2),
461+
new ConstantArrayType([], []),
462+
TrinaryLogic::createNo(),
463+
];
464+
465+
yield [
466+
new ConstantArrayType([
467+
new ConstantStringType('foo'),
468+
new ConstantStringType('bar'),
469+
], [
470+
new IntegerType(),
471+
new IntegerType(),
472+
], 2, [0]),
473+
new ConstantArrayType([], []),
474+
TrinaryLogic::createNo(),
475+
];
476+
477+
yield [
478+
new ConstantArrayType([
479+
new ConstantStringType('foo'),
480+
new ConstantStringType('bar'),
481+
], [
482+
new IntegerType(),
483+
new IntegerType(),
484+
], 2, [0, 1]),
485+
new ConstantArrayType([], []),
486+
TrinaryLogic::createMaybe(),
487+
];
488+
489+
yield [
490+
new ConstantArrayType([], []),
491+
new ConstantArrayType([
492+
new ConstantStringType('foo'),
493+
new ConstantStringType('bar'),
494+
], [
495+
new IntegerType(),
496+
new IntegerType(),
497+
], 2, [0, 1]),
498+
TrinaryLogic::createMaybe(),
499+
];
452500
}
453501

454502
/**

0 commit comments

Comments
 (0)