Skip to content

Commit

Permalink
Fix ConstantArrayType::isSuperTypeOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 5, 2024
1 parent bc81fe9 commit 0210d45
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
return TrinaryLogic::createNo();
}

return $result->and($isKeySuperType, $this->getIterableValueType()->isSuperTypeOf($type->getIterableKeyType()));
return $result->and($isKeySuperType, $this->getIterableValueType()->isSuperTypeOf($type->getIterableValueType()));
}

if ($type instanceof CompoundType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ public function testRule(): void
'PHPDoc tag @var above a function has no effect.',
313,
],
[
"PHPDoc tag @var with type array<int, mixed> is not subtype of native type array{: 'empty', 1: '1'}.",
324,
],
]);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Type/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public function dataIsSuperTypeOf(): array
new IntersectionType([new ArrayType(new IntegerType(), new StringType()), new OversizedArrayType()]),
TrinaryLogic::createYes(),
],
[
new ArrayType(new StringType(), new MixedType()),
new ConstantArrayType([
new ConstantStringType('a'),
new ConstantStringType('b'),
], [
new IntegerType(),
new UnionType([new IntegerType(), new NullType()]),
]),
TrinaryLogic::createYes(),
],
];
}

Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -654,6 +655,42 @@ public function dataIsSuperTypeOf(): iterable
], [1], [0]),
TrinaryLogic::createMaybe(),
];

yield [
new ConstantArrayType([
new ConstantStringType('a'),
new ConstantStringType('b'),
], [
new IntegerType(),
new UnionType([new IntegerType(), new NullType()]),
]),
new ArrayType(new StringType(), new MixedType()),
TrinaryLogic::createMaybe(),
];

yield [
new ConstantArrayType([
new ConstantStringType('a'),
new ConstantStringType('b'),
], [
new IntegerType(),
new UnionType([new IntegerType(), new NullType()]),
]),
new ArrayType(new StringType(), new StringType()),
TrinaryLogic::createNo(),
];

yield [
new ConstantArrayType([
new ConstantIntegerType(1),
new ConstantIntegerType(2),
], [
new IntegerType(),
new UnionType([new IntegerType(), new NullType()]),
]),
new ArrayType(new StringType(), new MixedType()),
TrinaryLogic::createNo(),
];
}

/**
Expand Down

0 comments on commit 0210d45

Please sign in to comment.