From dedd4b55fe3e03cae9bd5ac822cfdccd8deb3fb6 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 9 May 2021 21:24:57 +0200 Subject: [PATCH] make node_helper.php safe for similar names --- src/functions/node_helper.php | 67 ++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/src/functions/node_helper.php b/src/functions/node_helper.php index d4aaf95659d..54afb11cc55 100644 --- a/src/functions/node_helper.php +++ b/src/functions/node_helper.php @@ -6,33 +6,44 @@ use PhpParser\Node; use PhpParser\PrettyPrinter\Standard; use Tracy\Dumper; -function dn(Node $node, int $depth = 2): void -{ - dump_node($node, $depth); -} - - -function dump_node(Node $node, int $depth = 2): void -{ - Dumper::dump($node, [ - Dumper::DEPTH => $depth, - ]); -} - -/** - * @param Node|Node[] $node - */ -function print_node($node): void -{ - $standard = new Standard(); - - if (is_array($node)) { - foreach ($node as $singleNode) { - $printedContent = $standard->prettyPrint([$singleNode]); - Dumper::dump($printedContent); - } - } else { - $printedContent = $standard->prettyPrint([$node]); - Dumper::dump($printedContent); +if (! function_exists('dn')) { + function dn(Node $node, int $depth = 2): void + { + dump_node($node, $depth); + } +} + + +if (! function_exists('dump_node')) { + /** + * @param Node|Node[] $node + */ + function dump_node($node, int $depth = 2): void + { + $nodes = is_array($node) ? $node : [$node]; + + foreach ($nodes as $node) { + Dumper::dump($node, [ + Dumper::DEPTH => $depth, + ]); + } + } +} + + +if (! function_exists('print_node')) { + /** + * @param Node|Node[] $node + */ + function print_node($node): void + { + $standard = new Standard(); + + $nodes = is_array($node) ? $node : [$node]; + + foreach ($nodes as $node) { + $printedContent = $standard->prettyPrint([$node]); + Dumper::dump($printedContent); + } } }