diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index f8219658d15..8cc3329b835 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1160,7 +1160,10 @@ public function issetCheck(Expr $expr, callable $typeCallback, ?bool $result = n if ($propertyReflection->hasNativeType() && !$propertyReflection->isVirtual()->yes()) { if (!$this->hasExpressionType($expr)->yes()) { $nativeReflection = $propertyReflection->getNativeReflection(); - if ($nativeReflection === null || !$nativeReflection->isPromoted() || (!$nativeReflection->isReadOnly() && !$nativeReflection->isHooked())) { + if ( + ($nativeReflection === null || !$nativeReflection->getNativeReflection()->hasDefaultValue()) + && ($nativeReflection === null || !$nativeReflection->isPromoted() || (!$nativeReflection->isReadOnly() && !$nativeReflection->isHooked())) + ) { if ($expr instanceof Node\Expr\PropertyFetch) { return $this->issetCheckUndefined($expr->var); } diff --git a/tests/PHPStan/Analyser/nsrt/bug-10786.php b/tests/PHPStan/Analyser/nsrt/bug-10786.php new file mode 100644 index 00000000000..4dc9333aba6 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-10786.php @@ -0,0 +1,23 @@ +value) && is_null($b->value)) { + throw new \Exception(); + } + + assertType('int', $a->value ?? $b->value); + + return $a->value ?? $b->value; + } +} diff --git a/tests/PHPStan/Analyser/nsrt/isset-coalesce-empty-type.php b/tests/PHPStan/Analyser/nsrt/isset-coalesce-empty-type.php index ad81a1e6ac1..caa54683c96 100644 --- a/tests/PHPStan/Analyser/nsrt/isset-coalesce-empty-type.php +++ b/tests/PHPStan/Analyser/nsrt/isset-coalesce-empty-type.php @@ -246,7 +246,7 @@ class FooNativeProp public int $canBeUninitialized; function doFoo(FooNativeProp $foo): void { - assertType('bool', isset($foo->hasDefaultValue)); + assertType('true', isset($foo->hasDefaultValue)); $foo->isAssignedBefore = 5; assertType('true', isset($foo->isAssignedBefore)); diff --git a/tests/PHPStan/Analyser/nsrt/isset-property-default-value.php b/tests/PHPStan/Analyser/nsrt/isset-property-default-value.php new file mode 100644 index 00000000000..0fc19bb8f6e --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/isset-property-default-value.php @@ -0,0 +1,32 @@ +value === null && $b->value === null) { + throw new \LogicException(); + } + + assertType('int', $a->value ?? $b->value); +} + +function coalesceWithoutDefault(Holder $a, Holder $b): void +{ + if ($a->noDefault === null && $b->noDefault === null) { + throw new \LogicException(); + } + + assertType('int|null', $a->noDefault ?? $b->noDefault); +}