Skip to content

Instantly share code, notes, and snippets.

@drupol
Last active February 29, 2020 10:29
Show Gist options
  • Save drupol/8a6436ec801f079409ede3456052b41a to your computer and use it in GitHub Desktop.
Save drupol/8a6436ec801f079409ede3456052b41a to your computer and use it in GitHub Desktop.
loophp/phptree + nikic/php-parser = <3
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<?php
declare(strict_types=1);
include __DIR__ . '/vendor/autoload.php';
use drupol\launcher\Launcher;
use loophp\phptree\Exporter\Image;
use PhpParser\Error;
use PhpParser\ParserFactory;
use loophp\phptree\Importer\NikicPhpParserImporter;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\Declare_;
$code = file_get_contents(__DIR__. '/vendor/symfony/finder/Finder.php');
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse($code);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
return;
}
// Optional: Remove specific node types for better readability.
$filter = new \loophp\phptree\Modifier\Filter(
static function (\loophp\phptree\Node\AttributeNodeInterface $node) {
$removeTypes = [
Use_::class,
Declare_::class,
];
return in_array(get_class($node->getAttribute('astNode')), $removeTypes, true);
});
$tree = $filter->modify((new NikicPhpParserImporter())->import($ast));
$exporter = new Image();
$imagePath = $exporter->setFormat('svg')->export($tree);
Launcher::open($imagePath);
@drupol
Copy link
Author

drupol commented Feb 26, 2020

graphviz3MvKUN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment