Infer return type from class-string<T> in ClassResolver extensions#965
Merged
Infer return type from class-string<T> in ClassResolver extensions#965
Conversation
…nsions When a variable typed as class-string<T> is passed to ClassResolverInterface::getInstanceFromDefinition() or Drupal::classResolver(), PHPStan now infers the return type as T instead of falling back to object. The existing service-ID and constant-string class-name resolution via ServiceMap is unchanged. This only fills the gap for non-constant class-string<T> variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
75ef74a to
6a25edb
Compare
There was a problem hiding this comment.
Pull request overview
Improves PHPStan dynamic return type inference for Drupal’s class resolver so that passing a class-string<T> variable to ClassResolverInterface::getInstanceFromDefinition() / \Drupal::classResolver() yields return type T (rather than a generic object), while preserving existing behavior for service-id resolution and constant class-string literals.
Changes:
- Extend
DrupalClassResolverReturnTypeto extract and return the object type fromclass-string<T>inputs when the argument is not a constant string. - Add a new type inference fixture covering
class-string<T>variables across the four supported call patterns.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Type/DrupalClassResolverReturnType.php |
Uses Type::getClassStringObjectType() in the non-constant-string path to return the generic object type for class-string<T> arguments. |
tests/src/Type/data/drupal-class-resolver.php |
Adds fixture assertions validating inferred return types for class-string<Foo> and class-string<MyService> variables across resolver call patterns. |
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
class-string<T>is passed toClassResolverInterface::getInstanceFromDefinition()orDrupal::classResolver(), PHPStan now infers the return type asTinstead ofobjectType::getClassStringObjectType()to extract the generic class type in the no-constant-strings fallback path ofDrupalClassResolverReturnType'service_map.my_service'→MyService) and constant class-name strings (Foo::class) are unchangedThis fills the gap that PR #402 was attempting to address, without replacing the dynamic extensions (which are still needed for service-ID resolution via
ServiceMap).Test plan
test_class_string_vars()fixture function covers all four call patterns (ClassResolver,\Drupal::service('class_resolver'),\Drupal::classResolver()->...,\Drupal::classResolver($var)) withclass-string<T>variablesphp vendor/bin/phpunit --filter=DrupalContainerDynamicReturnTypeTest— all passphp vendor/bin/phpcs src/— cleanCloses #402
🤖 Generated with Claude Code