Skip ClosureToArrowFunctionRector when closure has @var docblock #7885
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)