Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1068,15 +1068,20 @@ public function issetCheck(Expr $expr, callable $typeCallback, ?bool $result = n

if ($propertyReflection->hasNativeType() && !$propertyReflection->isVirtual()->yes()) {
if (!$this->hasExpressionType($expr)->yes()) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->issetCheckUndefined($expr->var);
}
if ($propertyReflection->getWritableType()->isNull()->no()) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->issetCheckUndefined($expr->var);
}

if ($expr->class instanceof Expr) {
return $this->issetCheckUndefined($expr->class);
if ($expr->class instanceof Expr) {
return $this->issetCheckUndefined($expr->class);
}

return null;
}

return null;
// Nullable native property: fall through to type callback
// so that isset() falsey properly narrows to null
}
}

Expand Down
58 changes: 14 additions & 44 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1917,10 +1917,7 @@ private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes
{
$conditionExpressionTypes = [];
foreach ($leftTypes->getSureTypes() as $exprString => [$expr, $type]) {
if (!$expr instanceof Expr\Variable) {
continue;
}
if (!is_string($expr->name)) {
if (!$this->canBeConditionalExpressionHolder($expr)) {
continue;
}

Expand All @@ -1933,10 +1930,7 @@ private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes
if (count($conditionExpressionTypes) > 0) {
$holders = [];
foreach ($rightTypes->getSureTypes() as $exprString => [$expr, $type]) {
if (!$expr instanceof Expr\Variable) {
continue;
}
if (!is_string($expr->name)) {
if (!$this->canBeConditionalExpressionHolder($expr)) {
continue;
}

Expand All @@ -1945,20 +1939,7 @@ private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes
}

$conditions = $conditionExpressionTypes;
foreach ($conditions as $conditionExprString => $conditionExprTypeHolder) {
$conditionExpr = $conditionExprTypeHolder->getExpr();
if (!$conditionExpr instanceof Expr\Variable) {
continue;
}
if (!is_string($conditionExpr->name)) {
continue;
}
if ($conditionExpr->name !== $expr->name) {
continue;
}

unset($conditions[$conditionExprString]);
}
unset($conditions[$exprString]);

if (count($conditions) === 0) {
continue;
Expand Down Expand Up @@ -2038,10 +2019,7 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
{
$conditionExpressionTypes = [];
foreach ($leftTypes->getSureNotTypes() as $exprString => [$expr, $type]) {
if (!$expr instanceof Expr\Variable) {
continue;
}
if (!is_string($expr->name)) {
if (!$this->canBeConditionalExpressionHolder($expr)) {
continue;
}

Expand All @@ -2054,10 +2032,7 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
if (count($conditionExpressionTypes) > 0) {
$holders = [];
foreach ($rightTypes->getSureNotTypes() as $exprString => [$expr, $type]) {
if (!$expr instanceof Expr\Variable) {
continue;
}
if (!is_string($expr->name)) {
if (!$this->canBeConditionalExpressionHolder($expr)) {
continue;
}

Expand All @@ -2066,20 +2041,7 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
}

$conditions = $conditionExpressionTypes;
foreach ($conditions as $conditionExprString => $conditionExprTypeHolder) {
$conditionExpr = $conditionExprTypeHolder->getExpr();
if (!$conditionExpr instanceof Expr\Variable) {
continue;
}
if (!is_string($conditionExpr->name)) {
continue;
}
if ($conditionExpr->name !== $expr->name) {
continue;
}

unset($conditions[$conditionExprString]);
}
unset($conditions[$exprString]);

if (count($conditions) === 0) {
continue;
Expand All @@ -2098,6 +2060,14 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
return [];
}

private function canBeConditionalExpressionHolder(Expr $expr): bool
{
return $expr instanceof Expr\Variable
|| $expr instanceof Expr\PropertyFetch
|| $expr instanceof Expr\StaticPropertyFetch
|| $expr instanceof Expr\ArrayDimFetch;
}

/**
* @return array{Expr, ConstantScalarType, Type}|null
*/
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10786.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Bug10786;

use function PHPStan\Testing\assertType;

class Value {
public ?int $value = null;
}

class HelloWorld
{
public function sayHello(Value $a, Value $b): int
{
if (is_null($a->value) && is_null($b->value)) {
throw new \Exception();
}

assertType('int', $a->value ?? $b->value);

return $a->value ?? $b->value;
}

public function sayHello2(Value $a, Value $b): int
{
if ($a->value === null && $b->value === null) {
throw new \Exception();
}

assertType('int|null', $a->value);
assertType('int|null', $b->value);
assertType('int', $a->value ?? $b->value);

return $a->value ?? $b->value;
}
}
Loading