30
30
use Ganlv \EnphpDecoder \NodeVisitors \GlobalStringNodeVisitor ;
31
31
use Ganlv \EnphpDecoder \NodeVisitors \RemoveDefineGlobalVariableNameNodeVisitor ;
32
32
use Ganlv \EnphpDecoder \NodeVisitors \RemoveUnusedConstFetchNodeVisitor ;
33
+ use Ganlv \EnphpDecoder \PrettyPrinter \StandardPrettyPrinter ;
33
34
use PhpParser \NodeTraverser ;
34
35
use PhpParser \ParserFactory ;
35
- use PhpParser \PrettyPrinter \Standard ;
36
36
37
37
class AutoDecoder
38
38
{
@@ -53,13 +53,6 @@ public function __construct($ast)
53
53
$ this ->ast = $ ast ;
54
54
}
55
55
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
-
63
56
public function findAndRemoveGlobalVariableName ()
64
57
{
65
58
$ nodeVisitor = new FindAndRemoveGlobalVariableNameNodeVisitor ();
@@ -165,27 +158,48 @@ public function beautify()
165
158
166
159
public function prettyPrintFile ()
167
160
{
168
- $ prettyPrinter = new Standard ();
169
- return $ prettyPrinter ->prettyPrintFile ($ this ->ast );
161
+ return StandardPrettyPrinter::prettyPrinter ()->prettyPrintFile ($ this ->ast );
170
162
}
171
163
172
- public static function decode ($ code )
164
+ /**
165
+ * @return bool is ast modified
166
+ */
167
+ public function autoDecode ()
173
168
{
174
- $ ast = self ::parseFile ($ code );
175
- $ decoder = new self ($ ast );
169
+ $ modified = false ;
176
170
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 ) {
179
173
break ;
180
174
}
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 ;
188
203
}
189
- return $ decoder ->prettyPrintFile ();
190
204
}
191
205
}
0 commit comments