kompaneytsev / mermaid-node-dumper
There is no license information available for the latest version (dev-main) of this package.
dev-main
2023-03-20 15:37 UTC
Requires
- jbzoo/mermaid-php: ^3.0
- nikic/php-parser: ^4.15
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-12-20 19:31:16 UTC
README
Output ast tree built by nikic/php-parser and represented in mermaid using jbzoo/mermaid-php.
Extended \Kompaneytsev\MermaidNodeDumper\StyledNode
supports mermaid styles, check out advanced example.
Simple example
<?php require 'vendor/autoload.php'; use Kompaneytsev\MermaidNodeDumper\NodeDumper; use PhpParser\ParserFactory; $code = <<<'CODE' <?php function test($foo) { var_dump($foo); } CODE; $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $ast = $parser->parse($code); $dumper = new NodeDumper; echo $dumper->dump($ast)->getLiveEditorUrl();
Output
Install
composer require --dev kompaneytsev/mermaid-node-dumper
Tests
vendor/bin/phpunit
Phpstan
vendor/bin/phpstan analyse
Advanced usage
<?php declare(strict_types=1); require 'vendor/autoload.php'; use JBZoo\MermaidPHP\Node; use Kompaneytsev\MermaidNodeDumper\NodeDumper; use Kompaneytsev\MermaidNodeDumper\StyledNode as MermaidNode; use PhpParser\Error; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Return_; use PhpParser\ParserFactory; use PhpParser\PrettyPrinter\Standard; $code = <<<'CODE' <?php namespace App\Controller; use Symfony\Component\HttpFoundation\Response; class LuckyController { public function number(): Response { $number = random_int(0, 100); return new Response( '<html><body>Lucky number: '.$number.'</body></html>' ); } } CODE; $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); try { $ast = $parser->parse($code); } catch (Error $error) { echo "Parse error: {$error->getMessage()}\n"; return; } $dumper = new NodeDumper([ ClassMethod::class => static function (string $id, ClassMethod $node): MermaidNode { $style = sprintf('style %s fill:#f9f,color:#000', $id); $name = sprintf('%s (%s)', $node->getType(), $node->name); return new MermaidNode($id, $name, Node::ROUND, $style); }, Return_::class => static function (string $id, Return_ $node): MermaidNode { $prettyPrinter = new Standard(); $style = sprintf('style %s fill:#ff0,color:#000', $id); $name = sprintf('%s (%s)', $node->getType(), $prettyPrinter->prettyPrint([$node])); return new MermaidNode($id, $name, Node::ROUND, $style); }, ],[ Return_::class => static function (Return_ $node): bool { return true; } ], [ Identifier::class => static function (Identifier $node): bool { return true; } ]); echo (string) $dumper->dump($ast);