Handle getProperties()/getMethods() loop in DowngradeSetAccessibleReflectionPropertyRector - #396
Merged
Conversation
…owngradeSetAccessibleReflectionPropertyRector The rule only added setAccessible() for properties created via new ReflectionProperty()/new ReflectionMethod(). Properties iterated from ReflectionClass::getProperties()/getMethods() were skipped, so a downgraded 7.4 build failed with "Cannot access non-public member" when reading their values. Detect a foreach over a ReflectionClass getProperties()/getMethods() call and prepend the guarded setAccessible(true) to the loop body. Claude-Session: https://claude.ai/code/session_01HDxKQQjvqdXnmdvetn4t5d
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.
Fixes rectorphp/rector#9878.
Problem
On a downgraded PHP 7.4 build,
php vendor/bin/rector composer-basedthrows:DowngradeSetAccessibleReflectionPropertyRectoronly inserts the guardedsetAccessible(true)when aReflectionProperty/ReflectionMethodis built withnew. When the property comes from aReflectionClass::getProperties()(orgetMethods()) loop - as inComposerBasedCommand::printConfigurationValue()- the rule never fired, so the 7.4 build read a non-public member withoutsetAccessible().Change
Detect a
foreachiterating aReflectionClass(incl.ReflectionObject,ReflectionEnum)getProperties()/getMethods()call, and prepend the guardedsetAccessible(true)to the loop body:The caller type is checked against
ReflectionClass, so unrelatedgetProperties()methods are left untouched. Idempotent - skips when the guard is already present.Tests
Added fixtures:
on_get_properties,on_get_methods,skip_non_reflection_get_properties,skip_already_accessible_in_foreach. Full rule test green.https://claude.ai/code/session_01HDxKQQjvqdXnmdvetn4t5d