phpdocumentor/type-resolver's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.
The phpDocumentor/Type-Resolver is a powerful tool based on the PSR-5 specification used to resolve class names, types and structural element names in PHP. It involves the creation of two resolvers that are capable of returning value objects for given expressions while resolving any partial class names and returning an FQSEN (Fully Qualified Structural Element Name) object after resolving any partial structural element names into fully qualified ones. This proves to be highly effective in resolving class names in doc blocks, dealing with arrays, compounds and object interfaces and more.
To use phpDocumentor/Type-Resolver, you first need to install the package via Composer using the command
composer require phpdocumentor/type-resolver. When you want to deal with types, you instantiate the
TypeResolver class and call its
resolve method. For instance:
$typeResolver = new \phpDocumentor\Reflection\TypeResolver(); $type = $typeResolver->resolve('string|integer');
$type object represents a compound type consisting of the string and integer types.
Similarly, you can deal with FQSENs using the
$fqsenResolver = new \phpDocumentor\Reflection\FqsenResolver(); $fqsen = $fqsenResolver->resolve('\phpDocumentor\Reflection\FqsenResolver::resolve()');
In this case, the
$fqsen object represents a fully qualified structural element name.
One key strength of phpDocumentor/Type-Resolver is its ability to handle partial class names. This involves providing a bit of Context to the resolving actions. This is done by creating and using an instance of the
$context = new \phpDocumentor\Reflection\Types\Context('\My\Example',[ 'Types' => '\phpDocumentor\Reflection\Types']); $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); $type = $typeResolver->resolve('Types\Context', $context);
Here, a given type
Types\Context is resolved into a fully qualified class name according to the provided namespace and aliases context.
As for the documentation of phpDocumentor/Type-Resolver, the best source of reference is the GitHub page. The readme file of the repository provides a comprehensive guide on installation, usage, examples as well as understanding types and element names, resolving a type, resolving FQSEN areas and handling partial classes and structural element names.