PHP code example of zero-to-prod / docgen-visitor

1. Go to this page and download the library: Download zero-to-prod/docgen-visitor 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/ */

    

zero-to-prod / docgen-visitor example snippets




hpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use Zerotoprod\DocgenVisitor\DocgenVisitor;

$comments = ['This is an updated class docblock'];
$changes = []; // This is used to accumulate changes from the DocgenVisitor
$traverser = new NodeTraverser();
$traverser->addVisitor(
    new DocgenVisitor(
        function (Node $node) {
            // Filter comments to specific types
            if ($node instanceof Node\Stmt\Class_) use ($comments) {
                return $comments;
            }
            return [];
        },
        $changes
    )
);

// Apply the visitor to a php file.
$traverser->traverse(
    (new ParserFactory())->createForHostVersion()
        ->parse(file_get_contents('User.php'));
);

$updatedCode = null;
foreach ($changes as $change) {
    // Replace the old docblock text with the new one
    $updatedCode = substr_replace(
        $originalCode,
        $change->text,
        $change->start,
        $change->end - $change->start + 1
    );
}

file_put_contents($filePath, $updatedCode);