forked from scribu/wp-phptidy
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathphptidy.php
2322 lines (1940 loc) · 55.5 KB
/
phptidy.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/php
<?php
/**
* phptidy
*
* See README for more information.
*
* PHP version >= 5.0
*
* @copyright 2003-2013 Magnus Rosenbaum
* @license GPL v2
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* @version 2.11 (2013-05-07)
* @author Magnus Rosenbaum <phptidy@cmr.cx>
* @package phptidy
*/
//////////////// DEFAULT CONFIGURATION ///////////////////
// You can overwrite all these settings in your configuration files.
// List of files in your project
// Wildcards for glob() may be used.
// Example: array("*.php", "inc/*.php");
$project_files = array();
// List of files you want to exclude from the project files
// Wildcards are not allowed here.
// Example: array("inc/external_lib.php");
$project_files_excludes = array();
// diff command
// Examples: "diff", "colordiff", "diff -u", "colordiff -u"
$diff = "colordiff -u";
// The automatically added author in the phpdoc file docblocks
// If left empty no new @author doctags will be added.
// Example: "Your Name <you@example.com>"
$default_author = "";
// Name of the automatically added @package doctag in the phpdoc file docblocks
// Example: "myproject"
$default_package = "default";
// String used for indenting
// If you indent with spaces you can use as much spaces as you like.
// Useful values: "\t" for indenting with tabs,
// " " for indenting with two spaces
$indent_char = "\t";
// Control structures with the opening curly brace on a new line
// Examples: false always on the same line
// true always on a new line
// array(T_CLASS, T_FUNCTION) for PEAR Coding Standards
$curly_brace_newline = false;
// PHP open tag
// All php open tags will be replaced by the here defined kind of open tag.
// Useful values: "<?", "<?php", "<?PHP"
$open_tag = "<?php";
// Check encoding
// If left empty the encoding will not be checked.
// See http://php.net/manual/en/ref.mbstring.html for a list of supported
// encodings.
// Examples: "ASCII", "UTF-8", "ISO-8859-1"
$encoding = "";
// Docroot-Variables
// phptidy will strip these variables and constants from the beginning of
// include and require commands to generate appropriate @see tags also for
// these files.
// Example: array('DOCROOT', '$docroot', '$GLOBALS[\'docroot\']');
$docrootvars = array();
// Enable the single cleanup functions
$fix_token_case = true;
$fix_builtin_functions_case = true;
$replace_inline_tabs = true;
$replace_phptags = true;
$replace_shell_comments = true;
$fix_statement_brackets = true;
$fix_separation_whitespace = true;
$fix_comma_space = true;
$add_file_docblock = true;
$add_function_docblocks = true;
$add_doctags = true;
$fix_docblock_format = true;
$fix_docblock_space = true;
$add_blank_lines = true;
$indent = true;
///////////// END OF DEFAULT CONFIGURATION ////////////////
define('CONFIGFILE', dirname(__FILE__) . "/.phptidy-config.php");
define('CACHEFILE', dirname(__FILE__) . "/.phptidy-cache");
error_reporting(E_ALL);
if (!version_compare(phpversion(), "5.0", ">=")) {
echo "Error: phptidy requires PHP 5 or newer.\n";
exit(1);
}
if (php_sapi_name() != "cli") {
echo "Error: phptidy has to be run on command line with CLI SAPI\n";
exit(1);
}
// Read command line
$command = "";
$files = array();
$options = array();
foreach ( $_SERVER['argv'] as $key => $value ) {
if ($key==0) continue;
if ($key==1) {
$command = $value;
continue;
}
if (substr($value, 0, 1)=="-") {
$options[] = $value;
} else {
$files[] = $value;
}
}
// Get command
switch ($command) {
case "help":
case "--help":
case "-h":
usage();
exit;
case "suffix":
case "replace":
case "diff":
case "source":
case "files":
case "tokens":
break;
default:
echo "Unknown command: '".$command."'\n";
case "":
usage();
exit(1);
}
// Get options
$verbose = false;
foreach ( $options as $option ) {
switch ($option) {
case "-v":
case "--verbose":
$verbose = true;
continue 2;
}
echo "Unknown option: '".$option."'\n";
usage();
exit(1);
}
// Load config file
if ( file_exists(CONFIGFILE) ) {
echo "Using configuration file ".CONFIGFILE."\n";
require CONFIGFILE;
} else {
echo "Running without configuration file\n";
}
// Files from config file
if (!count($files)) {
if (!count($project_files)) {
echo "Error: No files supplied on commandline and also no project files specified in config file\n";
exit(1);
}
foreach ( $project_files as $pf ) {
$files = array_unique(array_merge($files, glob($pf)));
}
}
// File excludes from config file
foreach ( $project_files_excludes as $file_exclude ) {
if (
($key = array_search($file_exclude, $files)) !== false
) unset($files[$key]);
}
// Check files
foreach ( $files as $key => $file ) {
// Ignore backups and results from phptidy
if (
substr($file, -12)==".phptidybak~" or
substr($file, -12)==".phptidy.php"
) {
unset($files[$key]);
continue;
}
if ( !is_readable($file) or !is_file($file) ) {
echo "Error: File '".$file."' does not exist or is not readable\n";
exit(1);
}
}
// Show files
if ($command=="files") {
print_r($files);
exit;
}
// Read cache file
if ( file_exists(CACHEFILE) ) {
echo "Using cache file ".CACHEFILE."\n";
$cache = unserialize(file_get_contents(CACHEFILE));
$cache_orig = $cache;
} else {
$cache = array(
'md5sums' => array(),
);
$cache_orig = false;
}
// Find functions and includes
echo "Find functions and includes ";
$functions = array();
$seetags = array();
foreach ( $files as $file ) {
echo ".";
$source = file_get_contents($file);
$functions = array_unique(array_merge($functions, get_functions($source)));
find_includes($seetags, $source, $file);
}
echo "\n";
//print_r($functions);
//print_r($seetags);
$md5sum = md5(serialize($functions).serialize($seetags));
if ( isset($cache['functions_seetags']) and $md5sum == $cache['functions_seetags'] ) {
// Use cache only if functions and seetags haven't changed
$use_cache = true;
} else {
$use_cache = false;
$cache['functions_seetags'] = $md5sum;
}
if ( !extension_loaded("tokenizer") ) {
echo "Error: The 'Tokenizer' extension for PHP is missing. See http://php.net/manual/en/book.tokenizer.php for more information.\n";
exit(1);
}
echo "Process files\n";
$replaced = 0;
foreach ( $files as $file ) {
echo " ".$file."\n";
$source_orig = file_get_contents($file);
// Cache
$md5sum = md5($source_orig);
if ( $use_cache and isset($cache['md5sums'][$file]) and $md5sum == $cache['md5sums'][$file] ) {
// Original file has not changed, so we don't process it
if ($verbose) echo " File unchanged since last processing.\n";
continue;
}
// Check encoding
if ($encoding and !mb_check_encoding($source_orig, $encoding)) {
echo " File contains characters which are not valid in ".$encoding.":\n";
$source_converted = mb_convert_encoding($source_orig, $encoding);
$tmpfile = "/tmp/tmp.phptidy.php";
if ( !file_put_contents($tmpfile, $source_converted) ) {
echo "Error: The temporary file '".$tmpfile."' could not be saved.\n";
exit(1);
}
system($diff." ".$file." ".$tmpfile." 2>&1");
}
// Process source code
$source = $source_orig;
$count = 0;
do {
$source_in = $source;
$source = phptidy($source_in);
++$count;
if ($count > 3) {
echo " Code processed 3 times and still not consistent!\n";
break;
}
} while ( $source != $source_in );
// Processing has not changed content of file
if ( $count == 1 ) {
if ($verbose) echo " Processed without changes.\n";
// Write md5sum of the unchanged file into cache
$cache['md5sums'][$file] = $md5sum;
continue;
}
// Output
switch ($command) {
case "suffix":
$newfile = $file.".phptidy.php";
if ( !file_put_contents($newfile, $source) ) {
echo "Error: The file '".$newfile."' could not be saved.\n";
exit(1);
}
echo " ".$newfile." saved.\n";
break;
case "replace":
$backupfile = dirname($file).(dirname($file)?"/":"").".".basename($file).".phptidybak~";
if ( !copy($file, $backupfile) ) {
echo "Error: The file '".$backupfile."' could not be saved.\n";
exit(1);
}
if ( !file_put_contents($file, $source) ) {
echo "Error: The file '".$file."' could not be overwritten.\n";
exit(1);
}
echo " replaced.\n";
++$replaced;
// Write new md5sum into cache
$cache['md5sums'][$file] = md5($source);
break;
case "diff":
$tmpfile = "/tmp/tmp.phptidy.php";
if ( !file_put_contents($tmpfile, $source) ) {
echo "Error: The temporary file '".$tmpfile."' could not be saved.\n";
exit(1);
}
system($diff." ".$file." ".$tmpfile." 2>&1");
break;
case "source":
echo $source;
break;
}
}
if ($command=="replace") {
if ($replaced) {
echo "Replaced ".$replaced." files.\n";
}
if ($cache != $cache_orig) {
echo "Write cache file ".CACHEFILE."\n";
if ( !file_put_contents(CACHEFILE, serialize($cache)) ) {
echo "Warning: The cache file '".CACHEFILE."' could not be saved.\n";
}
}
}
/////////////////// FUNCTIONS //////////////////////
/**
* Display usage information
*/
function usage() {
echo "
Usage: phptidy.php command [files|options]
Commands:
suffix Write output into files with suffix .phptidy.php
replace Replace files and backup original as .phptidybak
diff Show diff between old and new source
source Show processed source code of affected files
files Show files that would be processed
tokens Show source file tokens
help Display this message
Options:
-v Verbose messages
If no files are supplied on command line, they will be read from the config
file.
See README and source comments for more information.
";
}
/**
* Clean up source code
*
* @param string $source
* @return string
*/
function phptidy($source) {
// Replace non-Unix line breaks
// http://pear.php.net/manual/en/standards.file.php
// Windows line breaks -> Unix line breaks
$source = str_replace("\r\n", "\n", $source);
// Mac line breaks -> Unix line breaks
$source = str_replace("\r", "\n", $source);
$tokens = get_tokens($source);
if ($GLOBALS['command']=="tokens") {
print_tokens($tokens);
exit;
}
// Simple formatting
if ($GLOBALS['fix_token_case']) fix_token_case($tokens);
if ($GLOBALS['fix_builtin_functions_case']) fix_builtin_functions_case($tokens);
if ($GLOBALS['replace_inline_tabs']) replace_inline_tabs($tokens);
if ($GLOBALS['replace_phptags']) replace_phptags($tokens);
if ($GLOBALS['replace_shell_comments']) replace_shell_comments($tokens);
if ($GLOBALS['fix_statement_brackets']) fix_statement_brackets($tokens);
if ($GLOBALS['fix_separation_whitespace']) fix_separation_whitespace($tokens);
if ($GLOBALS['fix_comma_space']) fix_comma_space($tokens);
// PhpDocumentor
if ($GLOBALS['add_doctags']) {
list($usestags, $paramtags, $returntags) = collect_doctags($tokens);
//print_r($usestags);
//print_r($paramtags);
//print_r($returntags);
}
if ($GLOBALS['add_file_docblock']) add_file_docblock($tokens);
if ($GLOBALS['add_function_docblocks']) add_function_docblocks($tokens);
if ($GLOBALS['add_doctags']) {
add_doctags($tokens, $usestags, $paramtags, $returntags, $GLOBALS['seetags']);
}
if ($GLOBALS['fix_docblock_format']) fix_docblock_format($tokens);
if ($GLOBALS['fix_docblock_space']) fix_docblock_space($tokens);
if ($GLOBALS['add_blank_lines']) add_blank_lines($tokens);
// Indenting
if ($GLOBALS['indent']) {
indent($tokens);
strip_closetag_indenting($tokens);
}
$source = combine_tokens($tokens);
// Strip trailing whitespace
$source = preg_replace("/[ \t]+\n/", "\n", $source);
if ( substr($source, -1)!="\n" ) {
// Add one line break at the end of the file
// http://pear.php.net/manual/en/standards.file.php
$source .= "\n";
} else {
// Strip empty lines at the end of the file
while ( substr($source, -2)=="\n\n" ) $source = substr($source, 0, -1);
}
return $source;
}
//////////////// TOKEN FUNCTIONS ///////////////////
/**
* Returns the text part of a token
*
* @param mixed $token
* @return string
*/
function token_text($token) {
if (is_string($token)) return $token;
return $token[1];
}
/**
* Prints all tokens
*
* @param array $tokens
*/
function print_tokens($tokens) {
foreach ( $tokens as $token ) {
if (is_string($token)) {
echo $token."\n";
} else {
list($id, $text) = $token;
echo token_name($id)." ".addcslashes($text, "\0..\40!@\@\177..\377")."\n";
}
}
}
/**
* Wrapper for token_get_all(), because there is new mysterious index 2 ...
*
* @param string $source
* @return array
*/
function get_tokens(&$source) {
$tokens = token_get_all($source);
foreach ( $tokens as &$token ) {
if (isset($token[2])) unset($token[2]);
}
return $tokens;
}
/**
* Combines the tokens to the source code
*
* @param array $tokens
* @return string
*/
function combine_tokens($tokens) {
$out = "";
foreach ( $tokens as $key => $token ) {
if (is_string($token)) {
$out .= $token;
} else {
$out .= $token[1];
}
}
return $out;
}
/**
* Displays a possible syntax error
*
* @param array $tokens
* @param integer $key
* @param string $message (optional)
*/
function possible_syntax_error($tokens, $key, $message="") {
echo "Possible syntax error detected";
if ($message) echo " (".$message.")";
echo ":\n";
echo combine_tokens(array_slice($tokens, max(0, $key-5), 10))."\n";
}
/**
* Removes whitespace from the beginning of a token array
*
* @param array $tokens
*/
function tokens_ltrim(&$tokens) {
while (
isset($tokens[0][0]) and
$tokens[0][0] === T_WHITESPACE
) {
array_splice($tokens, 0, 1);
}
}
/**
* Removes whitespace from the end of a token array
*
* @param array $tokens (reference)
*/
function tokens_rtrim(&$tokens) {
while (
isset($tokens[$k=count($tokens)-1][0]) and
$tokens[$k][0] === T_WHITESPACE
) {
array_splice($tokens, -1);
}
}
/**
* Removes all whitespace
*
* @param array $tokens (reference)
*/
function strip_whitespace(&$tokens) {
foreach ( $tokens as $key => $token ) {
if (
isset($token[0]) and
$token[0] === T_WHITESPACE
) {
unset($tokens[$key]);
}
}
$tokens = array_values($tokens);
}
/**
* Gets the argument of a statement
*
* @param array $tokens
* @param integer $key Key of the token of the command for which we want the argument
* @return array
*/
function get_argument_tokens(&$tokens, $key) {
$tokens_arg = array();
$round_braces_count = 0;
$curly_braces_count = 0;
++$key;
while ( isset($tokens[$key]) ) {
$token = &$tokens[$key];
if (is_string($token)) {
if ($token === ";") break;
} else {
if ($token[0] === T_CLOSE_TAG) break;
}
if ($token === "(") {
++$round_braces_count;
} elseif ($token === ")") {
--$round_braces_count;
} elseif (
$token === "{" or (
is_array($token) and (
$token[0] === T_CURLY_OPEN or
$token[0] === T_DOLLAR_OPEN_CURLY_BRACES
)
)
) {
++$curly_braces_count;
} elseif ($token === "}") {
--$curly_braces_count;
}
if ( $round_braces_count < 0 or $round_braces_count < 0 ) break;
$tokens_arg[] = $token;
++$key;
}
return $tokens_arg;
}
//////////////// FORMATTING FUNCTIONS ///////////////////
/**
* Checks for some tokens which must not be touched
*
* @param array $token
* @return boolean
*/
function token_is_taboo(&$token) {
return (
// Do not touch HTML content
$token[0] === T_INLINE_HTML or
$token[0] === T_CLOSE_TAG or
// Do not touch the content of strings
$token[0] === T_CONSTANT_ENCAPSED_STRING or
$token[0] === T_ENCAPSED_AND_WHITESPACE or
// Do not touch the content of multiline comments
($token[0] === T_COMMENT and substr($token[1], 0, 2) === "/*")
);
}
/**
* Converts commands to lower case
*
* @param array $tokens (reference)
*/
function fix_token_case(&$tokens) {
static $lower_case_tokens = array(
T_ABSTRACT,
T_ARRAY,
T_ARRAY_CAST,
T_AS,
T_BOOL_CAST,
T_BREAK,
T_CASE,
T_CATCH,
T_CLASS,
T_CLONE,
T_CONST,
T_CONTINUE,
T_DECLARE,
T_DEFAULT,
T_DO,
T_DOUBLE_CAST,
T_ECHO,
T_ELSE,
T_ELSEIF,
T_EMPTY,
T_ENDDECLARE,
T_ENDFOR,
T_ENDFOREACH,
T_ENDIF,
T_ENDSWITCH,
T_ENDWHILE,
T_EVAL,
T_EXIT,
T_EXTENDS,
T_FINAL,
T_FOR,
T_FOREACH,
T_FUNCTION,
T_GLOBAL,
T_IF,
T_IMPLEMENTS,
T_INCLUDE,
T_INCLUDE_ONCE,
T_INSTANCEOF,
T_INT_CAST,
T_INTERFACE,
T_ISSET,
T_LIST,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_NEW,
T_OBJECT_CAST,
T_PRINT,
T_PRIVATE,
T_PUBLIC,
T_PROTECTED,
T_REQUIRE,
T_REQUIRE_ONCE,
T_RETURN,
T_STATIC,
T_STRING_CAST,
T_SWITCH,
T_THROW,
T_TRY,
T_UNSET,
T_UNSET_CAST,
T_VAR,
T_WHILE
);
foreach ( $tokens as &$token ) {
if (is_string($token)) continue;
if ($token[1] === strtolower($token[1])) continue;
if (in_array($token[0], $lower_case_tokens)) {
$token[1] = strtolower($token[1]);
}
}
}
/**
* Converts builtin functions to lower case
*
* @param array $tokens (reference)
*/
function fix_builtin_functions_case(&$tokens) {
static $defined_internal_functions = false;
if ($defined_internal_functions === false) {
$defined_functions = get_defined_functions();
$defined_internal_functions = $defined_functions['internal'];
}
foreach ( $tokens as $key => &$token ) {
if (
is_string($token) or
$token[0] !== T_STRING or
!isset($tokens[$key+2]) or
// Ignore object methods
(is_array($tokens[$key-1]) and $tokens[$key-1][0] === T_OBJECT_OPERATOR)
) continue;
if (
$tokens[$key+1] === "("
) {
$lowercase = strtolower($token[1]);
if (
$token[1] !== $lowercase and
in_array($lowercase, $defined_internal_functions)
) {
$token[1] = $lowercase;
}
} elseif (
$tokens[$key+2] === "(" and
is_array($tokens[$key+1]) and $tokens[$key+1][0] === T_WHITESPACE
) {
if (
in_array(strtolower($token[1]), $defined_internal_functions)
) {
$token[1] = strtolower($token[1]);
// Remove whitespace between function name and opening round bracket
unset($tokens[$key+1]);
}
}
}
$tokens = array_values($tokens);
}
/**
* Replaces inline tabs with spaces
*
* @param array $tokens (reference)
*/
function replace_inline_tabs(&$tokens) {
foreach ( $tokens as &$token ) {
if ( is_string($token) ) {
$text =& $token;
} else {
if (token_is_taboo($token)) continue;
$text =& $token[1];
}
// Replace one tab with one space
$text = str_replace("\t", " ", $text);
}
}
/**
* Replaces PHP-Open-Tags with consistent tags
*
* @param array $tokens (reference)
*/
function replace_phptags(&$tokens) {
foreach ( $tokens as $key => &$token ) {
if (is_string($token)) continue;
switch ($token[0]) {
case T_OPEN_TAG:
// The open tag is already the right one
if ( rtrim($token[1]) == $GLOBALS['open_tag'] ) continue;
// Collect following whitespace
preg_match("/\s*$/", $token[1], $matches);
$whitespace = $matches[0];
if ( $tokens[$key+1][0] === T_WHITESPACE ) {
$whitespace .= $tokens[$key+1][1];
array_splice($tokens, $key+1, 1);
}
if ($GLOBALS['open_tag']=="<?") {
// Short open tags have the following whitespace in a seperate token
array_splice($tokens, $key, 1, array(
array(T_OPEN_TAG, $GLOBALS['open_tag']),
array(T_WHITESPACE, $whitespace)
));
} else {
// Long open tags have the following whitespace included in the token string
switch (strlen($whitespace)) {
case 0:
// Add an additional space if no whitespace is found
$whitespace = " ";
case 1:
// Use the one found space or newline
$tokens[$key][1] = $GLOBALS['open_tag'].$whitespace;
break;
default:
// Use the first space or newline for the open tag and append the rest of the whitespace as a seperate token
array_splice($tokens, $key, 1, array(
array(T_OPEN_TAG, $GLOBALS['open_tag'].substr($whitespace, 0, 1)),
array(T_WHITESPACE, substr($whitespace, 1))
));
}
}
break;
case T_OPEN_TAG_WITH_ECHO:
// If we use short tags we also accept the echo tags
if ($GLOBALS['open_tag']=="<?") continue;
if ( $tokens[$key+1][0] === T_WHITESPACE ) {
// If there is already whitespace following we only replace the open tag
array_splice($tokens, $key, 1, array(
array(T_OPEN_TAG, $GLOBALS['open_tag']." "),
array(T_ECHO, "echo")
));
} else {
// If there is no whitespace following we add one space
array_splice($tokens, $key, 1, array(
array(T_OPEN_TAG, $GLOBALS['open_tag']." "),
array(T_ECHO, "echo"),
array(T_WHITESPACE, " ")
));
}
}
}
}
/**
* Replaces shell style comments with C style comments
*
* http://pear.php.net/manual/en/standards.comments.php
*
* @param array $tokens (reference)
*/
function replace_shell_comments(&$tokens) {
foreach ( $tokens as &$token ) {
if (is_string($token)) continue;
if (
$token[0] === T_COMMENT and
substr($token[1], 0, 1) === "#"
) {
$token[1] = "//".substr($token[1], 1);
}
}
}
/**
* Enforces statements without brackets and fixes whitespace
*
* http://pear.php.net/manual/en/standards.including.php
*
* @param array $tokens (reference)
*/
function fix_statement_brackets(&$tokens) {
static $statement_tokens = array(
T_INCLUDE,
T_INCLUDE_ONCE,
T_REQUIRE,
T_REQUIRE_ONCE,
T_RETURN,
T_BREAK,
T_CONTINUE,
T_ECHO
);
foreach ( $tokens as $key => &$token ) {
if ( is_string($token) or !in_array($token[0], $statement_tokens) ) continue;
$tokens_arg = get_argument_tokens($tokens, $key);
$tokens_arg_orig = $tokens_arg;
tokens_ltrim($tokens_arg);
if ( !count($tokens_arg) or $tokens_arg[0] !== "(" ) continue;
tokens_rtrim($tokens_arg);
// Check if the opening bracket has a matching one at the end of the expression
$round_braces_count = 0;
foreach ( $tokens_arg as $k => $t ) {
if (is_string($t)) {
if ($t === "(") ++$round_braces_count;
elseif ($t === ")") --$round_braces_count;
else continue;
// Check if the expression begins without a bracket or if the bracket was closed before the end of the expression was reached
if ( $round_braces_count == 0 and $k != count($tokens_arg)-1 ) {
continue 2;
}
if ( $round_braces_count < 0 ) {
possible_syntax_error($tokens, $key, "Closing round bracket found which has not been opened");
continue 2;
}
} else {
// Do not touch multiline expressions
if ($t[0] === T_WHITESPACE and strpos($t[1], "\n")!==false) {