Skip to content

Commit

Permalink
Fix supertype checks between int range and constant int union
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen committed Sep 22, 2022
1 parent 8226c4f commit 97eb6e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Type/UnionType.php
Expand Up @@ -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;
Expand Down
Expand Up @@ -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'], []);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-3383.php
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Bug3383;

class HelloWorld
{
/** @var 0|1|2|3 */
public int $classification = 0;

public function test(): void {
$this->classification = random_int(0, 3);
}
}

0 comments on commit 97eb6e2

Please sign in to comment.