[CodeQuality] Skip NarrowUnusedSetUpDefinedPropertyRector for property captured by lazy/self-referencing arrow function#694
Merged
Conversation
…y captured by lazy/self-referencing arrow function Follow-up to the closure guard. Arrow functions auto-capture by value at definition time, so narrowing a property referenced inside one is only safe when the property is already fully assigned before the arrow function (e.g. a mock yielded from a generator - kept narrowing). Skip narrowing when the assignment wraps or follows the arrow function (e.g. a self-referencing type definition, as in webonyx/graphql-php DeferredFieldsTest), where the captured local would still be unset and the closure would observe null.
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.
Follow-up to #693.
Why
#693 stopped
NarrowUnusedSetUpDefinedPropertyRectorfrom narrowing a propertyreferenced inside a regular closure (no
usebinding → undefined local).But it still narrows properties captured by an arrow function, which is only
safe in some cases.
An arrow function auto-captures by value at definition time. That matches the
original lazy
$this->propertyread only when the property is already fullyassigned before the arrow function. When the assignment wraps or follows
the arrow function, the captured local is still unset:
Found in webonyx/graphql-php
DeferredFieldsTest(verified: breaks the deferred resolvers withzend.assertions=1, fixed after this change, full suite otherwise unchanged).Fix
Skip narrowing when the property is referenced inside an arrow function whose
capture happens before its assignment completes (wrapping or later assignment).
Still narrow when the property is assigned first (e.g. a mock yielded from a
generator —
MockToStubSetupCombinedkeeps narrowing).Added
skip_property_self_referenced_in_arrow_function.php.inc.