Skip to content

Commit 40d765c

Browse files
phpstan-botclaude
authored andcommitted
Use $varType->isNull()->maybe() instead of TypeCombinator::containsNull()
Address review feedback: cache $varType->isNull() in a variable to avoid computing it twice, and use ->maybe() instead of TypeCombinator::containsNull() for the nullable check. Update throw-points test: doesntThrow() returns mixed which can be null, so ?-> may short-circuit and variables assigned in the method name/arguments should be "maybe defined", not "yes defined". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9b0b84a commit 40d765c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Analyser/ExprHandler/NullsafeMethodCallHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
8181
);
8282
$scope = $this->nonNullabilityHelper->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions());
8383

84-
if ($varType->isNull()->yes()) {
84+
$varIsNull = $varType->isNull();
85+
if ($varIsNull->yes()) {
8586
// Arguments are never evaluated when the var is always null.
8687
$scope = $scopeBeforeNullsafe;
87-
} elseif (TypeCombinator::containsNull($varType)) {
88+
} elseif ($varIsNull->maybe()) {
8889
// Arguments might not be evaluated (short-circuit).
8990
// Merge with the original scope so variables assigned in arguments become "maybe defined".
9091
$scope = $scope->mergeWith($scopeBeforeNullsafe);

tests/PHPStan/Analyser/nsrt/throw-points/php8/null-safe-method-call.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ function () {
4242
try {
4343
doesntThrow()?->{$foo = 1}($bar = 2);
4444
} finally {
45-
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
46-
assertVariableCertainty(TrinaryLogic::createYes(), $bar);
45+
// doesntThrow() returns mixed which can be null, so ?-> may short-circuit
46+
assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);
47+
assertVariableCertainty(TrinaryLogic::createMaybe(), $bar);
4748
}
4849
};
4950

0 commit comments

Comments
 (0)