[DeadCode] Remove unreachable class-like checks in Class_ only rules - #8217
Merged
Conversation
Trait_, Interface_ and Enum_ are separate php-parser node classes, not subclasses of Class_. A rule whose getNodeTypes() returns [Class_::class] can therefore never see one, so isTrait(), isInterface() and isClass() checks on the node own class reflection are constant.
TomasVotruba
marked this pull request as ready for review
July 29, 2026 16:38
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.
Draft. Small first step of the "remove PHPStan, don't wrap it" thread — the subset where reflection is answering a question that cannot have another answer.
Why these are constant
Trait_,Interface_andEnum_are sibling php-parser node classes, not subclasses ofClass_:All three rules below declare:
so the reflection they resolve from their own node is always a class.
isTrait()andisInterface()are alwaysfalse,isClass()alwaystrue.Changes
RemoveUnusedPrivateMethodRector— never reached, node is alwaysClass_:ParamTypeByParentCallTypeRector—! $classReflection->isClass()is alwaysfalse, so the guard reduces to the reflection-resolved check that precedes it:RemoveArgumentFromDefaultParentCallRector—$classReflectionis the outer node's own reflection, soisClass()is loop-invariant and alwaystrue:$ancestors = array_filter( $classReflection->getAncestors(), - fn (ClassReflection $ancestorClassReflection): bool => $classReflection->isClass() && $ancestorClassReflection->getName() !== $classReflection->getName() + fn (ClassReflection $ancestorClassReflection): bool => $ancestorClassReflection->getName() !== $classReflection->getName() );Scope
No behaviour change — these are provably constant conditions, not heuristics. 15 lines removed, 3 added.
Both
isClass()sites were checked to be the node's own reflection (resolveClassReflection($node)/($classMethod)), not an ancestor's. Ancestor-reflection checks like$parentClassReflection->isTrait()inAddOverrideAttributeToOverriddenMethodsRectorare genuinely needed and left alone.Touches the same
shouldSkip()as #TBD (native node API); whichever lands second needs a trivial rebase.