Skip to content

Fix spurious error nullCoalesce.offset#5145

Merged
staabm merged 2 commits intophpstan:2.1.xfrom
staabm:bug13921
Mar 9, 2026
Merged

Fix spurious error nullCoalesce.offset#5145
staabm merged 2 commits intophpstan:2.1.xfrom
staabm:bug13921

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Mar 8, 2026

before this PR we only remembered maybe/yes expr certainty when restoring expressions.
this lead to that previously not-existing expressions (no-certainy) where restored into a yes-certainty instead of the state before.

closes phpstan/phpstan#13921
refs phpstan/phpstan#10305 (comment) (fixes a issue mentioned in comments but not the issue itself)

@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 2.2.x. PHPStan 2.2 is not going to be released for months. If your code is relevant on 2.1.x and you want it to be released sooner, please rebase your pull request and change its target to 2.1.x.

@staabm staabm changed the base branch from 2.2.x to 2.1.x March 8, 2026 08:22
@staabm staabm marked this pull request as ready for review March 8, 2026 09:21
@staabm staabm requested a review from VincentLanglet March 8, 2026 09:21
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm marked this pull request as draft March 9, 2026 06:15
@staabm staabm force-pushed the bug13921 branch 2 times, most recently from fb95750 to 3b180d2 Compare March 9, 2026 06:54
@staabm staabm marked this pull request as ready for review March 9, 2026 09:05
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Contributor Author

staabm commented Mar 9, 2026

remaining errors also happen on 2.1.x - so seem unrelated

@staabm staabm requested a review from VincentLanglet March 9, 2026 09:05

return new EnsuredNonNullabilityResult($scope, [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $certainty),
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $hasExpressionType),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need $certainty var ?

Copy link
Contributor Author

@staabm staabm Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - its used a few lines below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could maybe move the block

// keep certainty
		$certainty = TrinaryLogic::createYes();
		$hasExpressionType = $originalScope->hasExpressionType($exprToSpecify);
		if (!$hasExpressionType->no()) {
			$certainty = $hasExpressionType;
		}

where it's used in order to avoid those check when they are not needed, but it doesn't cost a lot i think.

$scope->getType($parentExpr),
$scope->getNativeType($parentExpr),
$parentCertainty,
$hasParentExpressionType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need $parentCertainty var ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inlined


return new EnsuredNonNullabilityResult($scope, [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $certainty),
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType, $hasExpressionType),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could maybe move the block

// keep certainty
		$certainty = TrinaryLogic::createYes();
		$hasExpressionType = $originalScope->hasExpressionType($exprToSpecify);
		if (!$hasExpressionType->no()) {
			$certainty = $hasExpressionType;
		}

where it's used in order to avoid those check when they are not needed, but it doesn't cost a lot i think.

@staabm staabm merged commit d3e3da3 into phpstan:2.1.x Mar 9, 2026
630 of 648 checks passed
@staabm staabm deleted the bug13921 branch March 9, 2026 10:03
@staabm
Copy link
Contributor Author

staabm commented Mar 9, 2026

We could maybe move the block

we need $hasExpressionType and $certainty in 2 independent branches. and $certainty depends on $hasExpressionType.

I think its already as easy as it can get

thank you.

@staabm
Copy link
Contributor Author

staabm commented Mar 9, 2026

aahh I finally understood what you mean :-)

#5158

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spurious error nullCoalesce.offset

3 participants