diff --git a/src/Reflection/Type/UnionTypeMethodReflection.php b/src/Reflection/Type/UnionTypeMethodReflection.php index b330b6fdad..072d950882 100644 --- a/src/Reflection/Type/UnionTypeMethodReflection.php +++ b/src/Reflection/Type/UnionTypeMethodReflection.php @@ -177,7 +177,13 @@ public function getDocComment(): ?string public function getAsserts(): Assertions { - return Assertions::createEmpty(); + $assertions = Assertions::createEmpty(); + + foreach ($this->methods as $method) { + $assertions = $assertions->intersectWith($method->getAsserts()); + } + + return $assertions; } public function acceptsNamedArguments(): TrinaryLogic diff --git a/tests/PHPStan/Analyser/nsrt/bug-11441.php b/tests/PHPStan/Analyser/nsrt/bug-11441.php new file mode 100644 index 0000000000..d1c497ec17 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-11441.php @@ -0,0 +1,39 @@ +getParam() */ + public function checkNotNull(): void { + if ($this->getParam() === null) { + throw new \Exception(); + } + } +} + +class Bar { + public function getParam(): ?int { + return 1; + } + + /** @phpstan-assert !null $this->getParam() */ + public function checkNotNull(): void { + if ($this->getParam() === null) { + throw new \Exception(); + } + } +} + +function test(Foo|Bar $obj): void { + assertType('int|string|null', $obj->getParam()); + + $obj->checkNotNull(); + + assertType('int|string', $obj->getParam()); +} diff --git a/tests/PHPStan/Analyser/nsrt/bug-13358.php b/tests/PHPStan/Analyser/nsrt/bug-13358.php new file mode 100644 index 0000000000..7c8af419e3 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-13358.php @@ -0,0 +1,40 @@ +isSystem()) { + assertType('Bug13358\SystemActor', $actor); + } else { + assertType('Bug13358\AnonymousVisitorActor', $actor); + } +}