phpdocumentor/type-resolver
Resolves PHP DocBlock types and partial class names into fully qualified names, and resolves structural element references into FQSENs, per PSR-5. Useful for static analysis, documentation generators, and reflection tooling.
Install or upgrade with Composer:
composer require phpdocumentor/type-resolver:^2.1
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. Version 2.1.0 is a minor release with no breaking changes; composer update from 2.0.x is safe.
phpreflection (e.g., ReflectionMethod) to extract types from docblocks:
$context = $contextFactory->createFromReflector(new ReflectionMethod(...));
$type = $resolver->resolve('@return User[]', $context);
@see references:
$fqsen = (new FqsenResolver())->resolve('App\Controller::index()', $context);
// Yields \App\Controller::index()
list<string>, array<int, User>, non-empty-array, class-string<MyClass>, and the newly supported non-decimal-int-string pseudo-type.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.self, static, parent are handled—but only in a valid Context with the correct FQCN.?string) are auto-wrapped in Nullable—check with $type instanceof Nullable and unwrap via $type->getInnerType().GenericTemplate removed, Collection → GenericType, removed True_/False_ aliases. See upgrade guide.non-decimal-int-string requires 2.1.0+: If you're on 2.0.x, upgrade before relying on it. Treat it as a PHPDoc/pseudo-type constraint for tooling, not runtime validation.Context via ContextFactory::createForNamespace() parses full source—cache results when processing many docblocks (e.g., across an entire codebase).$type->getValue() or cast to string ((string)$type) to get normalized output—ideal for logging or codegen.How can I help you explore Laravel packages today?