Skip to content

Commit b55208e

Browse files
committed
Fixed ConstantArrayType::isSuperTypeOf()
1 parent 56eadce commit b55208e

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

src/Type/Constant/ConstantArrayType.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,21 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
121121
public function isSuperTypeOf(Type $type): TrinaryLogic
122122
{
123123
if ($type instanceof self) {
124-
if (count($this->keyTypes) !== count($type->keyTypes)) {
125-
return TrinaryLogic::createNo();
124+
if (count($this->keyTypes) === 0) {
125+
if (count($type->keyTypes) > 0) {
126+
return TrinaryLogic::createNo();
127+
}
128+
129+
return TrinaryLogic::createYes();
126130
}
127131

128132
$results = [];
129-
foreach (array_keys($this->keyTypes) as $i) {
130-
$results[] = $this->keyTypes[$i]->isSuperTypeOf($type->keyTypes[$i]);
131-
$results[] = $this->valueTypes[$i]->isSuperTypeOf($type->valueTypes[$i]);
133+
foreach ($this->keyTypes as $i => $keyType) {
134+
$hasOffset = $type->hasOffsetValueType($keyType);
135+
if ($hasOffset->no()) {
136+
return TrinaryLogic::createNo();
137+
}
138+
$results[] = $this->valueTypes[$i]->isSuperTypeOf($type->getOffsetValueType($keyType));
132139
}
133140

134141
return TrinaryLogic::createYes()->and(...$results);

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ public function testStrictComparison(): void
118118
'Strict comparison using === between array(\'X\' => 1, \'Y\' => 2) and array(\'X\' => 2, \'Y\' => 1) will always evaluate to false.',
119119
300,
120120
],
121-
[
122-
'Strict comparison using === between array(\'X\' => 1, \'Y\' => 2) and array(\'Y\' => 2, \'X\' => 1) will always evaluate to false.',
123-
308,
124-
],
125121
[
126122
'Strict comparison using === between \'/\'|\'\\\\\' and \'//\' will always evaluate to false.',
127123
320,
@@ -300,10 +296,6 @@ public function testStrictComparisonWithoutAlwaysTrue(): void
300296
'Strict comparison using === between array(\'X\' => 1, \'Y\' => 2) and array(\'X\' => 2, \'Y\' => 1) will always evaluate to false.',
301297
300,
302298
],
303-
[
304-
'Strict comparison using === between array(\'X\' => 1, \'Y\' => 2) and array(\'Y\' => 2, \'X\' => 1) will always evaluate to false.',
305-
308,
306-
],
307299
[
308300
'Strict comparison using === between \'/\'|\'\\\\\' and \'//\' will always evaluate to false.',
309301
320,

tests/PHPStan/Rules/Methods/data/method-signature.php

+24
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,27 @@ public function processNode(\PhpParser\Node $node): void
457457
}
458458

459459
}
460+
461+
interface ConstantArrayInterface
462+
{
463+
464+
/**
465+
* @return array{foo: string}
466+
*/
467+
public function foobar(): array;
468+
469+
}
470+
471+
class ConstantArrayClass implements ConstantArrayInterface
472+
{
473+
/**
474+
* @return array{foo: string, bar: string}
475+
*/
476+
public function foobar(): array
477+
{
478+
return [
479+
'foo' => '',
480+
'bar' => '',
481+
];
482+
}
483+
}

tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,38 @@ public function dataIsSuperTypeOf(): iterable
188188
new IterableType(new MixedType(false), new MixedType(true)),
189189
TrinaryLogic::createMaybe(),
190190
];
191+
192+
yield [
193+
new ConstantArrayType([
194+
new ConstantStringType('foo'),
195+
], [
196+
new IntegerType(),
197+
]),
198+
new ConstantArrayType([
199+
new ConstantStringType('foo'),
200+
new ConstantStringType('bar'),
201+
], [
202+
new IntegerType(),
203+
new IntegerType(),
204+
]),
205+
TrinaryLogic::createYes(),
206+
];
207+
208+
yield [
209+
new ConstantArrayType([
210+
new ConstantStringType('foo'),
211+
new ConstantStringType('bar'),
212+
], [
213+
new IntegerType(),
214+
new IntegerType(),
215+
]),
216+
new ConstantArrayType([
217+
new ConstantStringType('foo'),
218+
], [
219+
new IntegerType(),
220+
]),
221+
TrinaryLogic::createNo(),
222+
];
191223
}
192224

193225
/**

0 commit comments

Comments
 (0)