2001-10-16 20:59:27 +00:00
|
|
|
#!/usr/bin/perl -w
|
2002-11-06 13:54:08 +00:00
|
|
|
# $Id$
|
2001-10-16 20:59:27 +00:00
|
|
|
|
2007-02-15 07:15:53 +00:00
|
|
|
use Pod::Usage;
|
|
|
|
use Getopt::Long qw(GetOptions);
|
|
|
|
Getopt::Long::Configure ("bundling");
|
2001-10-16 20:59:27 +00:00
|
|
|
|
2007-02-15 07:15:53 +00:00
|
|
|
my %opt = ( "help" => 0,
|
|
|
|
'debug' => 0,
|
|
|
|
);
|
2001-10-16 20:59:27 +00:00
|
|
|
|
2007-02-15 07:15:53 +00:00
|
|
|
if(!GetOptions(\%opt,
|
|
|
|
'help|?',
|
|
|
|
'debug',
|
|
|
|
)) {
|
|
|
|
pod2usage(-exitval => 1, 'verbose'=>0);
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
2007-02-15 07:15:53 +00:00
|
|
|
|
|
|
|
pod2usage(-exitval => 0, -verbose => 2) if($opt{'help'});
|
|
|
|
|
|
|
|
$debug = $opt{'debug'};
|
|
|
|
|
|
|
|
$comment = 0; #flag used to signal we're inside /* */
|
|
|
|
$program = 0; #flag used to signal we're inside <?php ?>
|
|
|
|
#read the file
|
2001-10-16 20:59:27 +00:00
|
|
|
while (<>) {
|
|
|
|
$org=$_;
|
|
|
|
s/\\["']//g;
|
|
|
|
# please don't use nested comments for now... thanks!
|
|
|
|
# handles comments // style, but don't mess with http://
|
2001-10-23 18:20:43 +00:00
|
|
|
s/\/\/[^:].*//;
|
2001-10-16 20:59:27 +00:00
|
|
|
# handles comments /**/ on a single line
|
|
|
|
s/\/\*.*\*\///g;
|
|
|
|
# handles comments /**/ over several lines
|
|
|
|
if ($comment == 1) {
|
|
|
|
if (s/.*\*\///) {
|
2001-10-17 18:18:35 +00:00
|
|
|
$comment = 0;
|
|
|
|
}
|
2001-10-16 20:59:27 +00:00
|
|
|
else {
|
2001-10-17 18:18:35 +00:00
|
|
|
next;
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s/\/\*.*//) {
|
|
|
|
$comment = 1;
|
|
|
|
}
|
|
|
|
if (/^\s*#/) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s/<\?php//) {
|
|
|
|
$program = 1;
|
|
|
|
}
|
|
|
|
if (/\?>/) {
|
|
|
|
$program = 0;
|
|
|
|
}
|
2001-10-17 18:18:35 +00:00
|
|
|
|
2005-03-31 21:18:08 +00:00
|
|
|
# enforce "bar". foo() ."bar" syntax
|
2001-10-16 20:59:27 +00:00
|
|
|
if (/^("[^"]*"|[^"])*("[^"]*")\.[^ ]/ && $program) {
|
|
|
|
$msg = "'\".' -> '\". '";
|
|
|
|
}
|
|
|
|
elsif (/^("[^"]*"|[^"])*("[^"]*")\s+\./ && $program) {
|
|
|
|
$msg = "'\" .' -> '\".'";
|
|
|
|
}
|
2005-03-31 21:18:08 +00:00
|
|
|
# enforce "bar". foo() ."bar" syntax
|
2001-10-16 20:59:27 +00:00
|
|
|
elsif (/^("[^"]*"|[^"])*[^ "]\.("[^"]*")/ && $program) {
|
|
|
|
$msg = "'.\"' -> '.\"'";
|
|
|
|
}
|
|
|
|
elsif (/^("[^"]*"|[^"])*[^ "]\.\s+("[^"]*")/ && $program) {
|
|
|
|
$msg = "'. \"' -> '.\"'";
|
|
|
|
}
|
|
|
|
# XHTML requires closing tag
|
2001-10-23 18:20:43 +00:00
|
|
|
elsif (/<br>/i) {
|
|
|
|
$msg = "'<br>' -> '<br />'";
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
2003-12-28 10:40:17 +00:00
|
|
|
elsif (/\$REQUEST_URI/i) {
|
|
|
|
$msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
|
2003-06-06 21:37:11 +00:00
|
|
|
}
|
2003-12-28 10:40:17 +00:00
|
|
|
elsif (/\"REQUEST_URI\"/i) {
|
|
|
|
$msg = "the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead";
|
2003-06-06 21:37:11 +00:00
|
|
|
}
|
|
|
|
|
2001-10-17 18:18:35 +00:00
|
|
|
# XHTML compatibility mode suggests a blank before /
|
2001-10-16 20:59:27 +00:00
|
|
|
# i.e. <br />
|
|
|
|
elsif (/<[a-z][^>]*[^ >]\/>/i) {
|
|
|
|
$msg = "'<foo/".">' -> '<foo />'";
|
|
|
|
}
|
|
|
|
# we write '{' on the same line, not on the next
|
|
|
|
elsif (/^\s*{/ && $program) {
|
2001-10-17 18:18:35 +00:00
|
|
|
$msg = "take '{' to previous line";
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
2003-10-30 20:56:17 +00:00
|
|
|
elsif (/([a-z])([A-Z])/) {
|
|
|
|
$msg = "no mixed case function or variable names, use lower case and _";
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
2003-10-30 20:56:17 +00:00
|
|
|
elsif (/<[\/]*[A-Z]+[^>]*>/) {
|
|
|
|
$msg = "XHTML demands tags to be lowercase";
|
|
|
|
}
|
|
|
|
|
2001-10-16 20:59:27 +00:00
|
|
|
# trying to recognize splitted lines
|
|
|
|
# there are only a few valid last characters in programming mode,
|
|
|
|
# only sometimes it is ( if you use if/else with a single statement
|
2001-10-17 18:18:35 +00:00
|
|
|
|
2001-10-16 20:59:27 +00:00
|
|
|
# from here on we need no more strings
|
|
|
|
while (s/^([^"]*)"[^"]*"/$1#/) {};
|
|
|
|
while (s/^([^']*)'[^']*'/$1#/) {};
|
|
|
|
|
|
|
|
# it should be 'if (' all the time
|
|
|
|
if (/(^|[^a-zA-Z])(if|else|elseif|while|foreach|switch|return|for)\(/) {
|
2001-10-23 18:20:43 +00:00
|
|
|
$msg = "'(' -> ' ('";
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
2003-12-28 10:40:17 +00:00
|
|
|
#elsif (/[^;{}:\s\n]\s*\n*$/ && $program && !/^[\s}]*(if|else)/) {
|
|
|
|
# $msg = "don't split lines";
|
|
|
|
#}
|
2001-10-16 20:59:27 +00:00
|
|
|
elsif (/\}\s*else/) {
|
|
|
|
$msg = "'} else' -> '}\\nelse'";
|
|
|
|
}
|
|
|
|
elsif (/[^{\s\n]\s*\n*$/ && $program && /^\s*(if|else)/) {
|
|
|
|
$msg = "every if/else needs a { at eol";
|
|
|
|
}
|
|
|
|
elsif (/([\(\[]) / && $program) {
|
|
|
|
$msg = "'$1 ' -> '$1'";
|
|
|
|
}
|
2006-10-14 10:03:27 +00:00
|
|
|
elsif (/\S ([\)\]])/ && $program) {
|
2001-10-16 20:59:27 +00:00
|
|
|
$msg = "' $1' -> '$1'";
|
|
|
|
}
|
2001-10-17 18:18:35 +00:00
|
|
|
# but no brackets
|
2001-10-16 20:59:27 +00:00
|
|
|
elsif (/([a-z-A-Z_][a-zA-Z0-9_-]*)\s+\(/ && $program) {
|
|
|
|
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") {
|
|
|
|
$msg = "'$1 (' -> '$1('";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# there should be a space before '{'
|
2001-10-23 18:20:43 +00:00
|
|
|
if (/[^ ]{/ && $program) {
|
2001-10-16 20:59:27 +00:00
|
|
|
$msg = "missing space before '{'";
|
|
|
|
}
|
|
|
|
# there should be a space after ','
|
2001-11-17 15:44:21 +00:00
|
|
|
elsif (/[,][^ \n\r]/ && $program) {
|
2001-10-16 20:59:27 +00:00
|
|
|
$msg = "missing space after ','";
|
|
|
|
}
|
|
|
|
# spaces before and after, only foreach may use $foo=>bar
|
2005-12-28 12:04:14 +00:00
|
|
|
elsif (/[^ =|\-|\+](\+|\-)[^ =>|\-|\+]/ && $program && !/foreach/) {
|
2001-11-17 15:44:21 +00:00
|
|
|
$msg = "'$1' -> ' $1 '";
|
|
|
|
}
|
|
|
|
elsif (/[^ =](\*|==|\.=|=>|=|\|\|)[^ =>]/ && $program && !/foreach/) {
|
2001-10-23 18:20:43 +00:00
|
|
|
$msg = "'$1' -> ' $1 '";
|
|
|
|
}
|
|
|
|
# ensure $bar["foo"] and $bar[$foo] and $bar[0]
|
|
|
|
elsif (/\[[^#][^\]]*\]/ && !/\[[0-9\$][^\]]*\]/ && !/\[\]/) {
|
|
|
|
$msg = "only [\"foo\"], [\$foo] or [0] is allowed";
|
|
|
|
}
|
|
|
|
# first try to find missing quotes after = in (X)HTML tags
|
|
|
|
elsif (/<[^>]*=[a-zA-Z0-9][^>]*>/) {
|
|
|
|
$msg = "=... -> =\"...\"";
|
2001-10-16 20:59:27 +00:00
|
|
|
}
|
|
|
|
if (defined $msg) {
|
|
|
|
if ($debug==0) {
|
|
|
|
print $ARGV .":". $. .": $msg : ". $org;
|
|
|
|
}
|
|
|
|
undef $msg;
|
2001-10-17 18:18:35 +00:00
|
|
|
}
|
2001-10-16 20:59:27 +00:00
|
|
|
elsif ($debug==1) {
|
|
|
|
print $org;
|
|
|
|
}
|
|
|
|
} continue {
|
|
|
|
close ARGV if eof;
|
|
|
|
}
|
2007-02-15 07:15:53 +00:00
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
code-style.pl - Review drupal code for style
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
code-style.pl [options] <filename>
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-? --help detailed help message
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Originally written for Drupal (http://drupal.org/) to ensure stylish
|
2007-02-15 11:40:19 +00:00
|
|
|
code. This program reviews PHP code, and tries to show as many code
|
2007-02-15 07:15:53 +00:00
|
|
|
improvements as possible with no false positives.
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
--comment
|
|
|
|
|
|
|
|
=head1 EXAMPLES
|
|
|
|
|
|
|
|
./code-style.pl ../index.php
|
|
|
|
|
|
|
|
=cut
|