PHP code example of uuf6429 / phpstan-phpdoc-type-resolver
1. Go to this page and download the library: Download uuf6429/phpstan-phpdoc-type-resolver library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uuf6429 / phpstan-phpdoc-type-resolver example snippets
// Reflect our class method
$reflector = new \ReflectionMethod(\My\Project\Greeter::class, 'greet');
// Use the provided factory to easily parse the PHPDoc, which additionally automatically resolves the types
$docBlock = \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Factory::createInstance()
->createFromReflector($reflector);
// And finally, retrieve the resolved type of the return tag
/** @var \PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode $returnTag */
$returnTag = $docBlock->getTag('@return');
$finalReturnType = $returnTag->type;
// Reflect our class method
$reflector = new \ReflectionMethod(\My\Project\Greeter::class, 'greet');
// Use the scope resolver to get information about that method
$scopeResolver = new \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\ReflectorScopeResolver();
$scope = $scopeResolver->resolve($reflector);
// Parse the PHPDoc block with PHPStan PHPDoc parser
$lexer = new \PHPStan\PhpDocParser\Lexer\Lexer();
$constExprParser = new \PHPStan\PhpDocParser\Parser\ConstExprParser();
$typeParser = new \PHPStan\PhpDocParser\Parser\TypeParser($constExprParser);
$parser = new \PHPStan\PhpDocParser\Parser\PhpDocParser($typeParser, $constExprParser);
$docBlock = $parser->parse(
new \PHPStan\PhpDocParser\Parser\TokenIterator(
$lexer->tokenize($scope->comment) // 👈 note that the scope resolver also retrieves the PHPDoc block for us
)
);
// Finally, we initialize the type resolver and resolve the first return type of the doc block
$typeResolver = new \uuf6429\PHPStanPHPDocTypeResolver\TypeResolver();
$finalReturnType = $typeResolver->resolve($scope, $docBlock->getReturnTagValues()[0]->type);
$source = <<<'PHP'
namespace My\Project\Services;
use My\Project\PersonEntity as Person;
class Greeter {
/**
* @param Person|object{name: string} $person
*/
public function greet($person): void {
echo "Hello, {$person->name}!";
}
}
PHP;
// Construct the scope manually - automating this will take some work
$scope = new \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Scope(
// In-memory file; you could also use php memory streams etc
file: 'data:base64,' . base64_encode($source),
// approximate line where the type has occurred - everything else below has to be specified manually
line: 73,
class: 'My\Project\Services\Greeter',
comment: <<<'PHP'
/**
* @param Person|object{name: string} $person
*/
PHP
);
// The factory can also be used with a custom scope
$docBlock = \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Factory::createInstance()
->createFromScope($scope);
// And as before, retrieve the resolved type of the return tag
/** @var \PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode $returnTag */
$returnTag = $docBlock->getTag('@return');
$finalReturnType = $returnTag->type;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.