PHPackages                             substpk/type-resolver - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. substpk/type-resolver

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

substpk/type-resolver
=====================

1.1(9y ago)09MITPHPPHP &gt;=5.5

Since Feb 4Pushed 9y agoCompare

[ Source](https://github.com/substpk/TypeResolver)[ Packagist](https://packagist.org/packages/substpk/type-resolver)[ RSS](/packages/substpk-type-resolver/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

TypeResolver and FqsenResolver
==============================

[](#typeresolver-and-fqsenresolver)

The specification on types in DocBlocks (PSR-5) describes various keywords and special constructs but also how to statically resolve the partial name of a Class into a Fully Qualified Class Name (FQCN).

PSR-5 also introduces an additional way to describe deeper elements than Classes, Interfaces and Traits called the Fully Qualified Structural Element Name (FQSEN). Using this it is possible to refer to methods, properties and class constants but also functions and global constants.

This package provides two Resolvers that are capable of

1. Returning a series of Value Object for given expression while resolving any partial class names, and
2. Returning an FQSEN object after resolving any partial Structural Element Names into Fully Qualified Structural Element names.

Installing
----------

[](#installing)

The easiest way to install this library is with [Composer](https://getcomposer.org) using the following command:

```
$ composer require phpdocumentor/type-resolver

```

Examples
--------

[](#examples)

Ready to dive in and don't want to read through all that text below? Just consult the [examples](examples) folder and check which type of action that your want to accomplish.

On Types and Element Names
--------------------------

[](#on-types-and-element-names)

This component can be used in one of two ways

1. To resolve a Type or
2. To resolve a Fully Qualified Structural Element Name

The big difference between these two is in the number of things it can resolve.

The TypeResolver can resolve:

- a php primitive or pseudo-primitive such as a string or void (`@var string` or `@return void`).
- a composite such as an array of string (`@var string[]`).
- a compound such as a string or integer (`@var string|integer`).
- an object or interface such as the TypeResolver class (`@var TypeResolver`or `@var \phpDocumentor\Reflection\TypeResolver`)

    > please note that if you want to pass partial class names that additional steps are necessary, see the chapter `Resolving partial classes and FQSENs` for more information.

Where the FqsenResolver can resolve:

- Constant expressions (i.e. `@see \MyNamespace\MY_CONSTANT`)
- Function expressions (i.e. `@see \MyNamespace\myFunction()`)
- Class expressions (i.e. `@see \MyNamespace\MyClass`)
- Interface expressions (i.e. `@see \MyNamespace\MyInterface`)
- Trait expressions (i.e. `@see \MyNamespace\MyTrait`)
- Class constant expressions (i.e. `@see \MyNamespace\MyClass::MY_CONSTANT`)
- Property expressions (i.e. `@see \MyNamespace\MyClass::$myProperty`)
- Method expressions (i.e. `@see \MyNamespace\MyClass::myMethod()`)

Resolving a type
----------------

[](#resolving-a-type)

In order to resolve a type you will have to instantiate the class `\phpDocumentor\Reflection\TypeResolver`and call its `resolve` method like this:

```
$typeResolver = new \phpDocumentor\Reflection\TypeResolver();
$type = $typeResolver->resolve('string|integer');

```

In this example you will receive a Value Object of class `\phpDocumentor\Reflection\Types\Compound` that has two elements, one of type `\phpDocumentor\Reflection\Types\String_` and one of type `\phpDocumentor\Reflection\Types\Integer`.

The real power of this resolver is in its capability to expand partial class names into fully qualified class names; but in order to do that we need an additional `\phpDocumentor\Reflection\Types\Context` class that will inform the resolver in which namespace the given expression occurs and which namespace aliases (or imports) apply.

Resolving an FQSEN
------------------

[](#resolving-an-fqsen)

A Fully Qualified Structural Element Name is a reference to another element in your code bases and can be resolved using the `\phpDocumentor\Reflection\FqsenResolver` class' `resolve` method, like this:

```
$fqsenResolver = new \phpDocumentor\Reflection\FqsenResolver();
$fqsen = $fqsenResolver->resolve('\phpDocumentor\Reflection\FqsenResolver::resolve()');

```

In this example we resolve a Fully Qualified Structural Element Name (meaning that it includes the full namespace, class name and element name) and receive a Value Object of type `\phpDocumentor\Reflection\Fqsen`.

The real power of this resolver is in its capability to expand partial element names into Fully Qualified Structural Element Names; but in order to do that we need an additional `\phpDocumentor\Reflection\Types\Context` class that will inform the resolver in which namespace the given expression occurs and which namespace aliases (or imports) apply.

Resolving partial Classes and Structural Element Names
------------------------------------------------------

[](#resolving-partial-classes-and-structural-element-names)

Perhaps the best feature of this library is that it knows how to resolve partial class names into fully qualified class names.

For example, you have this file:

```
