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(\uuf6429\PHPStanPHPDocTypeResolverTests\Fixtures\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 param tag
$paramTag = $docBlock->getTags('@param')[0];
assert((string)$paramTag->type === '(object{name: string} | uuf6429\PHPStanPHPDocTypeResolverTests\Fixtures\Person)');
// Reflect our class method
$reflector = new \ReflectionMethod(\uuf6429\PHPStanPHPDocTypeResolverTests\Fixtures\Greeter::class, 'greet');
// Use the scope resolver to get information about that method
$phpDocResolverFactory = new \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Factory();
$genericsExtractor = new \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Generics\Extractor($phpDocResolverFactory);
$scopeResolver = new \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\ReflectorScopeResolver($genericsExtractor);
$scope = $scopeResolver->resolve($reflector);
// Parse the PHPDoc block with PHPStan PHPDoc parser
$parserConfig = new \PHPStan\PhpDocParser\ParserConfig([]);
$lexer = new \PHPStan\PhpDocParser\Lexer\Lexer($parserConfig);
$constExprParser = new \PHPStan\PhpDocParser\Parser\ConstExprParser($parserConfig);
$typeParser = new \PHPStan\PhpDocParser\Parser\TypeParser($parserConfig, $constExprParser);
$parser = new \PHPStan\PhpDocParser\Parser\PhpDocParser($parserConfig, $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 param type of the first param
$typeResolver = new \uuf6429\PHPStanPHPDocTypeResolver\TypeResolver(
$genericsExtractor,
new \uuf6429\PHPStanPHPDocTypeResolver\PhpImports\Resolver(),
);
$paramType = $typeResolver->resolve($scope, $docBlock->getParamTagValues()[0]->type);
assert((string)$paramType === '(object{name: string} | uuf6429\PHPStanPHPDocTypeResolverTests\Fixtures\Person)');
$source = <<<'PHP'
namespace My\Project\Services;
use uuf6429\PHPStanPHPDocTypeResolverTests\Fixtures\Person as PersonEntity;
class Greeter {
/**
* @param PersonEntity|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:application/x-httpd-php;base64,' . base64_encode($source),
// Approximate line where the type occurred
line: 9,
// The class within which the type occurred
class: 'My\Project\Services\Greeter',
// The actualy PHPDoc block containing the type we're interested in
comment: "/**\n * @param PersonEntity|object{name: string} \$person\n */",
genericsResolver: new \uuf6429\PHPStanPHPDocTypeResolver\PhpDoc\Generics\GenericTypeMap(),
);
// 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 param tag
$paramTag = $docBlock->getTags('@param')[0];
assert((string)$paramTag->type === '(uuf6429\PHPStanPHPDocTypeResolverTests\Fixtures\Person | object{name: string})');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.