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 0000000..cc2cceb Binary files /dev/null and b/tests/assets/index.php differ diff --git a/tests/index.php b/tests/index.php deleted file mode 100644 index 0df42fc..0000000 --- a/tests/index.php +++ /dev/null @@ -1,6 +0,0 @@ -