Download the PHP package uuf6429/phpstan-phpdoc-type-resolver without Composer
On this page you can find all versions of the php package uuf6429/phpstan-phpdoc-type-resolver. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download uuf6429/phpstan-phpdoc-type-resolver
More information about uuf6429/phpstan-phpdoc-type-resolver
Files in uuf6429/phpstan-phpdoc-type-resolver
Package phpstan-phpdoc-type-resolver
Short Description Resolve (fully-qualify) types from PHPStan's PHPDoc parser
License MIT
Informations about the package phpstan-phpdoc-type-resolver
ðĄ PHPStan PHPDoc Type Resolver
Resolve (fully-qualify) types from PHPStan's PHPDoc parser.
ðū Installation
This package can be installed with Composer, simply run the following:
Consider using --dev
if you intend to use this library during development only.
ðĪ Why?
Because phpstan/phpdoc-parser
doesn't resolve types (it's not its responsibility) and phpdocument/type-resolver
currently has some major limitations.
ð Usage
In principle the resolver needs two things:
- The PHPStan-PHPDoc type (an instance of
TypeNode
). - '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
is converted intoIdentifierTypeNode
for the currently-passed class. - *
GenericTypeNode
to eitherConcreteGenericTypeNode
orTemplateGenericTypeNode
based on if the received instance contains unresolved generic/template types. - PHPStan locally-defined or imported-types, a
TypeDefTypeNode
will be provided (instead of anIdentifierTypeNode
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
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:
ð Without Factory/DocBlock Wrapper
Assuming as before that we have a \My\Project\Greeter
class with a greet
method, here's the longer way to resolve
the method's return type:
ðĪŠ Via Source Strings
It's also possible to resolve the type without actually loading the PHP source code (which is a requirement for reflection). However, this will take more work - the main difference is that you will need to set up the scope yourself.
Let's assume we want to resolve a type in a PHP source code string: