Skip to content

Commit 61cb5aa

Browse files
committed
Fixed ConstantArrayType::accepts()
1 parent b55208e commit 61cb5aa

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/Type/Constant/ConstantArrayType.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Type\CompoundType;
1313
use PHPStan\Type\ConstantType;
1414
use PHPStan\Type\ErrorType;
15+
use PHPStan\Type\Generic\TemplateMixedType;
1516
use PHPStan\Type\Generic\TemplateTypeMap;
1617
use PHPStan\Type\Generic\TemplateTypeVariance;
1718
use PHPStan\Type\IntersectionType;
@@ -94,8 +95,12 @@ public function getValueTypes(): array
9495

9596
public function accepts(Type $type, bool $strictTypes): TrinaryLogic
9697
{
97-
if (parent::isSuperTypeOf($type)->no()) {
98-
return TrinaryLogic::createNo();
98+
if ($type instanceof MixedType && !$type instanceof TemplateMixedType) {
99+
return $type->isAcceptedBy($this, $strictTypes);
100+
}
101+
102+
if ($type instanceof self && count($this->keyTypes) === 0) {
103+
return TrinaryLogic::createFromBoolean(count($type->keyTypes) === 0);
99104
}
100105

101106
$result = TrinaryLogic::createYes();
@@ -115,7 +120,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
115120
$result = $result->and($acceptsValue);
116121
}
117122

118-
return $result;
123+
return $result->and($type->isArray());
119124
}
120125

121126
public function isSuperTypeOf(Type $type): TrinaryLogic

tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php

+63-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Type\MixedType;
1414
use PHPStan\Type\StringType;
1515
use PHPStan\Type\Type;
16+
use PHPStan\Type\TypeCombinator;
1617
use PHPStan\Type\VerbosityLevel;
1718

1819
class ConstantArrayTypeTest extends \PHPStan\Testing\TestCase
@@ -109,15 +110,75 @@ public function dataAccepts(): iterable
109110
new ConstantArrayType([new ConstantStringType('foo')], [new ConstantStringType('bar')]),
110111
TrinaryLogic::createYes(),
111112
];
113+
114+
yield [
115+
TypeCombinator::union(
116+
new ConstantArrayType([
117+
new ConstantStringType('name'),
118+
], [
119+
new StringType(),
120+
]),
121+
new ConstantArrayType([
122+
new ConstantStringType('name'),
123+
new ConstantStringType('color'),
124+
], [
125+
new StringType(),
126+
new StringType(),
127+
])
128+
),
129+
new ConstantArrayType([
130+
new ConstantStringType('name'),
131+
new ConstantStringType('color'),
132+
new ConstantStringType('year'),
133+
], [
134+
new StringType(),
135+
new StringType(),
136+
new IntegerType(),
137+
]),
138+
TrinaryLogic::createYes(),
139+
];
140+
141+
yield [
142+
new ConstantArrayType([
143+
new ConstantStringType('name'),
144+
new ConstantStringType('color'),
145+
new ConstantStringType('year'),
146+
], [
147+
new StringType(),
148+
new StringType(),
149+
new IntegerType(),
150+
]),
151+
new MixedType(),
152+
TrinaryLogic::createYes(),
153+
];
154+
155+
yield [
156+
TypeCombinator::union(
157+
new ConstantArrayType([], []),
158+
new ConstantArrayType([
159+
new ConstantStringType('name'),
160+
new ConstantStringType('color'),
161+
], [
162+
new StringType(),
163+
new StringType(),
164+
])
165+
),
166+
new ConstantArrayType([
167+
new ConstantStringType('surname'),
168+
], [
169+
new StringType(),
170+
]),
171+
TrinaryLogic::createNo(),
172+
];
112173
}
113174

114175
/**
115176
* @dataProvider dataAccepts
116-
* @param ConstantArrayType $type
177+
* @param Type $type
117178
* @param Type $otherType
118179
* @param TrinaryLogic $expectedResult
119180
*/
120-
public function testAccepts(ConstantArrayType $type, Type $otherType, TrinaryLogic $expectedResult): void
181+
public function testAccepts(Type $type, Type $otherType, TrinaryLogic $expectedResult): void
121182
{
122183
$actualResult = $type->accepts($otherType, true);
123184
$this->assertSame(

0 commit comments

Comments
 (0)