From c55c36a39dc7ff9eb1c93d4a90f53c84889d268d Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Mon, 25 Jul 2022 16:37:21 +0200 Subject: [PATCH] Fix incorrect mixed template type for impossible union template type intersection --- src/Type/TypeCombinator.php | 4 ++ .../Analyser/NodeScopeResolverTest.php | 1 + tests/PHPStan/Analyser/data/bug-7688.php | 50 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 tests/PHPStan/Analyser/data/bug-7688.php diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 8244bf601f..6cf51bc2f4 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -678,6 +678,10 @@ public static function intersect(Type ...$types): Type } $union = self::union(...$topLevelUnionSubTypes); + if ($union instanceof NeverType) { + return $union; + } + if ($type instanceof BenevolentUnionType) { $union = TypeUtils::toBenevolentUnion($union); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index e3bbf904f2..b28fd409ea 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -943,6 +943,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7663-php8.php'); } yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7663.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7688.php'); } /** diff --git a/tests/PHPStan/Analyser/data/bug-7688.php b/tests/PHPStan/Analyser/data/bug-7688.php new file mode 100644 index 0000000000..cc0f7818a8 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-7688.php @@ -0,0 +1,50 @@ + + */ +function baz($value) +{ + if (is_int($value)) { + assertType('int', $value); + return $value < 1 ? 1 : $value; + } + + return $value; +}