phpdocumentor/type-resolver
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
doctrine/deprecations | 1.1.2 | 7.42 kB | MIT | prod | |
phpdocumentor/reflection-common | 2.2.0 | 8.06 kB | MIT | prod | |
phpstan/phpdoc-parser | 1.24.3 | 70.56 kB | MIT | prod |
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');
Here, the $type
object represents a compound type consisting of the string and integer types.
Similarly, you can deal with FQSENs using the FqsenResolver
class:
$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
class:
$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.