Skip ClosureToArrowFunctionRector when closure has @var docblock#7885
Merged
TomasVotruba merged 1 commit intorectorphp:mainfrom Feb 8, 2026
Merged
Conversation
Arrow functions do not support inline @var annotations for type narrowing. Previously, the rule only skipped conversion when the @var type was more specific than the native type. However, type comparison can fail with generics (e.g. Builder<Team> vs Builder), causing unintended conversion that breaks PHPStan type narrowing. Now the rule skips conversion whenever a @var tag is present on the return statement, regardless of type comparison. Fixes rectorphp/rector#9637
Member
|
Thanks for detailed analysis. I agree this reduction to arrow function would remove important metadata and context, that is clearly intended to keep. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes rectorphp/rector#9637
Arrow functions do not support inline
@varannotations for type narrowing. When a closure contains a@vardocblock (e.g./** @var Builder<Team> $query */), converting it to an arrow function breaks PHPStan type narrowing since there is no way to apply@vartype annotations within arrow functions.Problem
Previously, the rule only skipped conversion when the
@vartype was more specific than the native type (via type comparison). However, this comparison could fail with generics (e.g.Builder<Team>vsBuilder), causing unintended conversions.Example from the issue:
Solution
Skip
ClosureToArrowFunctionRectorwhenever a@vartag is present on the return statement, regardless of type comparison. This is the safe approach because:@vardocblock, they did so intentionally@varannotations for type narrowingChanges
shouldSkipMoreSpecificTypeWithVarDoc()to skip on any@varpresenceNodeTypeResolverandMixedTypedependencies@vardocblock scenariowith_equal_var_doc_typefixture to be a skip test (equal types should also be preserved)