source: sipes/cord/scripts/code-style.pl

stableversion-3.0
Last change on this file was b354002, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

  • Propiedad mode establecida a 100755
File size: 4.8 KB
Línea 
1#!/usr/bin/perl -w
2
3use Pod::Usage;
4use Getopt::Long qw(GetOptions);
5Getopt::Long::Configure ("bundling");
6
7my %opt = (  "help" => 0,
8    'debug' => 0,
9  );
10
11if(!GetOptions(\%opt,
12    'help|?',
13    'debug',
14    )) {
15  pod2usage(-exitval => 1, 'verbose'=>0);
16}
17
18pod2usage(-exitval => 0, -verbose => 2) if($opt{'help'});
19
20$debug = $opt{'debug'};
21
22$comment = 0; #flag used to signal we're inside /* */
23$program = 0; #flag used to signal we're inside <?php ?>
24#read the file
25while (<>) {
26  $org=$_;
27  s/\\["']//g;
28  # please don't use nested comments for now... thanks!
29  # handles comments // style, but don't mess with http://
30  s/\/\/[^:].*//;
31  # handles comments /**/ on a single line
32  s/\/\*.*\*\///g;
33  # handles comments /**/ over several lines
34  if ($comment == 1) {
35    if (s/.*\*\///) {
36      $comment = 0;
37    }
38    else {
39      next;
40    }
41  }
42  if (s/\/\*.*//) {
43    $comment = 1;
44  }
45  if (/^\s*#/) {
46    next;
47  }
48
49  if (s/<\?php//) {
50    $program = 1;
51  }
52  if (/\?>/) {
53    $program = 0;
54  }
55
56  # enforce "bar". foo() ."bar" syntax
57  if (/^("[^"]*"|[^"])*("[^"]*")\.[^ ]/ && $program) {
58    $msg = "'\".' -> '\". '";
59  }
60  elsif (/^("[^"]*"|[^"])*("[^"]*")\s+\./ && $program) {
61    $msg = "'\" .' -> '\".'";
62  }
63  # enforce "bar". foo() ."bar" syntax
64  elsif (/^("[^"]*"|[^"])*[^ "]\.("[^"]*")/ && $program) {
65    $msg = "'.\"' -> '.\"'";
66  }
67  elsif (/^("[^"]*"|[^"])*[^ "]\.\s+("[^"]*")/ && $program) {
68    $msg = "'. \"' -> '.\"'";
69  }
70  # XHTML requires closing tag
71  elsif (/<br>/i) {
72    $msg = "'<br>' -> '<br />'";
73  }
74  elsif (/\$REQUEST_URI/i) {
75    $msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
76  }
77  elsif (/\"REQUEST_URI\"/i) {
78    $msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
79  }
80
81  # XHTML compatibility mode suggests a blank before /
82  # i.e. <br />
83  elsif (/<[a-z][^>]*[^ >]\/>/i) {
84    $msg = "'<foo/".">' -> '<foo />'";
85  }
86  # we write '{' on the same line, not on the next
87  elsif (/^\s*{/ && $program) {
88    $msg = "take '{' to previous line";
89  }
90  elsif (/([a-z])([A-Z])/) {
91    $msg = "no mixed case function or variable names, use lower case and _";
92  }
93  elsif (/<[\/]*[A-Z]+[^>]*>/) {
94    $msg = "XHTML demands tags to be lowercase";
95  }
96
97  # trying to recognize splitted lines
98  # there are only a few valid last characters in programming mode,
99  # only sometimes it is ( if you use if/else with a single statement
100
101  # from here on we need no more strings
102  while (s/^([^"]*)"[^"]*"/$1#/) {};
103  while (s/^([^']*)'[^']*'/$1#/) {};
104
105  # it should be 'if (' all the time
106  if (/(^|[^a-zA-Z])(if|else|elseif|while|foreach|switch|return|for)\(/) {
107    $msg = "'(' -> ' ('";
108  }
109  #elsif (/[^;{}:\s\n]\s*\n*$/ && $program && !/^[\s}]*(if|else)/) {
110  #  $msg = "don't split lines";
111  #}
112  elsif (/\}\s*else/) {
113    $msg = "'} else' -> '}\\nelse'";
114  }
115  elsif (/[^{\s\n]\s*\n*$/ && $program && /^\s*(if|else)/) {
116    $msg = "every if/else needs a { at eol";
117  }
118  elsif (/([\(\[]) / && $program) {
119    $msg = "'$1 ' -> '$1'";
120  }
121  elsif (/\S ([\)\]])/ && $program) {
122    $msg = "' $1' -> '$1'";
123  }
124  # but no brackets
125  elsif (/([a-z-A-Z_][a-zA-Z0-9_-]*)\s+\(/ && $program) {
126    if ($1 ne "switch" and $1 ne "if" and $1 ne "while" and $1 ne "foreach" and $1 ne "return" and $1 ne "for" and $1 ne "elseif") {
127      $msg = "'$1 (' -> '$1('";
128    }
129  }
130  # there should be a space before '{'
131  if (/[^ ]{/ && $program) {
132    $msg = "missing space before '{'";
133  }
134  # there should be a space after ','
135  elsif (/[,][^ \n\r]/ && $program) {
136    $msg = "missing space after ','";
137  }
138  # spaces before and after, only foreach may use $foo=>bar
139  elsif (/[^ =|\-|\+](\+|\-)[^ =>|\-|\+]/ && $program && !/foreach/) {
140    $msg = "'$1' -> ' $1 '";
141  }
142  elsif (/[^ =](\*|==|\.=|=>|=|\|\|)[^ =>]/ && $program && !/foreach/) {
143    $msg = "'$1' -> ' $1 '";
144  }
145  # ensure $bar["foo"] and $bar[$foo] and $bar[0]
146  elsif (/\[[^#][^\]]*\]/ && !/\[[0-9\$][^\]]*\]/ && !/\[\]/) {
147    $msg = "only [\"foo\"], [\$foo] or [0] is allowed";
148  }
149  # first try to find missing quotes after = in (X)HTML tags
150  elsif (/<[^>]*=[a-zA-Z0-9][^>]*>/) {
151    $msg = "=... -> =\"...\"";
152  }
153  if (defined $msg) {
154    if ($debug==0) {
155      print $ARGV .":". $. .": $msg : ". $org;
156    }
157    undef $msg;
158  }
159  elsif ($debug==1) {
160    print $org;
161  }
162} continue {
163  close ARGV if eof;
164}
165
166__END__
167
168=head1 NAME
169
170code-style.pl - Review drupal code for style
171
172=head1 SYNOPSIS
173
174  code-style.pl [options] <filename>
175
176  Options:
177
178  -? --help  detailed help message
179
180=head1 DESCRIPTION
181
182Originally written for Drupal (http://drupal.org/) to ensure stylish
183code.  This program reviews PHP code, and tries to show as many code
184improvements as possible with no false positives.
185
186=head1 OPTIONS
187
188  --comment
189
190=head1 EXAMPLES
191
192 ./code-style.pl ../index.php
193
194=cut
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.