@@ -179,6 +179,106 @@ $simpleCallableNodeTraverser->traverseNodesWithCallable($classMethod, function (
179179});
180180```
181181
182+ ### 5. Register Config
183+
184+ Register config in your ` config/config.php ` :
185+
186+ ``` php
187+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
188+ use Symplify\Astral\ValueObject\AstralConfig;
189+
190+ return static function (ContainerConfigurator $containerConfigurator): void {
191+ $containerConfigurator->import(AstralConfig::FILE_PATH);
192+ };
193+ ```
194+
195+ ### 6. Usage of ` SimplePhpDocParser `
196+
197+ Required services ` Symplify\Astral\PhpDocParser\SimplePhpDocParser ` in constructor, where you need it, and use it:
198+
199+ ``` php
200+ use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
201+ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
202+ use PHPStan\PhpDocParser\Ast\Type\TypeNode;
203+ use Symplify\Astral\PhpDocParser\SimplePhpDocParser;
204+
205+ final class SomeClass
206+ {
207+ public function __construct(
208+ private SimplePhpDocParser $simplePhpDocParser
209+ ) {
210+ }
211+
212+ public function some(): void
213+ {
214+ $docBlock = '/** @param int $name */';
215+
216+ /** @var PhpDocNode $phpDocNode */
217+ $simplePhpDocNode = $this->simplePhpDocParser->parseDocBlock($docBlock);
218+
219+ // param extras
220+
221+ /** @var TypeNode $nameParamType */
222+ $nameParamType = $simplePhpDocNode->getParamType('name');
223+
224+ /** @var ParamTagValueNode $nameParamTagValueNode */
225+ $nameParamTagValueNode = $simplePhpDocNode->getParam('name');
226+ }
227+ }
228+ ```
229+
230+ ## 4. Traverse Nodes with ` PhpDocNodeTraverser `
231+
232+ ``` php
233+ use PHPStan\PhpDocParser\Ast\Node;
234+ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
235+ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
236+ use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
237+ use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
238+ use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
239+ use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
240+ use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;
241+
242+ $phpDocNodeTraverser = new PhpDocNodeTraverser();
243+ $phpDocNode = new PhpDocNode([new PhpDocTagNode('@var', new VarTagValueNode(new IdentifierTypeNode('string')))]);
244+
245+ // A. you can use callable to traverse
246+ $callable = function (Node $node): Node {
247+ if (! $node instanceof VarTagValueNode) {
248+ return $node;
249+ }
250+
251+ $node->type = new IdentifierTypeNode('int');
252+ return $node;
253+ };
254+
255+ $callablePhpDocNodeVisitor = new CallablePhpDocNodeVisitor($callable, null);
256+ $phpDocNodeTraverser->addPhpDocNodeVisitor($callablePhpDocNodeVisitor);
257+
258+ // B. or class that extends AbstractPhpDocNodeVisitor
259+ final class IntegerPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
260+ {
261+ /**
262+ * @return Node|int|null
263+ */
264+ public function enterNode(Node $node)
265+ {
266+ if (! $node instanceof VarTagValueNode) {
267+ return $node;
268+ }
269+
270+ $node->type = new IdentifierTypeNode('int');
271+ return $node;
272+ }
273+ }
274+
275+ $integerPhpDocNodeVisitor = new IntegerPhpDocNodeVisitor();
276+ $phpDocNodeTraverser->addPhpDocNodeVisitor($integerPhpDocNodeVisitor);
277+
278+ // then traverse the main node
279+ $phpDocNodeTraverser->traverse($phpDocNode);
280+ ```
281+
182282## Report Issues
183283
184284In case you are experiencing a bug or want to request a new feature head over to the [ Symplify monorepo issue tracker] ( https://github.com/symplify/symplify/issues )
0 commit comments