Skip to content

Commit 548f4f6

Browse files
committed
Fix spurious error nullCoalesce.offset
1 parent c87f1cb commit 548f4f6

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/Analyser/ExprHandler/Helper/NonNullabilityHelper.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ public function ensureShallowNonNullability(MutatingScope $scope, Scope $origina
3030
}
3131

3232
// keep certainty
33-
$certainty = TrinaryLogic::createYes();
34-
$hasExpressionType = $originalScope->hasExpressionType($exprToSpecify);
35-
if (!$hasExpressionType->no()) {
36-
$certainty = $hasExpressionType;
37-
}
38-
33+
$certainty = $originalScope->hasExpressionType($exprToSpecify);
3934
$exprTypeWithoutNull = TypeCombinator::removeNull($exprType);
4035
if ($exprType->equals($exprTypeWithoutNull)) {
4136
$originalExprType = $originalScope->getType($exprToSpecify);
@@ -107,6 +102,10 @@ public function ensureNonNullability(MutatingScope $scope, Expr $expr): EnsuredN
107102
public function revertNonNullability(MutatingScope $scope, array $specifiedExpressions): MutatingScope
108103
{
109104
foreach ($specifiedExpressions as $specifiedExpressionResult) {
105+
if ($specifiedExpressionResult->getCertainty()->no()) {
106+
$scope = $scope->invalidateExpression($specifiedExpressionResult->getExpression());
107+
continue;
108+
}
110109
$scope = $scope->specifyExpressionType(
111110
$specifiedExpressionResult->getExpression(),
112111
$specifiedExpressionResult->getOriginalType(),

tests/PHPStan/Analyser/nsrt/has-offset-type-bug.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function doFoo2($mixed)
105105
public function doBar(\SimpleXMLElement $xml)
106106
{
107107
if (isset($xml['foo'])) {
108-
assertType('SimpleXMLElement', $xml['foo']);
108+
assertType("SimpleXMLElement&hasOffset('foo')", $xml['foo']);
109109
assertType("SimpleXMLElement&hasOffset('foo')", $xml);
110110
}
111111
}

0 commit comments

Comments
 (0)