PHPackages                             uuf6429/phpstan-phpdoc-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. uuf6429/phpstan-phpdoc-type-resolver

ActiveLibrary

uuf6429/phpstan-phpdoc-type-resolver
====================================

Resolve (fully-qualify) types from PHPStan's PHPDoc parser

3.0.0(1y ago)11021MITPHPPHP ^8.1

Since Jun 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/uuf6429/phpstan-phpdoc-type-resolver)[ Packagist](https://packagist.org/packages/uuf6429/phpstan-phpdoc-type-resolver)[ Fund](https://paypal.me/uuf6429)[ GitHub Sponsors](https://github.com/uuf6429)[ RSS](/packages/uuf6429-phpstan-phpdoc-type-resolver/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (11)Used By (1)

📡 PHPStan PHPDoc Type Resolver
==============================

[](#-phpstan-phpdoc-type-resolver)

[![CI](https://github.com/uuf6429/phpstan-phpdoc-type-resolver/actions/workflows/ci.yml/badge.svg)](https://github.com/uuf6429/phpstan-phpdoc-type-resolver/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/160b4e975748fde4bacf49b38640732dbe598bfc3c7ea3ada3c33562cf26ff70/68747470733a2f2f636f6465636f762e696f2f67682f757566363432392f7068707374616e2d706870646f632d747970652d7265736f6c7665722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/uuf6429/phpstan-phpdoc-type-resolver)[![Minimum PHP Version](https://camo.githubusercontent.com/ef363a5a30025ffef1857918c40fa01e97f03929e5f87d12a14bf334a7de9220/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e312d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/0b4c6ee001eda8688d05ce26b257fe36b26e0a4ad955758e5529aaeb5566db35/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d3432384637452e737667)](https://github.com/uuf6429/phpstan-phpdoc-type-resolver/blob/main/LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/7240920c60322707f246a5b88abbf69b92559fe381b381647398a2a6e3de3a38/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f7068707374616e2d706870646f632d747970652d7265736f6c7665722f76)](https://packagist.org/packages/uuf6429/phpstan-phpdoc-type-resolver)[![Latest Unstable Version](https://camo.githubusercontent.com/20fc14daf9c0c912b2555f6c475a867ed2c50d441aa856a752e791a3a98c2b1e/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f7068707374616e2d706870646f632d747970652d7265736f6c7665722f762f756e737461626c65)](https://packagist.org/packages/uuf6429/phpstan-phpdoc-type-resolver)

Resolve (fully-qualify) types from PHPStan's PHPDoc parser.

💾 Installation
--------------

[](#-installation)

This package can be installed with [Composer](https://getcomposer.org), simply run the following:

```
composer require uuf6429/phpstan-phpdoc-type-resolver
```

*Consider using `--dev` if you intend to use this library during development only.*

🤔 Why?
------

[](#-why)

Because `phpstan/phpdoc-parser` doesn't resolve types (it's not its responsibility) and `phpdocument/type-resolver`[currently has some major limitations](https://github.com/phpDocumentor/ReflectionDocBlock/issues/372).

🚀 Usage
-------

[](#-usage)

In principle the resolver needs two things:

1. The PHPStan-PHPDoc type (an instance of [`TypeNode`](https://github.com/phpstan/phpdoc-parser/blob/1.23.x/src/Ast/Type/TypeNode.php)).
2. 'Scope' information of where that type occurred.

There are two ways to retrieve that information, as shown below.

**Important:** The resolver will always convert some specific PHPStan types into something else as follows:

- \*[`ThisTypeNode`](https://github.com/phpstan/phpdoc-parser/blob/1.23.x/src/Ast/Type/ThisTypeNode.php) is converted into [`IdentifierTypeNode`](https://github.com/phpstan/phpdoc-parser/blob/1.23.x/src/Ast/Type/IdentifierTypeNode.php) for the currently-passed class.
- \*[`GenericTypeNode`](https://github.com/phpstan/phpdoc-parser/blob/1.23.x/src/Ast/Type/GenericTypeNode.php) to either [`ConcreteGenericTypeNode`](https://github.com/uuf6429/phpstan-phpdoc-type-resolver/blob/main/src/PhpDoc/Types/ConcreteGenericTypeNode.php) or [`TemplateGenericTypeNode`](https://github.com/uuf6429/phpstan-phpdoc-type-resolver/blob/main/src/PhpDoc/Types/TemplateGenericTypeNode.php) based on if the received instance contains unresolved generic/template types.
- PHPStan locally-defined or imported-types, a [`TypeDefTypeNode`](https://github.com/uuf6429/phpstan-phpdoc-type-resolver/blob/main/src/PhpDoc/Types/TypeDefTypeNode.php) will be provided (instead of an [`IdentifierTypeNode`](https://github.com/phpstan/phpdoc-parser/blob/1.23.x/src/Ast/Type/IdentifierTypeNode.php) with just the type name).

(\*) conversion is mandatory, failures will trigger some sort of exception (meaning: the original type *should* never be returned).

### 😎 Via Reflection

[](#-via-reflection)

Let's assume we have a `\My\Project\Greeter` class with a `greet` method, here's how we can resolve that method's return type:

```
