Skip to content

fix union/intersect involving enum case #3907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Type/Enum/EnumCaseObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\SubtractableType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult

public function subtract(Type $type): Type
{
return $this;
return $this->changeSubtractedType($type);
}

public function getTypeWithoutSubtractedType(): Type
Expand All @@ -104,7 +105,11 @@ public function getTypeWithoutSubtractedType(): Type

public function changeSubtractedType(?Type $subtractedType): Type
{
return $this;
if ($subtractedType === null || ! $this->equals($subtractedType)) {
return $this;
}

return new NeverType();
}

public function getSubtractedType(): ?Type
Expand Down
32 changes: 23 additions & 9 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,31 @@ private static function intersectWithSubtractedType(
}

$subtractedType = self::union(...$subtractedTypes);
} elseif ($b instanceof SubtractableType) {
$subtractedType = $b->getSubtractedType();
if ($subtractedType === null) {
return $a->getTypeWithoutSubtractedType();
}
} else {
$subtractedTypeTmp = self::intersect($a->getTypeWithoutSubtractedType(), $a->getSubtractedType());
if ($b->isSuperTypeOf($subtractedTypeTmp)->yes()) {
return $a->getTypeWithoutSubtractedType();
$isBAlreadySubtracted = $a->getSubtractedType()->isSuperTypeOf($b);

if ($isBAlreadySubtracted->no()) {
return $a;
} elseif ($isBAlreadySubtracted->yes()) {
$subtractedType = self::remove($a->getSubtractedType(), $b);

if ($subtractedType instanceof NeverType) {
$subtractedType = null;
}

return $a->changeSubtractedType($subtractedType);
} elseif ($b instanceof SubtractableType) {
$subtractedType = $b->getSubtractedType();
if ($subtractedType === null) {
return $a->getTypeWithoutSubtractedType();
}
} else {
$subtractedTypeTmp = self::intersect($a->getTypeWithoutSubtractedType(), $a->getSubtractedType());
if ($b->isSuperTypeOf($subtractedTypeTmp)->yes()) {
return $a->getTypeWithoutSubtractedType();
}
$subtractedType = new MixedType(false, $b);
}
$subtractedType = new MixedType(false, $b);
}

$subtractedType = self::intersect(
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8105,11 +8105,11 @@ public function dataArrayKeysInBranches(): array
'$array',
],
[
'non-empty-array&hasOffsetValue(\'key\', mixed)',
'non-empty-array&hasOffsetValue(\'key\', mixed~null)',
'$generalArray',
],
[
'mixed',
'mixed~null',
'$generalArray[\'key\']',
],
[
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/nsrt/enum-vs-in-array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace EnumVsInArray;

use function PHPStan\Testing\assertType;

enum FooEnum
{
case A;
case B;
case C;
case D;
case E;
case F;
case G;
case H;
case I;
case J;
}

function foo(FooEnum $e): int
{
if (in_array($e, [FooEnum::A, FooEnum::B, FooEnum::C], true)) {
throw new \Exception('a');
}

assertType('EnumVsInArray\FooEnum~(EnumVsInArray\FooEnum::A|EnumVsInArray\FooEnum::B|EnumVsInArray\FooEnum::C)', $e);

if (rand(0, 10) === 1) {
if (!in_array($e, [FooEnum::D, FooEnum::E], true)) {
throw new \Exception('d');
}
}

assertType('EnumVsInArray\FooEnum~(EnumVsInArray\FooEnum::A|EnumVsInArray\FooEnum::B|EnumVsInArray\FooEnum::C)', $e);

return match ($e) {
FooEnum::D, FooEnum::E, FooEnum::F, FooEnum::G, FooEnum::H, FooEnum::I => 2,
FooEnum::J => 3,
};
}
55 changes: 53 additions & 2 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ public function dataUnion(): iterable
new ObjectWithoutClassType(new ObjectType('A')),
],
MixedType::class,
'mixed=implicit',
'mixed~int=implicit',
],
[
[
Expand Down Expand Up @@ -1125,7 +1125,7 @@ public function dataUnion(): iterable
new ObjectType('InvalidArgumentException'),
],
MixedType::class,
'mixed=implicit', // should be MixedType~Exception+InvalidArgumentException
'mixed~Exception~InvalidArgumentException=implicit',
],
[
[
Expand Down Expand Up @@ -2262,6 +2262,36 @@ public function dataUnion(): iterable
'PHPStan\Fixture\ManyCasesTestEnum~PHPStan\Fixture\ManyCasesTestEnum::A',
];

yield [
[
new ObjectType('PHPStan\Fixture\ManyCasesTestEnum', new UnionType([
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A'),
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'B'),
])),
new UnionType([
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'C'),
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'D'),
]),
],
ObjectType::class,
'PHPStan\Fixture\ManyCasesTestEnum~(PHPStan\Fixture\ManyCasesTestEnum::A|PHPStan\Fixture\ManyCasesTestEnum::B)',
];

yield [
[
new ObjectType('PHPStan\Fixture\ManyCasesTestEnum', new UnionType([
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A'),
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'B'),
])),
new UnionType([
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A'),
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'D'),
]),
],
ObjectType::class,
'PHPStan\Fixture\ManyCasesTestEnum~PHPStan\Fixture\ManyCasesTestEnum::B',
];

yield [
[
new ThisType(
Expand Down Expand Up @@ -4224,6 +4254,27 @@ public function dataIntersect(): iterable
'$this(stdClass)&stdClass::foo',
];

yield [
[
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A'),
new MixedType(false, new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A')),
],
NeverType::class,
'*NEVER*=implicit',
];

yield [
[
new UnionType([
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A'),
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'B'),
]),
new MixedType(false, new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A')),
],
EnumCaseObjectType::class,
'PHPStan\Fixture\ManyCasesTestEnum::B',
];

yield [
[
TypeCombinator::intersect(new StringType(), new AccessoryNonEmptyStringType()),
Expand Down
Loading