Skip to content

[perf] Read attribute-class from node attrGroups in RemoveUnusedConstructorParamRector - #8720

Merged
TomasVotruba merged 1 commit into
mainfrom
tv-swap-attribute-class-node
Oct 19, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv-swap-attribute-class-node

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

What

RemoveUnusedConstructorParamRector skips attribute marker classes (their constructor params are meta, not dead code). It did that by resolving the class reflection and calling ClassReflection::isAttributeClass().

Whether a class carries #[Attribute] is declared directly on the class - it is not inherited - so it is fully visible in the node's attrGroups. This reads it straight off the Class_ node instead, and runs the check before resolveClassReflection(). An attribute marker class with a doc-commented constructor now bails before any reflection resolves.

Same node-direct pattern as the sibling attribute checks already in the dead-code rules. ReflectionResolver stays - it is still needed for the interface hasNativeMethod contract check below.

Bench

bench/remove_unused_constructor_param_attribute_bench.php - 150 attribute marker classes with doc-commented constructors, refactor() driven over each (9000 calls), CPU time (user+sys), warmup excluded.

before (reflection): 33.070 us/call
after  (node scan):   3.863 us/call   (~8.5x)

The win is the skipped resolveClassReflection() on the attribute-marker path; behaviour is unchanged.

Tests

Existing skip_attribute_marker.php.inc (FQ #[\Attribute]) still passes. Added skip_imported_attribute_marker.php.inc to lock the imported + argumented form #[Attribute(Attribute::TARGET_CLASS)], which the node name-resolution path must also recognise. Full rule test green (16 tests).

@TomasVotruba
TomasVotruba merged commit a4b53e8 into main Oct 19, 2026
48 of 49 checks passed
@TomasVotruba
TomasVotruba deleted the tv-swap-attribute-class-node branch October 19, 2026 19:43
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.

1 participant