Skip to content

Commit 8376548

Browse files
cs278ondrejmirtes
authored andcommitted
Fix mixed with subtraction not being a super type of never
1 parent d31eb7a commit 8376548

6 files changed

Lines changed: 66 additions & 3 deletions

File tree

src/Type/ArrayType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public function equals(Type $type): bool
103103

104104
public function describe(VerbosityLevel $level): string
105105
{
106-
$isMixedKeyType = $this->keyType instanceof MixedType && !$this->keyType instanceof TemplateType;
107-
$isMixedItemType = $this->itemType instanceof MixedType && !$this->itemType instanceof TemplateType;
106+
$isMixedKeyType = $this->keyType instanceof MixedType && $this->keyType->describe(VerbosityLevel::precise()) === 'mixed';
107+
$isMixedItemType = $this->itemType instanceof MixedType && $this->itemType->describe(VerbosityLevel::precise()) === 'mixed';
108108

109109
$valueHandler = function () use ($level, $isMixedKeyType, $isMixedItemType): string {
110110
if ($isMixedKeyType || $this->keyType instanceof NeverType) {

src/Type/MixedType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function isSuperTypeOfMixed(MixedType $type): TrinaryLogic
8888

8989
public function isSuperTypeOf(Type $type): TrinaryLogic
9090
{
91-
if ($this->subtractedType === null) {
91+
if ($this->subtractedType === null || $type instanceof NeverType) {
9292
return TrinaryLogic::createYes();
9393
}
9494

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9757,6 +9757,11 @@ public function dataBug2835(): array
97579757
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2835.php');
97589758
}
97599759

9760+
public function dataBug2443(): array
9761+
{
9762+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2443.php');
9763+
}
9764+
97609765
/**
97619766
* @dataProvider dataBug2574
97629767
* @dataProvider dataBug2577
@@ -9781,6 +9786,7 @@ public function dataBug2835(): array
97819786
* @dataProvider dataListType
97829787
* @dataProvider dataBug2822
97839788
* @dataProvider dataBug2835
9789+
* @dataProvider dataBug2443
97849790
* @param ConstantStringType $expectedType
97859791
* @param Type $actualType
97869792
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Analyser\Bug2443;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
/**
8+
* @param array<int,mixed> $a
9+
*/
10+
function (array $a): void
11+
{
12+
assertType('bool', array_filter($a) !== []);
13+
assertType('bool', [] !== array_filter($a));
14+
15+
assertType('bool', array_filter($a) === []);
16+
assertType('bool', [] === array_filter($a));
17+
};

tests/PHPStan/Type/ArrayTypeTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use PHPStan\TrinaryLogic;
66
use PHPStan\Type\Constant\ConstantArrayType;
7+
use PHPStan\Type\Constant\ConstantBooleanType;
8+
use PHPStan\Type\Constant\ConstantFloatType;
79
use PHPStan\Type\Constant\ConstantIntegerType;
10+
use PHPStan\Type\Constant\ConstantStringType;
811
use PHPStan\Type\Generic\TemplateTypeFactory;
912
use PHPStan\Type\Generic\TemplateTypeScope;
1013
use PHPStan\Type\Generic\TemplateTypeVariance;
@@ -50,6 +53,23 @@ public function dataIsSuperTypeOf(): array
5053
new ConstantArrayType([], []),
5154
TrinaryLogic::createYes(),
5255
],
56+
[
57+
new ArrayType(new MixedType(), new MixedType(false, new UnionType([
58+
new NullType(),
59+
new ConstantBooleanType(false),
60+
new ConstantIntegerType(0),
61+
new ConstantFloatType(0.0),
62+
new ConstantStringType(''),
63+
new ConstantArrayType([], []),
64+
]))),
65+
new ConstantArrayType([], []),
66+
TrinaryLogic::createYes(),
67+
],
68+
[
69+
new ArrayType(new MixedType(), new MixedType(false, new NullType())),
70+
new ConstantArrayType([], []),
71+
TrinaryLogic::createYes(),
72+
],
5373
];
5474
}
5575

tests/PHPStan/Type/MixedTypeTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ public function dataIsSuperTypeOf(): array
121121
new MixedType(false, new ObjectType('stdClass')),
122122
TrinaryLogic::createMaybe(),
123123
],
124+
[
125+
new MixedType(),
126+
new NeverType(),
127+
TrinaryLogic::createYes(),
128+
],
129+
[
130+
new MixedType(false, new NullType()),
131+
new NeverType(),
132+
TrinaryLogic::createYes(),
133+
],
134+
[
135+
new MixedType(),
136+
new UnionType([new StringType(), new IntegerType()]),
137+
TrinaryLogic::createYes(),
138+
],
139+
[
140+
new MixedType(false, new NullType()),
141+
new UnionType([new StringType(), new IntegerType()]),
142+
TrinaryLogic::createYes(),
143+
],
124144
];
125145
}
126146

0 commit comments

Comments
 (0)