From d13540768923fb24330d8511d776ce08478947c9 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 19 Nov 2022 17:43:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8F=98=E9=87=8F=E6=B7=B7?= =?UTF-8?q?=E6=B7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/command/build/Dist.php | 12 +++++ .../phpparser/NodeFakeVarVisitorTools.php | 50 +++++++++++++++++++ .../tools/phpparser/NodeVisitorTools.php | 2 +- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 app/common/tools/phpparser/NodeFakeVarVisitorTools.php diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index 1149529..f216552 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -6,6 +6,7 @@ namespace app\common\command\build; use app\common\tools\PathTools; use app\common\tools\phpparser\MinifyPrinterTools; +use app\common\tools\phpparser\NodeFakeVarVisitorTools; use app\common\tools\phpparser\NodeVisitorTools; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; @@ -313,6 +314,11 @@ class Dist extends Command } } + $traverser = new NodeTraverser(); + + $traverser->addVisitor(new NodeFakeVarVisitorTools); + + $stmts = $traverser->traverse($stmts); $newCode = $prettyPrinter->prettyPrintFile($stmts); @@ -410,6 +416,12 @@ class Dist extends Command } + $traverser = new NodeTraverser(); + + $traverser->addVisitor(new NodeFakeVarVisitorTools); + + $function_stmts = $traverser->traverse($function_stmts); + $prettyPrinter = new MinifyPrinterTools(); $function_code = $prettyPrinter->prettyPrintFile($function_stmts); diff --git a/app/common/tools/phpparser/NodeFakeVarVisitorTools.php b/app/common/tools/phpparser/NodeFakeVarVisitorTools.php new file mode 100644 index 0000000..68bfa7d --- /dev/null +++ b/app/common/tools/phpparser/NodeFakeVarVisitorTools.php @@ -0,0 +1,50 @@ +name)) { + + if (in_array($node->name, $this->skipVariableName)) { + return; + } + + if (!isset($this::$varNameMap[$node->name])) { + $var_name = 'ul' . uniqid(); + $this::$varNameMap[$node->name] = $var_name; + } + + $node->name = $this::$varNameMap[$node->name]; + return $node; + } + } + } +} diff --git a/app/common/tools/phpparser/NodeVisitorTools.php b/app/common/tools/phpparser/NodeVisitorTools.php index 3960184..7535b79 100644 --- a/app/common/tools/phpparser/NodeVisitorTools.php +++ b/app/common/tools/phpparser/NodeVisitorTools.php @@ -53,7 +53,7 @@ class NodeVisitorTools extends NodeVisitorAbstract public function leaveNode(Node $node) { - if ($node instanceof Stmt) { + if ($node instanceof Node) { if (isset($node->attributes['comments'])) { $comments = $node->attributes['comments'];