[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/scripts/ -> code-style.pl (source)

   1  #!/usr/bin/perl -w
   2  
   3  use Pod::Usage;
   4  use Getopt::Long qw(GetOptions);
   5  Getopt::Long::Configure ("bundling");
   6  
   7  my %opt = (  "help" => 0,
   8      'debug' => 0,
   9    );
  10  
  11  if(!GetOptions(\%opt,
  12      'help|?',
  13      'debug',
  14      )) {
  15    pod2usage(-exitval => 1, 'verbose'=>0);
  16  }
  17  
  18  pod2usage(-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
  25  while (<>) {
  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  
 170  code-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  
 182  Originally written for Drupal (http://drupal.org/) to ensure stylish
 183  code.  This program reviews PHP code, and tries to show as many code
 184  improvements 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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7