Last active
February 29, 2020 10:29
-
-
Save drupol/8a6436ec801f079409ede3456052b41a to your computer and use it in GitHub Desktop.
loophp/phptree + nikic/php-parser = <3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Author
drupol
commented
Feb 26, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment