Resolve class-string expressions to object types when detecting unused private methods, constants and static properties - #5953
Merged
staabm merged 4 commits intoJun 29, 2026
Conversation
…d private methods, constants and static properties - In UnusedPrivateMethodRule, UnusedPrivatePropertyRule and UnusedPrivateConstantRule, call getObjectTypeOrClassStringObjectType() on the type of a static call/fetch whose class is an expression (e.g. self::class::method(), $class::CONST, $class::$prop) instead of using the raw string type. - A class-string expression like self::class resolves to a string type, on which getMethodReflection()/getStaticPropertyReflection() return null, so the member was wrongly reported as unused. Converting to the corresponding object type lets the member lookup succeed and the usage be recognized. - Fixes the reported unused-method false positive for self::class::method(), plus the analogous static-property "never read" and class-constant cases, and the same family for variables typed as class-string<self> (GenericClassStringType, which unlike ConstantStringType does not delegate hasConstant()/hasMethod() to its object type).
staabm
approved these changes
Jun 29, 2026
VincentLanglet
approved these changes
Jun 29, 2026
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.
Summary
PHPStan reported a private method as unused when it was called via a class-string expression such as
self::class::sampleClass(). The dead-code rules resolved the call's class expression to astringtype and then failed to find the member on it, so the member was treated as never used. This fixes the false positive and the analogous static-property and class-constant cases.Changes
src/Rules/DeadCode/UnusedPrivateMethodRule.php— when a static call's class is an expression (not aNode\Name), resolve the type withgetObjectTypeOrClassStringObjectType()before looking up the method.src/Rules/DeadCode/UnusedPrivatePropertyRule.php— same conversion for static property fetches whose class is an expression.src/Rules/DeadCode/UnusedPrivateConstantRule.php— same conversion for class-constant fetches whose class is an expression.Root cause
A static member access like
self::class::sampleClass()parses with the class part being an expression (self::class), not aName. The rules took theelsebranch and used$scope->getType($class), which yields astring/class-stringtype.getMethodReflection()andgetStaticPropertyReflection()returnnullfor a string type, and the class-string is not a subtype of the analysed class'ObjectType, so the "is it used on this class?" guard failed and the member was kept in the unused set.The pattern affected all three dead-code member rules (method ↔ static property ↔ class constant).
getObjectTypeOrClassStringObjectType()is the unifying fix: it returns object types as-is and converts class-string types to the corresponding object type, so the existing member lookup just works.Note:
UnusedPrivateConstantRulehappened to already handle a constant class-string (ConstantStringTypedelegateshasConstant()/getConstant()to its object type), but it was still broken when the class expression had a non-constant class-string type such asclass-string<self>(GenericClassStringTypedoes not delegate those queries). The same conversion fixes that case too, keeping the three rules consistent.Test
tests/PHPStan/Rules/DeadCode/data/bug-14880.php+UnusedPrivateMethodRuleTest::testBug14880—self::class::method(), a$class = self::class; $class::method()variant, and aclass-string<self>-typed variant; expects nomethod.unusederror.tests/PHPStan/Rules/DeadCode/data/bug-14880-property.php+UnusedPrivatePropertyRuleTest::testBug14880— the same three variants for a private static property read/written via the class-string expression.tests/PHPStan/Rules/DeadCode/data/bug-14880-constant.php+UnusedPrivateConstantRuleTest::testBug14880— the same three variants for a private class constant.All three method/property tests fail without the fix; the constant test fails without the fix for the
class-string<self>variant.make phpstanand the fullDeadCoderule suite pass.Fixes phpstan/phpstan#14880