2 # $Id: code-style.pl 144 2007-03-28 07:52:20Z thierry $
4 # Author: Alexander Schwartz (alexander.schwartz@gmx.net)
6 # First version: 2001-10-15
8 # Originally written for Drupal (http://drupal.org/) to ensure stylish
9 # code. This program tries to show as many improvements as possible with
12 # $Id: code-style.pl 144 2007-03-28 07:52:20Z thierry $
16 if ($ARGV[0] eq '-debug') {
26 # please don't use nested comments for now... thanks!
27 # handles comments // style, but don't mess with http://
29 # handles comments /**/ on a single line
31 # handles comments /**/ over several lines
54 # enforce "bar". foo() ."bar" syntax
55 if (/^("[^"]*"|[^"])*("[^"]*")\.[^ ]/ && $program) {
56 $msg = "'\".' -> '\". '";
58 elsif (/^("[^"]*"|[^"])*("[^"]*")\s+\./ && $program) {
59 $msg = "'\" .' -> '\".'";
61 # enforce "bar". foo() ."bar" syntax
62 elsif (/^("[^"]*"|[^"])*[^ "]\.("[^"]*")/ && $program) {
63 $msg = "'.\"' -> '.\"'";
65 elsif (/^("[^"]*"|[^"])*[^ "]\.\s+("[^"]*")/ && $program) {
66 $msg = "'. \"' -> '.\"'";
68 # XHTML requires closing tag
70 $msg = "'<br>' -> '<br />'";
72 elsif (/\$REQUEST_URI/i) {
73 $msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
75 elsif (/\"REQUEST_URI\"/i) {
76 $msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
79 # XHTML compatibility mode suggests a blank before /
81 elsif (/<[a-z][^>]*[^ >]\/>/i) {
82 $msg = "'<foo/".">' -> '<foo />'";
84 # we write '{' on the same line, not on the next
85 elsif (/^\s*{/ && $program) {
86 $msg = "take '{' to previous line";
88 elsif (/([a-z])([A-Z])/) {
89 $msg = "no mixed case function or variable names, use lower case and _";
91 elsif (/<[\/]*[A-Z]+[^>]*>/) {
92 $msg = "XHTML demands tags to be lowercase";
95 # trying to recognize splitted lines
96 # there are only a few valid last characters in programming mode,
97 # only sometimes it is ( if you use if/else with a single statement
99 # from here on we need no more strings
100 while (s/^([^"]*)"[^"]*"/$1#/) {};
101 while (s/^([^']*)'[^']*'/$1#/) {};
103 # it should be 'if (' all the time
104 if (/(^|[^a-zA-Z])(if|else|elseif|while|foreach|switch|return|for)\(/) {
105 $msg = "'(' -> ' ('";
107 #elsif (/[^;{}:\s\n]\s*\n*$/ && $program && !/^[\s}]*(if|else)/) {
108 # $msg = "don't split lines";
110 elsif (/\}\s*else/) {
111 $msg = "'} else' -> '}\\nelse'";
113 elsif (/[^{\s\n]\s*\n*$/ && $program && /^\s*(if|else)/) {
114 $msg = "every if/else needs a { at eol";
116 elsif (/([\(\[]) / && $program) {
117 $msg = "'$1 ' -> '$1'";
119 elsif (/ ([\)\]])/ && $program) {
120 $msg = "' $1' -> '$1'";
123 elsif (/([a-z-A-Z_][a-zA-Z0-9_-]*)\s+\(/ && $program) {
124 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") {
125 $msg = "'$1 (' -> '$1('";
128 # there should be a space before '{'
129 if (/[^ ]{/ && $program) {
130 $msg = "missing space before '{'";
132 # there should be a space after ','
133 elsif (/[,][^ \n\r]/ && $program) {
134 $msg = "missing space after ','";
136 # spaces before and after, only foreach may use $foo=>bar
137 elsif (/[^ =|\-|\+](\+|\-)[^ =>|\-|\+]/ && $program && !/foreach/) {
138 $msg = "'$1' -> ' $1 '";
140 elsif (/[^ =](\*|==|\.=|=>|=|\|\|)[^ =>]/ && $program && !/foreach/) {
141 $msg = "'$1' -> ' $1 '";
143 # ensure $bar["foo"] and $bar[$foo] and $bar[0]
144 elsif (/\[[^#][^\]]*\]/ && !/\[[0-9\$][^\]]*\]/ && !/\[\]/) {
145 $msg = "only [\"foo\"], [\$foo] or [0] is allowed";
147 # first try to find missing quotes after = in (X)HTML tags
148 elsif (/<[^>]*=[a-zA-Z0-9][^>]*>/) {
149 $msg = "=... -> =\"...\"";
153 print $ARGV .":". $. .": $msg : ". $org;