Skip to content

Commit 349b5a3

Browse files
committed
Improve AutoDecoder
1 parent 324bacd commit 349b5a3

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

src/AutoDecoder.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
use Ganlv\EnphpDecoder\NodeVisitors\GlobalStringNodeVisitor;
3131
use Ganlv\EnphpDecoder\NodeVisitors\RemoveDefineGlobalVariableNameNodeVisitor;
3232
use Ganlv\EnphpDecoder\NodeVisitors\RemoveUnusedConstFetchNodeVisitor;
33+
use Ganlv\EnphpDecoder\PrettyPrinter\StandardPrettyPrinter;
3334
use PhpParser\NodeTraverser;
3435
use PhpParser\ParserFactory;
35-
use PhpParser\PrettyPrinter\Standard;
3636

3737
class AutoDecoder
3838
{
@@ -53,13 +53,6 @@ public function __construct($ast)
5353
$this->ast = $ast;
5454
}
5555

56-
public static function parseFile($code)
57-
{
58-
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP5);
59-
$ast = $parser->parse($code);
60-
return $ast;
61-
}
62-
6356
public function findAndRemoveGlobalVariableName()
6457
{
6558
$nodeVisitor = new FindAndRemoveGlobalVariableNameNodeVisitor();
@@ -165,27 +158,48 @@ public function beautify()
165158

166159
public function prettyPrintFile()
167160
{
168-
$prettyPrinter = new Standard();
169-
return $prettyPrinter->prettyPrintFile($this->ast);
161+
return StandardPrettyPrinter::prettyPrinter()->prettyPrintFile($this->ast);
170162
}
171163

172-
public static function decode($code)
164+
/**
165+
* @return bool is ast modified
166+
*/
167+
public function autoDecode()
173168
{
174-
$ast = self::parseFile($code);
175-
$decoder = new self($ast);
169+
$modified = false;
176170
for ($i = 0; $i < 10; $i++) { // avoid too many loops
177-
$decoder->findAndRemoveGlobalVariableName();
178-
if ($decoder->dataType === 0) {
171+
$this->findAndRemoveGlobalVariableName();
172+
if ($this->dataType === 0) {
179173
break;
180174
}
181-
$decoder->decodeStringArray();
182-
$decoder->removeDefineGlobalVariableName();
183-
$decoder->removeUnusedConstFetchNodeVisitor();
184-
$decoder->replaceGlobalString();
185-
$decoder->replaceFunctionLikeGlobalString();
186-
$decoder->renameFunctionLikeLocalVariable();
187-
$decoder->beautify();
175+
$modified = true;
176+
$this->decodeStringArray();
177+
$this->removeDefineGlobalVariableName();
178+
$this->removeUnusedConstFetchNodeVisitor();
179+
$this->replaceGlobalString();
180+
$this->replaceFunctionLikeGlobalString();
181+
$this->renameFunctionLikeLocalVariable();
182+
$this->beautify();
183+
}
184+
return $modified;
185+
}
186+
187+
public static function parseFile($code)
188+
{
189+
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP5);
190+
$ast = $parser->parse($code);
191+
return $ast;
192+
}
193+
194+
public static function decode($code)
195+
{
196+
$ast = self::parseFile($code);
197+
$decoder = new self($ast);
198+
$modified = $decoder->autoDecode();
199+
if ($modified) {
200+
return $decoder->prettyPrintFile();
201+
} else {
202+
return $code;
188203
}
189-
return $decoder->prettyPrintFile();
190204
}
191205
}

0 commit comments

Comments
 (0)