Skip to content

Handle getProperties()/getMethods() loop in DowngradeSetAccessibleReflectionPropertyRector - #396

Merged
TomasVotruba merged 1 commit into
mainfrom
downgrade-set-accessible-on-get-properties
Sep 2, 2026
Merged

Handle getProperties()/getMethods() loop in DowngradeSetAccessibleReflectionPropertyRector#396
TomasVotruba merged 1 commit into
mainfrom
downgrade-set-accessible-on-get-properties

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9878.

Problem

On a downgraded PHP 7.4 build, php vendor/bin/rector composer-based throws:

Cannot access non-public member Rector\PHPUnit\ValueObject\AnnotationWithValueToAttribute::$annotationName

DowngradeSetAccessibleReflectionPropertyRector only inserts the guarded setAccessible(true) when a ReflectionProperty/ReflectionMethod is built with new. When the property comes from a ReflectionClass::getProperties() (or getMethods()) loop - as in ComposerBasedCommand::printConfigurationValue() - the rule never fired, so the 7.4 build read a non-public member without setAccessible().

Change

Detect a foreach iterating a ReflectionClass (incl. ReflectionObject, ReflectionEnum) getProperties()/getMethods() call, and prepend the guarded setAccessible(true) to the loop body:

foreach ($reflectionObject->getProperties() as $reflectionProperty) {
    if (PHP_VERSION_ID < 80100) {
        $reflectionProperty->setAccessible(true);
    }
    echo $reflectionProperty->getValue($object);
}

The caller type is checked against ReflectionClass, so unrelated getProperties() 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

…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
@TomasVotruba
TomasVotruba merged commit 1beef93 into main Sep 2, 2026
9 checks passed
@TomasVotruba
TomasVotruba deleted the downgrade-set-accessible-on-get-properties branch September 2, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

composer-based command errors on PHP 7.4

1 participant