Treat plain class-string as a definite subtype of class-string<object> in GenericClassStringType::isSuperTypeOf() - #5956
Merged
ondrejmirtes merged 1 commit intoJun 30, 2026
Conversation
…ect>` in `GenericClassStringType::isSuperTypeOf()` - Add a `ClassStringType` branch to `GenericClassStringType::isSuperTypeOf()` that treats a plain `class-string` as `class-string<object>` and compares the generic type against `ObjectWithoutClassType`, mirroring the branch that already exists in `accepts()` and `inferTemplateTypes()`. Previously a plain `class-string` fell through to the generic `StringType` branch and returned `Maybe`. - This is the root cause exposed by #14887: after `is_subclass_of()`/`is_a()` narrowing, the argument has type `(class-string&T)|class-string<T>`; the intersection acceptance heuristic added in bad7874 distrusts the eager `accepts()` Yes when the holistic `isSuperTypeOf()` only returns `Maybe`. With the imprecise `Maybe` corrected to `Yes`, the intersection is again a definite subtype and the false `new ReflectionClass($class)` error disappears, while the #13190 cases stay correctly reported. - Probed analogous narrowing constructs: both `is_subclass_of()` and `is_a()` produced the false positive and are now fixed by the same change. - Added unit test cases to GenericClassStringTypeTest::dataIsSuperTypeOf and a rule regression test (bug-14887) reproducing the playground snippet.
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
new ReflectionClass($class)reported a falseargument.typeerror when$classwas aclass-stringnarrowed throughis_subclass_of($class, $interface)(with$interfaceaclass-string<T>). The narrowed argument has type(class-string&T)|class-string<T>, andPHPStan claimed it was not accepted by the constructor's
class-string<object>|objectparameter. The fix makes
class-string<object>correctly recognize a plainclass-stringas a definite subtype.
Changes
src/Type/Generic/GenericClassStringType.php: added aClassStringTypebranch toisSuperTypeOf()that treats a plainclass-stringasclass-string<object>anddelegates to
$this->type->isSuperTypeOf(new ObjectWithoutClassType()). This mirrors thebranch already present in
accepts()and theisClassString()handling ininferTemplateTypes().tests/PHPStan/Type/Generic/GenericClassStringTypeTest.php: addeddataIsSuperTypeOfcases for
class-string<object>andclass-string<T of object>against a plainclass-string.tests/PHPStan/Rules/Classes/InstantiationRuleTest.php+data/bug-14887.php: ruleregression test reproducing the issue's playground snippet.
phpstan-baseline.neon: bumped the existing baselinedinstanceof ClassStringTypecount for this file from 1 to 2 (the new branch reuses the same established pattern as
the adjacent
accepts()branch).Root cause
GenericClassStringType::accepts()andinferTemplateTypes()already handle a plainClassStringTypeby treating it asclass-string<object>(viaObjectWithoutClassType),but
isSuperTypeOf()was missing the parallel branch — a plainclass-stringfell into thegeneric
StringTypebranch and returnedMaybe. That strayMaybeis wrong: everyclass-stringis aclass-string<object>.The bug surfaced because of the intersection-acceptance heuristic added in bad7874
(#13190): when an intersection like
class-string&Tcontains aTemplateTypeand theholistic
isSuperTypeOf()only returnsMaybe, the heuristic distrusts the eagertemplate-lenient
accepts()Yes and reportsMaybe. Correcting the impreciseMaybetoYesmakes the holistic check returnYes, so the heuristic no longer fires and theintersection is again a definite subtype. The pattern is "a non-generic refined type must be
recognized as a subtype of its generic form across
accepts(),isSuperTypeOf(), andinferTemplateTypes()"; the only out-of-sync location wasisSuperTypeOf().Test
GenericClassStringTypeTest::testIsSuperTypeOfdata sets 19/20 assertclass-string<object>is aYessupertype of plainclass-string, andclass-string<T of object>isMaybe(correct for an invariant template).InstantiationRuleTest::testBug14887analyses the exact playground snippet andexpects no errors. Both tests fail before the fix and pass after.
is_a()as an analogous narrowing construct: it was broken in the same way and isfixed by the same change.
Fixes phpstan/phpstan#14887