Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Nov 30, 2025

No description provided.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

One more thing - these expressions (Bool_, String_, Int_, Double) have some kind of logic in LegacyTypeSpecifier:

		} elseif ($expr instanceof Expr\Cast\Bool_) {
			return $this->specifyTypesInCondition(
				$scope,
				new Node\Expr\BinaryOp\Equal($expr->expr, new ConstFetch(new Name\FullyQualified('true'))),
				$context,
			)->setRootExpr($expr);
		} elseif ($expr instanceof Expr\Cast\String_) {
			return $this->specifyTypesInCondition(
				$scope,
				new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\String_('')),
				$context,
			)->setRootExpr($expr);
		} elseif ($expr instanceof Expr\Cast\Int_) {
			return $this->specifyTypesInCondition(
				$scope,
				new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\LNumber(0)),
				$context,
			)->setRootExpr($expr);
		} elseif ($expr instanceof Expr\Cast\Double) {
			return $this->specifyTypesInCondition(
				$scope,
				new Node\Expr\BinaryOp\NotEqual($expr->expr, new Node\Scalar\DNumber(0.0)),
				$context,
			)->setRootExpr($expr);

They influence this logic and similar: https://phpstan.org/r/7dbcb4be-6c01-4bf2-b312-450327d887b6 (try it with mixed instead of int too)

GNSR handlers work a bit differently - ExprAnalysisResult carries $specifiedTruthyTypes / $specifiedFalseyTypes / $specifiedNullTypes. If I take CastBoolHandler, it should do:

$specifiedExprResult = yield new ExprAnalysisRequest($stmt, new Node\Expr\BinaryOp\Equal($expr->expr, new ConstFetch(new Name\FullyQualified('true'))), ...)

The alternativeNodeCallback of this ExprAnalysisRequest should be new NoopNodeCallback()!

And then return specifiedTruthyTypes: $specifiedExprResult->specifiedTruthyTypes in its own ExprAnalysisResult.

I didn't verify this idea but I think this is how it should work.

@ondrejmirtes
Copy link
Member

Oops, I realized this will force you to implement EqualHandler and NotEqualHandler as well 😂

@staabm
Copy link
Contributor Author

staabm commented Nov 30, 2025

ok, will try to implement EqualHandler and NotEqualHandler in a separate PR, when I am back at a desk with more screens :)

@ondrejmirtes ondrejmirtes merged commit d91b0f2 into phpstan:2.1.x Nov 30, 2025
608 of 638 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

2 participants