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.
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.
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, or class-string<MyClass>.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.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?