Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Type Resolver Laravel Package

phpdocumentor/type-resolver

Resolves DocBlock types and structural element names per PSR-5. Converts partial class names to fully qualified class names, parses type expressions into value objects, and resolves FQSENs for classes, methods, properties, functions, constants and more.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package with Composer:

composer require phpdocumentor/type-resolver

Start by resolving simple types:

use phpDocumentor\Reflection\TypeResolver;

$resolver = new TypeResolver();
$type = $resolver->resolve('string|int');

For real-world use, pair with Context to resolve partial class names (e.g., @var UserService\App\Services\UserService). Use ContextFactory::createForNamespace() to auto-generate context from source code and namespace.

Implementation Patterns

  • DocBlock parsing: Use alongside phpreflection (e.g., ReflectionMethod) to extract types from docblocks:
    $context = $contextFactory->createFromReflector(new ReflectionMethod(...));
    $type = $resolver->resolve('@return User[]', $context);
    
  • Code generation & static analysis: Feed resolved types into AST tools (e.g., PHPStan, Psalm integrations), or generate type-aware documentation.
  • FQSEN resolution: Resolve @see references:
    $fqsen = (new FqsenResolver())->resolve('App\Controller::index()', $context);
    // Yields \App\Controller::index()
    
  • Generic types (v2+): Use for modern PHP type hints like list<string>, array<int, User>, non-empty-array, or class-string<MyClass>.
  • IDE/tooling integrations: Write custom linters, refactoring tools, or autocompletion helpers that understand PHPDoc semantics.

Gotchas and Tips

  • Context is critical for resolving partial class names. Skipping it → @var Repository stays unresolved. Always derive context from actual file scope, not just the namespace.
  • Pseudo-types like self, static, parent are handled—but only in a valid Context with the correct FQCN.
  • Nullable types (?string) are auto-wrapped in Nullable—check with $type instanceof Nullable and unwrap via $type->getInnerType().
  • v2 breaking changes: GenericTemplate removed, CollectionGenericType, removed True_/False_ aliases. See upgrade guide.
  • Memory/perf: Creating Context via ContextFactory::createForNamespace() parses full source—cache results when processing many docblocks (e.g., across an entire codebase).
  • Validation tip: Always check $type->getValue() or cast to string ((string)$type) to get normalized output—ideal for logging or codegen.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport