From 97eb6e28e42ade97d512c38847c8baa6ae7f6714 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Thu, 22 Sep 2022 15:01:42 +0200 Subject: [PATCH] Fix supertype checks between int range and constant int union --- src/Type/UnionType.php | 7 +++++++ .../TypesAssignedToPropertiesRuleTest.php | 6 ++++++ tests/PHPStan/Rules/Properties/data/bug-3383.php | 13 +++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/bug-3383.php diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index 9ae63c6cde8..7b1792ff6a0 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -137,6 +137,13 @@ public function isSuperTypeOf(Type $otherType): TrinaryLogic return $otherType->isSubTypeOf($this); } + if ($otherType instanceof IntegerRangeType) { + $remainingType = TypeCombinator::remove($otherType, $this); + if ($remainingType instanceof NeverType) { + return TrinaryLogic::createYes(); + } + } + $result = TrinaryLogic::createNo()->lazyOr($this->getTypes(), static fn (Type $innerType) => $innerType->isSuperTypeOf($otherType)); if ($result->yes()) { return $result; diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 84dc5c01643..ce66dc066e1 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -445,4 +445,10 @@ public function testBug4680(): void $this->analyse([__DIR__ . '/data/bug-4680.php'], []); } + public function testBug3383(): void + { + $this->checkExplicitMixed = true; + $this->analyse([__DIR__ . '/data/bug-3383.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Properties/data/bug-3383.php b/tests/PHPStan/Rules/Properties/data/bug-3383.php new file mode 100644 index 00000000000..68041788dbc --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-3383.php @@ -0,0 +1,13 @@ +classification = random_int(0, 3); + } +}