From 451efd7bede04b80f2747dd69f2d1ce971db16c4 Mon Sep 17 00:00:00 2001 From: Ganlv Date: Mon, 4 Mar 2019 16:21:46 +0800 Subject: [PATCH] Improve finder. New test script Global variable not always $GLOBALS --- src/AutoDecoder.php | 11 +++++++---- src/NodeVisitors/BeautifyNodeVisitor.php | 1 - ...indAndRemoveGlobalVariableNameNodeVisitor.php | 5 +++-- .../FunctionGlobalStringNodeVisitor.php | 8 +++++--- src/NodeVisitors/GlobalStringNodeVisitor.php | 8 +++++--- ...RemoveDefineGlobalVariableNameNodeVisitor.php | 8 ++++---- tests/assets/index.php | Bin 0 -> 8005 bytes tests/index.php | 6 ------ tests/test.php | 11 +++++++++++ 9 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 tests/assets/index.php delete mode 100644 tests/index.php create mode 100644 tests/test.php diff --git a/src/AutoDecoder.php b/src/AutoDecoder.php index 2da7395..115e848 100644 --- a/src/AutoDecoder.php +++ b/src/AutoDecoder.php @@ -39,6 +39,7 @@ class AutoDecoder { protected $ast; protected $globalVarName; + protected $globalVarKey; protected $delimiter; protected $data; protected $start; @@ -64,6 +65,7 @@ public function findAndRemoveGlobalVariableName() $traverser->addVisitor($nodeVisitor); $this->ast = $traverser->traverse($this->ast); $this->globalVarName = $nodeVisitor->globalVarName; + $this->globalVarKey = $nodeVisitor->globalVarKey; $this->delimiter = $nodeVisitor->delimiter; $this->data = $nodeVisitor->data; $this->start = $nodeVisitor->start; @@ -73,7 +75,7 @@ public function findAndRemoveGlobalVariableName() public function removeDefineGlobalVariableName() { - $nodeVisitor = new RemoveDefineGlobalVariableNameNodeVisitor($this->globalVarName); + $nodeVisitor = new RemoveDefineGlobalVariableNameNodeVisitor($this->globalVarKey); $traverser = new NodeTraverser(); $traverser->addVisitor($nodeVisitor); $this->ast = $traverser->traverse($this->ast); @@ -92,7 +94,7 @@ public function removeUnusedConstFetchNodeVisitor() public function replaceGlobalString() { - $nodeVisitor = new GlobalStringNodeVisitor($this->globalVarName, $this->stringArray); + $nodeVisitor = new GlobalStringNodeVisitor($this->globalVarName, $this->globalVarKey, $this->stringArray); $traverser = new NodeTraverser(); $traverser->addVisitor($nodeVisitor); $this->ast = $traverser->traverse($this->ast); @@ -102,10 +104,11 @@ public function replaceGlobalString() public function replaceFunctionLikeGlobalString() { $globalVarName = $this->globalVarName; + $globalVarKey = $this->globalVarKey; $stringArray = $this->stringArray; - $nodeVisitor = new FunctionLikeNodeVisitor(function ($node) use ($globalVarName, $stringArray) { + $nodeVisitor = new FunctionLikeNodeVisitor(function ($node) use ($globalVarName, $globalVarKey, $stringArray) { /** @var $node \PhpParser\Node\Stmt\Function_ */ - $nodeVisitor = new FunctionGlobalStringNodeVisitor($globalVarName, $stringArray); + $nodeVisitor = new FunctionGlobalStringNodeVisitor($globalVarName, $globalVarKey, $stringArray); $traverser = new NodeTraverser(); $traverser->addVisitor($nodeVisitor); $node->stmts = $traverser->traverse($node->stmts); diff --git a/src/NodeVisitors/BeautifyNodeVisitor.php b/src/NodeVisitors/BeautifyNodeVisitor.php index 7b176d4..fe8a979 100644 --- a/src/NodeVisitors/BeautifyNodeVisitor.php +++ b/src/NodeVisitors/BeautifyNodeVisitor.php @@ -14,7 +14,6 @@ public function leaveNode(Node $node) || $node instanceof Node\Expr\MethodCall) && $node->name instanceof Node\Scalar\String_) { $node->name = new Node\Name($node->name->value); - } elseif ($node instanceof Node\Expr\New_ && $node->class instanceof Node\Scalar\String_) { $node->class = new Node\Name($node->class->value); diff --git a/src/NodeVisitors/FindAndRemoveGlobalVariableNameNodeVisitor.php b/src/NodeVisitors/FindAndRemoveGlobalVariableNameNodeVisitor.php index 3fd3890..63aa492 100644 --- a/src/NodeVisitors/FindAndRemoveGlobalVariableNameNodeVisitor.php +++ b/src/NodeVisitors/FindAndRemoveGlobalVariableNameNodeVisitor.php @@ -9,6 +9,7 @@ class FindAndRemoveGlobalVariableNameNodeVisitor extends NodeVisitorAbstract { public $globalVarName; + public $globalVarKey; public $delimiter; public $data; public $start; @@ -20,7 +21,6 @@ public function leaveNode(Node $node) && $node->expr instanceof \PhpParser\Node\Expr\Assign && $node->expr->var instanceof \PhpParser\Node\Expr\ArrayDimFetch && $node->expr->var->var instanceof \PhpParser\Node\Expr\Variable - && $node->expr->var->var->name === 'GLOBALS' && $node->expr->var->dim instanceof \PhpParser\Node\Expr\ConstFetch && $node->expr->var->dim->name instanceof \PhpParser\Node\Name && count($node->expr->var->dim->name->parts) === 1 @@ -63,7 +63,8 @@ public function leaveNode(Node $node) && $node->expr->expr->args[1]->byRef === false && $node->expr->expr->args[1]->unpack === false ) { - $this->globalVarName = $node->expr->var->dim->name->parts[0]; + $this->globalVarName = $node->expr->var->var->name; + $this->globalVarKey = $node->expr->var->dim->name->parts[0]; $this->delimiter = $node->expr->expr->args[0]->value->value; $this->data = $node->expr->expr->args[1]->value->args[0]->value->args[0]->value->value; $this->start = $node->expr->expr->args[1]->value->args[0]->value->args[1]->value->value; diff --git a/src/NodeVisitors/FunctionGlobalStringNodeVisitor.php b/src/NodeVisitors/FunctionGlobalStringNodeVisitor.php index ff7db69..3b40521 100644 --- a/src/NodeVisitors/FunctionGlobalStringNodeVisitor.php +++ b/src/NodeVisitors/FunctionGlobalStringNodeVisitor.php @@ -10,11 +10,13 @@ class FunctionGlobalStringNodeVisitor extends NodeVisitorAbstract { public $localVarName; public $globalVarName; + public $globalVarKey; public $stringArray; - public function __construct($globalVarName, $stringArray) + public function __construct($globalVarName, $globalVarKey, $stringArray) { $this->globalVarName = $globalVarName; + $this->globalVarKey = $globalVarKey; $this->stringArray = $stringArray; } @@ -25,10 +27,10 @@ public function leaveNode(Node $node) && $node->expr->var instanceof Node\Expr\Variable && $node->expr->expr instanceof Node\Expr\ArrayDimFetch && $node->expr->expr->var instanceof Node\Expr\Variable - && $node->expr->expr->var->name === 'GLOBALS' + && $node->expr->expr->var->name === $this->globalVarName && $node->expr->expr->dim instanceof Node\Expr\ConstFetch && $node->expr->expr->dim->name instanceof Node\Name - && $node->expr->expr->dim->name->parts[0] === $this->globalVarName + && $node->expr->expr->dim->name->parts[0] === $this->globalVarKey ) { $this->localVarName = $node->expr->var->name; return NodeTraverser::REMOVE_NODE; diff --git a/src/NodeVisitors/GlobalStringNodeVisitor.php b/src/NodeVisitors/GlobalStringNodeVisitor.php index af30e9a..eb6e7bd 100644 --- a/src/NodeVisitors/GlobalStringNodeVisitor.php +++ b/src/NodeVisitors/GlobalStringNodeVisitor.php @@ -8,11 +8,13 @@ class GlobalStringNodeVisitor extends NodeVisitorAbstract { public $globalVarName; + public $globalVarKey; public $stringArray; - public function __construct($globalVarName, $stringArray) + public function __construct($globalVarName, $globalVarKey, $stringArray) { $this->globalVarName = $globalVarName; + $this->globalVarKey = $globalVarKey; $this->stringArray = $stringArray; } @@ -21,10 +23,10 @@ public function leaveNode(Node $node) if ($node instanceof Node\Expr\ArrayDimFetch && $node->var instanceof Node\Expr\ArrayDimFetch && $node->var->var instanceof Node\Expr\Variable - && $node->var->var->name === 'GLOBALS' + && $node->var->var->name === $this->globalVarName && $node->var->dim instanceof Node\Expr\ConstFetch && $node->var->dim->name instanceof Node\Name - && $node->var->dim->name->parts[0] === $this->globalVarName + && $node->var->dim->name->parts[0] === $this->globalVarKey && $node->dim instanceof Node\Scalar\LNumber ) { return new Node\Scalar\String_($this->stringArray[$node->dim->value]); diff --git a/src/NodeVisitors/RemoveDefineGlobalVariableNameNodeVisitor.php b/src/NodeVisitors/RemoveDefineGlobalVariableNameNodeVisitor.php index 1075262..bcbf971 100644 --- a/src/NodeVisitors/RemoveDefineGlobalVariableNameNodeVisitor.php +++ b/src/NodeVisitors/RemoveDefineGlobalVariableNameNodeVisitor.php @@ -8,11 +8,11 @@ class RemoveDefineGlobalVariableNameNodeVisitor extends NodeVisitorAbstract { - public $globalVarName; + public $globalVarKey; - public function __construct($globalVarName) + public function __construct($globalVarKey) { - $this->globalVarName = $globalVarName; + $this->globalVarKey = $globalVarKey; } public function leaveNode(Node $node) @@ -25,7 +25,7 @@ public function leaveNode(Node $node) && count($node->expr->args) === 2 && $node->expr->args[0] instanceof \PhpParser\Node\Arg && $node->expr->args[0]->value instanceof \PhpParser\Node\Scalar\String_ - && $node->expr->args[0]->value->value === $this->globalVarName + && $node->expr->args[0]->value->value === $this->globalVarKey && $node->expr->args[0]->byRef === false && $node->expr->args[0]->unpack === false && $node->expr->args[1] instanceof \PhpParser\Node\Arg diff --git a/tests/assets/index.php b/tests/assets/index.php new file mode 100644 index 0000000000000000000000000000000000000000..cc2cceb45a534f60e65304c813a1833c4702b3a4 GIT binary patch literal 8005 zcmcIpe{@WD7AIC~x0K`9u44}-%#nFS^5$0(d4^d++zXnY@{KBN=p#KXShB ze((L<&-Z@s_ugyx$jtOiS@dAp(4jKchJVM)(p|1hXMA+@G>a?B?lh)bY=$Ts>x#~p z1OGroB^w+qV;v5=Bh|rX+8r*7ZJHt}HF50NKP07&8$ao-QAreIV$BvCtB9!hsyQN3 z7O|o_f?}30zW3?E^JR}OpDrmq@k!mD8yg-i-0;P*y?JM=|8?i;=8tZ#UH#yG)xEQY zh4ZiPyi!z<^U1m|D(bI(b8y@G?JKIk`g6m{$5#qk_U&3+UU;x*&xH*qADzx;IKgnj)sC+9YtSyHp~{Y^F9sjhv0{Ced_xueHizxc+J(u{F^likkiCvVpO zcH5Wh7uL*Qv8-^DTQjr#(yiv>w;tr^b9?Q6zv272cK!733u#xCX=VDGhKUF2Y?|b{i9ah`ke@nE z@t~-_(0C@heEVqgJ^i?08LD>%sVk?9U-{v#?(3r}bMNKct?hki)fDRWO_RzFIA;I) zkMqiIvpwQxmJZB!4%u?+u=)Ebx0WSV|K^)x(S|--E4RJ=M#J{(js0Hv@_6Uy!#mdg zP}xgi*xx#9I>MiZgpCZtP#vFlKQ1hMu?9wAi9%cP#IAw{~pFR$1vgM@>JFK?#FFX%oWD~&ZSNL#Zb30SgU%~nbn@$s7tK{$-+OJ=vhGjK z1$#!%xu;0nF(7(WP1w&5Psu5m+_Otjm_DP-^;X|`y1L)*{Z)h3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