forked from Shaked/php.tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor.stub.php
executable file
·698 lines (618 loc) · 19.5 KB
/
refactor.stub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<?php $in_phar = true;
if (version_compare(phpversion(), '5.5.0', '<')) {
fwrite(STDERR, "PHP needs to be a minimum version of PHP 5.5.0\n");
exit(255);
}
//Copyright (c) 2014, Carlos C
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
//2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
//3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//Copyright (c) 2014, Carlos C
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
//2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
//3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
define("ST_AT", "@");
define("ST_BRACKET_CLOSE", "]");
define("ST_BRACKET_OPEN", "[");
define("ST_COLON", ":");
define("ST_COMMA", ",");
define("ST_CONCAT", ".");
define("ST_CURLY_CLOSE", "}");
define("ST_CURLY_OPEN", "{");
define("ST_DIVIDE", "/");
define("ST_DOLLAR", "$");
define("ST_EQUAL", "=");
define("ST_EXCLAMATION", "!");
define("ST_IS_GREATER", ">");
define("ST_IS_SMALLER", "<");
define("ST_MINUS", "-");
define("ST_MODULUS", "%");
define("ST_PARENTHESES_CLOSE", ")");
define("ST_PARENTHESES_OPEN", "(");
define("ST_PLUS", "+");
define("ST_QUESTION", "?");
define("ST_QUOTE", '"');
define("ST_REFERENCE", "&");
define("ST_SEMI_COLON", ";");
define("ST_TIMES", "*");
define("ST_BITWISE_OR", "|");
define("ST_BITWISE_XOR", "^");
if (!defined("T_POW")) {
define("T_POW", "**");
}
if (!defined("T_POW_EQUAL")) {
define("T_POW_EQUAL", "**=");
}
if (!defined("T_YIELD")) {
define("T_YIELD", "yield");
}
if (!defined("T_FINALLY")) {
define("T_FINALLY", "finally");
}
define('ST_PARENTHESES_BLOCK', 'ST_PARENTHESES_BLOCK');
define('ST_BRACKET_BLOCK', 'ST_BRACKET_BLOCK');
define('ST_CURLY_BLOCK', 'ST_CURLY_BLOCK');;
abstract class FormatterPass {
protected $indentChar = "\t";
protected $newLine = "\n";
protected $indent = 0;
protected $code = '';
protected $ptr = 0;
protected $tkns = [];
protected $useCache = false;
protected $cache = [];
protected $ignoreFutileTokens = [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT];
protected function appendCode($code = "") {
$this->code .= $code;
}
private function calculateCacheKey($direction, $ignoreList, $token) {
return $direction . "\x2" . implode('', $ignoreList) . "\x2" . (is_array($token) ? implode("\x2", $token) : $token);
}
abstract public function candidate($source, $foundTokens);
abstract public function format($source);
protected function getToken($token) {
if (isset($token[1])) {
return $token;
} else {
return [$token, $token];
}
}
protected function getCrlf($true = true) {
return $true ? $this->newLine : "";
}
protected function getCrlfIndent() {
return $this->getCrlf() . $this->getIndent();
}
protected function getIndent($increment = 0) {
return str_repeat($this->indentChar, $this->indent + $increment);
}
protected function getSpace($true = true) {
return $true ? " " : "";
}
protected function hasLn($text) {
return (false !== strpos($text, $this->newLine));
}
protected function hasLnAfter() {
$id = null;
$text = null;
list($id, $text) = $this->inspectToken();
return T_WHITESPACE === $id && $this->hasLn($text);
}
protected function hasLnBefore() {
$id = null;
$text = null;
list($id, $text) = $this->inspectToken(-1);
return T_WHITESPACE === $id && $this->hasLn($text);
}
protected function hasLnLeftToken() {
list($id, $text) = $this->getToken($this->leftToken());
return $this->hasLn($text);
}
protected function hasLnRightToken() {
list($id, $text) = $this->getToken($this->rightToken());
return $this->hasLn($text);
}
protected function inspectToken($delta = 1) {
if (!isset($this->tkns[$this->ptr + $delta])) {
return [null, null];
}
return $this->getToken($this->tkns[$this->ptr + $delta]);
}
protected function leftToken($ignoreList = [], $idx = false) {
$i = $this->leftTokenIdx($ignoreList);
return $this->tkns[$i];
}
protected function leftTokenIdx($ignoreList = []) {
$ignoreList = $this->resolveIgnoreList($ignoreList);
$i = $this->walkLeft($this->tkns, $this->ptr, $ignoreList);
return $i;
}
protected function leftTokenIs($token, $ignoreList = []) {
return $this->tokenIs('left', $token, $ignoreList);
}
protected function leftTokenSubsetIsAtIdx($tkns, $idx, $token, $ignoreList = []) {
$ignoreList = $this->resolveIgnoreList($ignoreList);
$idx = $this->walkLeft($tkns, $idx, $ignoreList);
return $this->resolveTokenMatch($tkns, $idx, $token);
}
protected function leftUsefulToken() {
return $this->leftToken($this->ignoreFutileTokens);
}
protected function leftUsefulTokenIdx() {
return $this->leftTokenIdx($this->ignoreFutileTokens);
}
protected function leftUsefulTokenIs($token) {
return $this->leftTokenIs($token, $this->ignoreFutileTokens);
}
protected function printAndStopAt($tknids) {
if (is_scalar($tknids)) {
$tknids = [$tknids];
}
$tknids = array_flip($tknids);
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
$this->cache = [];
if (isset($tknids[$id])) {
return [$id, $text];
}
$this->appendCode($text);
}
}
protected function printBlock($start, $end) {
$count = 1;
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
$this->cache = [];
$this->appendCode($text);
if ($start == $id) {
++$count;
}
if ($end == $id) {
--$count;
}
if (0 == $count) {
break;
}
}
}
protected function printCurlyBlock() {
$count = 1;
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
$this->cache = [];
$this->appendCode($text);
if (ST_CURLY_OPEN == $id) {
++$count;
}
if (T_CURLY_OPEN == $id) {
++$count;
}
if (T_DOLLAR_OPEN_CURLY_BRACES == $id) {
++$count;
}
if (ST_CURLY_CLOSE == $id) {
--$count;
}
if (0 == $count) {
break;
}
}
}
protected function printUntil($tknid) {
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
$this->cache = [];
$this->appendCode($text);
if ($tknid == $id) {
break;
}
}
}
protected function printUntilAny($tknids) {
$tknids = array_flip($tknids);
$whitespaceNewLine = false;
if (isset($tknids[$this->newLine])) {
$whitespaceNewLine = true;
}
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
$this->cache = [];
$this->appendCode($text);
if ($whitespaceNewLine && T_WHITESPACE == $id && $this->hasLn($text)) {
break;
}
if (isset($tknids[$id])) {
break;
}
}
return $id;
}
protected function printUntilTheEndOfString() {
$this->printUntil(ST_QUOTE);
}
protected function render($tkns = null) {
if (null == $tkns) {
$tkns = $this->tkns;
}
$tkns = array_filter($tkns);
$str = '';
foreach ($tkns as $token) {
list($id, $text) = $this->getToken($token);
$str .= $text;
}
return $str;
}
protected function renderLight($tkns = null) {
if (null == $tkns) {
$tkns = $this->tkns;
}
$str = '';
foreach ($tkns as $token) {
$str .= $token[1];
}
return $str;
}
private function resolveIgnoreList($ignoreList = []) {
if (empty($ignoreList)) {
$ignoreList[T_WHITESPACE] = true;
} else {
$ignoreList = array_flip($ignoreList);
}
return $ignoreList;
}
private function resolveTokenMatch($tkns, $idx, $token) {
if (!isset($tkns[$idx])) {
return false;
}
$foundToken = $tkns[$idx];
if ($foundToken === $token) {
return true;
} elseif (is_array($token) && isset($foundToken[1]) && in_array($foundToken[0], $token)) {
return true;
} elseif (is_array($token) && !isset($foundToken[1]) && in_array($foundToken, $token)) {
return true;
} elseif (isset($foundToken[1]) && $foundToken[0] == $token) {
return true;
}
return false;
}
protected function rightToken($ignoreList = []) {
$i = $this->rightTokenIdx($ignoreList);
return $this->tkns[$i];
}
protected function rightTokenIdx($ignoreList = []) {
$ignoreList = $this->resolveIgnoreList($ignoreList);
$i = $this->walkRight($this->tkns, $this->ptr, $ignoreList);
return $i;
}
protected function rightTokenIs($token, $ignoreList = []) {
return $this->tokenIs('right', $token, $ignoreList);
}
protected function rightTokenSubsetIsAtIdx($tkns, $idx, $token, $ignoreList = []) {
$ignoreList = $this->resolveIgnoreList($ignoreList);
$idx = $this->walkRight($tkns, $idx, $ignoreList);
return $this->resolveTokenMatch($tkns, $idx, $token);
}
protected function rightUsefulToken() {
return $this->rightToken($this->ignoreFutileTokens);
}
// protected function rightUsefulTokenIdx($idx = false) {
// return $this->rightTokenIdx($this->ignoreFutileTokens);
// }
protected function rightUsefulTokenIs($token) {
return $this->rightTokenIs($token, $this->ignoreFutileTokens);
}
protected function rtrimAndAppendCode($code = "") {
$this->code = rtrim($this->code) . $code;
}
protected function scanAndReplace(&$tkns, &$ptr, $start, $end, $call, $look_for) {
$look_for = array_flip($look_for);
$placeholder = '<?php' . ' /*\x2 PHPOPEN \x3*/';
$tmp = '';
$tknCount = 1;
$foundPotentialTokens = false;
while (list($ptr, $token) = each($tkns)) {
list($id, $text) = $this->getToken($token);
if (isset($look_for[$id])) {
$foundPotentialTokens = true;
}
if ($start == $id) {
++$tknCount;
}
if ($end == $id) {
--$tknCount;
}
$tkns[$ptr] = null;
if (0 == $tknCount) {
break;
}
$tmp .= $text;
}
if ($foundPotentialTokens) {
return $start . str_replace($placeholder, '', $this->{$call}($placeholder . $tmp)) . $end;
}
return $start . $tmp . $end;
}
protected function setIndent($increment) {
$this->indent += $increment;
if ($this->indent < 0) {
$this->indent = 0;
}
}
protected function siblings($tkns, $ptr) {
$ignoreList = $this->resolveIgnoreList([T_WHITESPACE]);
$left = $this->walkLeft($tkns, $ptr, $ignoreList);
$right = $this->walkRight($tkns, $ptr, $ignoreList);
return [$left, $right];
}
protected function substrCountTrailing($haystack, $needle) {
return strlen(rtrim($haystack, " \t")) - strlen(rtrim($haystack, " \t" . $needle));
}
protected function tokenIs($direction, $token, $ignoreList = []) {
if ('left' != $direction) {
$direction = 'right';
}
if (!$this->useCache) {
return $this->{$direction . 'tokenSubsetIsAtIdx'}($this->tkns, $this->ptr, $token, $ignoreList);
}
$key = $this->calculateCacheKey($direction, $ignoreList, $token);
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
$ret = $this->{$direction . 'tokenSubsetIsAtIdx'}($this->tkns, $this->ptr, $token, $ignoreList);
$this->cache[$key] = $ret;
return $ret;
}
protected function walkAndAccumulateUntil(&$tkns, $tknid) {
$ret = '';
while (list($index, $token) = each($tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
$ret .= $text;
if ($tknid == $id) {
break;
}
}
return $ret;
}
private function walkLeft($tkns, $idx, $ignoreList) {
$i = $idx;
while (--$i >= 0 && isset($tkns[$i][1]) && isset($ignoreList[$tkns[$i][0]]));
return $i;
}
private function walkRight($tkns, $idx, $ignoreList) {
$i = $idx;
$tknsSize = sizeof($tkns) - 1;
while (++$i < $tknsSize && isset($tkns[$i][1]) && isset($ignoreList[$tkns[$i][0]]));
return $i;
}
protected function walkUntil($tknid) {
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
if ($id == $tknid) {
return [$id, $text];
}
}
}
}
;
final class RefactorPass extends FormatterPass {
private $from;
private $to;
public function __construct($from, $to) {
$this->setFrom($from);
$this->setTo($to);
}
private function setFrom($from) {
$tkns = token_get_all('<?php ' . $from);
array_shift($tkns);
$tkns = array_map(function ($v) {
return $this->getToken($v);
}, $tkns);
$this->from = $tkns;
return $this;
}
private function getFrom() {
return $this->from;
}
private function setTo($to) {
$tkns = token_get_all('<?php ' . $to);
array_shift($tkns);
$tkns = array_map(function ($v) {
return $this->getToken($v);
}, $tkns);
$this->to = $tkns;
return $this;
}
private function getTo() {
return $this->to;
}
public function candidate($source, $foundTokens) {
return true;
}
public function format($source) {
$from = $this->getFrom();
$fromSize = sizeof($from);
$fromStr = implode('', array_map(function ($v) {
return $v[1];
}, $from));
$to = $this->getTo();
$toStr = implode('', array_map(function ($v) {
return $v[1];
}, $to));
$this->tkns = token_get_all($source);
$this->code = '';
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
if ($id == $from[0][0]) {
$match = true;
$buffer = $text;
for ($i = 1; $i < $fromSize; ++$i) {
list($index, $token) = each($this->tkns);
$this->ptr = $index;
list($id, $text) = $this->getToken($token);
$buffer .= $text;
if ('/*skipUntil' == substr($from[$i][1], 0, 11)) {
$skipCall = $from[$i][1];
$stopText = strtolower(trim(str_replace('skipUntil:', '', substr($text, 2, -2))));
++$i;
while (list($index, $token) = each($this->tkns)) {
$this->ptr = $index;
list($id, $text) = $this->getToken($token);
$buffer .= $text;
if ($id == $from[$i][0]) {
$tmp_i = $i;
$tmp_ptr = $this->ptr;
$s_match = true;
for ($tmp_i; $tmp_i < $fromSize; ++$tmp_i, ++$tmp_ptr) {
if ($from[$tmp_i][0] != $this->tkns[$tmp_ptr][0]) {
$s_match = false;
break;
}
}
if ($s_match) {
break;
} else {
continue;
}
}
if (strtolower($text) == $stopText) {
$match = false;
break 2;
}
}
continue;
}
if ($id != $from[$i][0]) {
$match = false;
break;
}
}
if ($match) {
if (strpos($toStr, '/*skip*/')) {
$buffer = str_replace(explode($skipCall, $fromStr), explode('/*skip*/', $toStr), $buffer);
} else {
$buffer = str_replace($fromStr, $toStr, $buffer);
}
}
$this->appendCode($buffer);
} else {
$this->appendCode($text);
}
}
return $this->code;
}
};
final class CodeFormatter {
private $passes = [];
private $debug = false;
public function __construct($debug = false) {
$this->debug = (bool) $debug;
}
public function addPass(FormatterPass $pass) {
$this->passes[] = $pass;
}
public function formatCode($source = '') {
gc_enable();
$passes = array_map(
function ($pass) {
return clone $pass;
},
$this->passes
);
while (($pass = array_shift($passes))) {
$source = $pass->format($source);
gc_collect_cycles();
}
gc_disable();
return $source;
}
}
if (!isset($testEnv)) {
$opts = getopt('ho:', ['from:', 'to:', 'help']);
if (isset($opts['h']) || isset($opts['help'])) {
echo 'Usage: ' . $argv[0] . ' [-ho] [--from=from --to=to] <target>', PHP_EOL;
$options = [
'--from=from, --to=to' => 'Search for "from" and replace with "to" - context aware search and replace',
'-h, --help' => 'this help message',
'-o=file' => 'output the formatted code to "file"',
];
$maxLen = max(array_map(function ($v) {
return strlen($v);
}, array_keys($options)));
foreach ($options as $k => $v) {
echo ' ', str_pad($k, $maxLen), ' ', $v, PHP_EOL;
}
echo PHP_EOL, 'If <target> is blank, it reads from stdin', PHP_EOL;
die();
}
if (isset($opts['from']) && !isset($opts['to'])) {
fwrite(STDERR, "Refactor must have --from and --to parameters" . PHP_EOL);
exit(255);
}
$debug = false;
$fmt = new CodeFormatter($debug);
if (isset($opts['from']) && isset($opts['to'])) {
$argv = array_values(
array_filter($argv,
function ($v) {
$param_from = '--from';
$param_to = '--to';
return substr($v, 0, strlen($param_from)) !== $param_from && substr($v, 0, strlen($param_to)) !== $param_to;
}
)
);
$fmt->addPass(new RefactorPass($opts['from'], $opts['to']));
}
if (isset($opts['o'])) {
unset($argv[1]);
unset($argv[2]);
$argv = array_values($argv);
file_put_contents($opts['o'], $fmt->formatCode(file_get_contents($argv[1])));
} elseif (isset($argv[1]) && is_file($argv[1])) {
echo $fmt->formatCode(file_get_contents($argv[1]));
} elseif (isset($argv[1]) && is_dir($argv[1])) {
$dir = new RecursiveDirectoryIterator($argv[1]);
$it = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($it, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
foreach ($files as $file) {
$file = $file[0];
echo $file;
$orig_code = file_get_contents($file);
$new_code = $fmt->formatCode($orig_code);
if ($orig_code != $new_code) {
file_put_contents($file . '-tmp', $new_code);
rename($file, $file . '~');
rename($file . '-tmp', $file);
}
echo PHP_EOL;
}
} else {
echo $fmt->formatCode(file_get_contents('php://stdin'));
}
}
;
__HALT_COMPILER();