Index: scripts/coder_format/coder_format.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/coder_format.inc,v
retrieving revision 1.2.4.5
diff -u -p -r1.2.4.5 coder_format.inc
--- scripts/coder_format/coder_format.inc	16 Jan 2008 22:26:33 -0000	1.2.4.5
+++ scripts/coder_format/coder_format.inc	20 Jan 2008 02:28:44 -0000
@@ -43,7 +43,7 @@ function coder_format_recursive($root, $
   
   if (!$undo) {
     // Fetch files to process.
-    $mask = '.module$|.inc$|.install|.profile$';
+    $mask = '\.php$|\.module$|\.inc$|\.install|\.profile$';
     $nomask = array('.', '..', 'CVS', '.svn');
     $files = file_scan_directory($root, $mask, $nomask, 0, true);
     foreach ($files as $file) {
@@ -88,17 +88,7 @@ function coder_format_file($sourcefile =
   fclose($fd);
   
   if ($code !== false) {
-    // Preprocess source code.
-    $code = coder_exec_processors($code, 'coder_preprocessor');
-    
-    // Process source code.
-    $code = coder_format_string($code);
-    
-    // Postprocess source code.
-    $code = coder_exec_processors($code, 'coder_postprocessor');
-    
-    // Fix beginning and end of code.
-    $code = coder_trim_php($code);
+    $code = coder_format_string_all($code);
     
     if ($code !== false) {
       // Write formatted source code to target file.
@@ -113,6 +103,29 @@ function coder_format_file($sourcefile =
 }
 
 /**
+ * Formats source code according to Drupal conventions, also using
+ * post and pre-processors.
+ * 
+ * @param
+ *   $code Code to process.
+ */
+function coder_format_string_all($code) {
+  // Preprocess source code.
+  $code = coder_exec_processors($code, 'coder_preprocessor');
+  
+  // Process source code.
+  $code = coder_format_string($code);
+  
+  // Postprocess source code.
+  $code = coder_exec_processors($code, 'coder_postprocessor');
+  
+  // Fix beginning and end of code.
+  $code = coder_trim_php($code);
+  
+  return $code;
+}
+
+/**
  * Format the source code according to Drupal coding style guidelines.
  *
  * This function uses PHP's tokenizer functions.
@@ -166,6 +179,20 @@ function coder_format_file($sourcefile =
  *   $inline_if bool
  *      Controls formatting of ? and : for inline ifs until a ; (semicolon) is
  *      processed.
+ *   $in_function_declaration
+ *      Prevents whitespace after & for function declarations, e.g.
+ *      function &foo(). Is true after function token but before first
+ *      parenthesis.
+ *   $in_array
+ *      Array of parenthesis level to whether or not the structure
+ *      is for an array.
+ *   $in_multiline
+ *      Array of parenthesis level to whether or not the structure
+ *      is multiline.
+ *   $after_semicolon
+ *      Whether or not the current line being processed has a semicolon.
+ *   $after_case
+ *      Whether or not the current line being processed has a case/default statement.
  *
  * @param $code
  *      The source code to format.
@@ -187,11 +214,18 @@ function coder_format_string($code = '')
   $in_do_while    = false;
   
   // Whitespace controls:
-  $in_object   = false;
-  $in_at       = false;
-  $in_php      = false;
-  $in_quote    = false;
-  $inline_if   = false;
+  $in_object        = FALSE;
+  $in_at            = FALSE;
+  $in_php           = FALSE;
+  $in_quote         = FALSE;
+  $inline_if        = FALSE;
+  $in_array         = array();
+  $in_multiline     = array();
+  $after_semicolon  = FALSE;
+  $after_case       = FALSE;
+  
+  // Whether or not a function token was encountered:
+  $in_function_declaration = FALSE;
   
   $result      = '';
   $lasttoken   = array(0);
@@ -220,7 +254,8 @@ function coder_format_string($code = '')
               ++$braces_in_case;
             }
             ++$_coder_indent;
-            $result = rtrim($result) .' '. $text . coder_br();
+            $result = rtrim($result) .' '. $text;
+            coder_br($result);
           }
           else {
             $in_brace = true;
@@ -243,9 +278,10 @@ function coder_format_string($code = '')
             $result = rtrim($result);
             if (substr($result, -1) != '{') {
               // Avoid line break in empty curly braces.
-              $result .= coder_br();
+              coder_br($result);
             }
-            $result .= $text . coder_br();
+            $result .= $text;
+            coder_br($result);
           }
           else {
             $in_brace = false;
@@ -256,7 +292,8 @@ function coder_format_string($code = '')
         case ';':
           $result = rtrim($result) . $text;
           if (!$parenthesis && !$in_heredoc) {
-            $result .= coder_br();
+            coder_br($result);
+            $after_semicolon = TRUE;
           }
           else {
             $result .= ' ';
@@ -279,27 +316,55 @@ function coder_format_string($code = '')
             if ($in_case) {
               ++$_coder_indent;
             }
-            $result = rtrim($result) . $text . coder_br();
+            $result = rtrim($result) . $text;
+            coder_br($result);
           }
           break;
         
         case '(':
           $result .= $text;
           ++$parenthesis;
+          // Not multiline until proven so by whitespace.
+          $in_multiline[$parenthesis] = FALSE;
+          // Not in an multiline array until proven so by whitespace.
+          $in_array[$parenthesis] = FALSE;
+          // Terminate function declaration, as a parenthesis indicates
+          // the beginning of the arguments. This will catch all other
+          // instances of parentheses, but in this case it's not a problem.
+          $in_function_declaration = FALSE;
           break;
         
         case ')':
-          if (!$in_quote && !$in_heredoc && substr(rtrim($result), -1) == ',') {
-            // Fix indent of right parenthesis in multiline arrays by
+          if ($in_array[$parenthesis] && $in_multiline[$parenthesis]) {
+            // Check if a comma insertion is necessary:
+            for ($c = strlen($result) - 1; $c >= 0; $c--) {
+              if ($result[$c] === "\n" || $result[$c] === " ") {
+                continue;
+              }
+              if ($result[$c] === ",") {
+                break;
+              }
+              // We need to add a comma at $c:
+              $result = substr($result, 0, $c + 1) .','. substr($result, $c + 1);
+              break;
+            }
+          }
+          if (!$in_quote && !$in_heredoc && (substr(rtrim($result), -1) == ',' || $in_multiline[$parenthesis])) {
+            // Fix indent of right parenthesis in multiline structures by
             // increasing indent for each parenthesis and decreasing one level.
             $_coder_indent = $_coder_indent + $parenthesis - 1;
-            $result = rtrim($result) . coder_br() . $text;
-            $_coder_indent = $_coder_indent - $parenthesis + 1;
+            $result = rtrim($result);
+            coder_br($result);
+            $result .= $text;
+            // Undo temporary change.
+            $_coder_indent = $_coder_indent - ($parenthesis - 1);
           }
           else {
             $result .= $text;
           }
           if ($parenthesis) {
+            // Current parenthesis level is not an array anymore.
+            $in_array[$parenthesis] = FALSE;
             --$parenthesis;
           }
           break;
@@ -340,16 +405,28 @@ function coder_format_string($code = '')
             $result .= $text;
           }
           else {
-            $result = rtrim($result) .' '. $text .' ';
+            $result = rtrim($result) .' '. $text;
+            // Ampersands used to declare reference return value for
+            // functions should not have trailing space.
+            if (!$in_function_declaration) {
+              $result .= ' ';
+            }
           }
           break;
 
         case '-':
           $result = rtrim($result);
           // Do not add a space before negative numbers or variables.
-          if (substr($result, -1) == '>' || substr($result, -1) == '=' || substr($result, -1) == ',' || substr($result, -1) == ':') {
+          $c = substr($result, -1);
+          // Do not add a space between closing parenthesis and negative arithmetic operators.
+          if ($c == '(') {
+            $result .= ltrim($text);
+          }
+          // Add a space in front of the following chars, but not after them.
+          elseif ($c == '>' || $c == '=' || $c == ',' || $c == ':' || $c == '?') {
             $result .= ' '. $text;
           }
+          // Default arithmetic operator behavior.
           else {
             $result .= ' '. $text .' ';
           }
@@ -390,6 +467,9 @@ function coder_format_string($code = '')
         case T_ARRAY:
           // Write array in lowercase.
           $result .= strtolower(trim($text));
+          // Mark the next parenthesis level (we haven't consumed that token
+          // yet) as an array.
+          $in_array[$parenthesis + 1] = TRUE;
           break;
         
         case T_OPEN_TAG:
@@ -397,12 +477,12 @@ function coder_format_string($code = '')
           $in_php = true;
           // Add a line break between two PHP tags.
           if (substr(rtrim($result), -2) == '?>') {
-            $result .= coder_br();
+            coder_br($result);
           }
           $result .= trim($text);
           if ($first_php_tag) {
-            $result .= coder_br();
-            $first_php_tag = false;
+            coder_br($result);
+            $first_php_tag = FALSE;
           }
           else {
             $result .= ' ';
@@ -454,18 +534,32 @@ function coder_format_string($code = '')
           // Avoid duplicate line feeds outside arrays.
           $c = $parenthesis ? 0 : 1;
           
-          for ($c, $cc = substr_count($text, chr(10)); $c < $cc; ++$c) {
+          for ($c, $cc = substr_count($text, "\n"); $c < $cc; ++$c) {
+            // Newlines were added; not after semicolon anymore
             if ($parenthesis) {
               // Add extra indent for each parenthesis in multiline definitions (f.e. arrays).
               $_coder_indent = $_coder_indent + $parenthesis;
-              $result = rtrim($result) . coder_br();
+              $result = rtrim($result);
+              coder_br($result);
               $_coder_indent = $_coder_indent - $parenthesis;
             }
             else {
               // Discard any whitespace, just insert a line break.
-              $result .= coder_br();
+              coder_br($result);
             }
           }
+          
+          // If there were newlines present inside a parenthesis,
+          // turn on multiline mode.
+          if ($cc && $parenthesis) {
+            $in_multiline[$parenthesis] = TRUE;
+          }
+          
+          // If there were newlines present, move inline comments above.
+          if ($cc) {
+            $after_semicolon = FALSE;
+            $after_case      = FALSE;
+          }
           break;
         
         case T_IF:
@@ -492,7 +586,7 @@ function coder_format_string($code = '')
           break;
         
         case T_WHILE:
-          if ($in_do_while) {
+          if ($in_do_while && substr(rtrim($result), -1) === '}') {
             // Write while after right parenthesis for do {...} while().
             $result = rtrim($result) .' ';
             $in_do_while = false;
@@ -504,30 +598,36 @@ function coder_format_string($code = '')
         case T_ELSE:
         case T_ELSEIF:
           // Write else and else if to a new line.
-          $result = rtrim($result) . coder_br() . trim($text) .' ';
+          $result = rtrim($result);
+          coder_br($result);
+          $result .= trim($text) .' ';
           break;
         
         case T_CASE:
         case T_DEFAULT:
           $braces_in_case = 0;
           $result = rtrim($result);
+          $after_case = true;
           if (!$in_case) {
             $in_case = true;
             // Add a line break between cases.
             if (substr($result, -1) != '{') {
-              $result .= coder_br();
+              coder_br($result);
             }
           }
           else {
             // Decrease current indent to align multiple cases.
             --$_coder_indent;
           }
-          $result .= coder_br() . trim($text) .' ';
+          coder_br($result);
+          $result .= trim($text) .' ';
           break;
         
         case T_BREAK:
           // Write break to a new line.
-          $result = rtrim($result) . coder_br() . trim($text);
+          $result = rtrim($result);
+          coder_br($result);
+          $result .= trim($text);
           if ($in_case && !$braces_in_case) {
             --$_coder_indent;
             $in_case = false;
@@ -546,13 +646,16 @@ function coder_format_string($code = '')
           break;
         
         case T_FUNCTION:
+          $in_function_declaration = true;
+          // Fall through.
         case T_CLASS:
           // Write function and class to new lines.
           $result = rtrim($result);
           if (substr($result, -1) == '}') {
-            $result .= coder_br();
+            coder_br($result);
           }
-          $result .= coder_br() . trim($text) .' ';
+          coder_br($result);
+          $result .= trim($text) .' ';
           break;
         
         case T_EXTENDS:
@@ -600,7 +703,9 @@ function coder_format_string($code = '')
         case T_DOC_COMMENT:
           if (substr($text, 0, 3) == '/**') {
             // Prepend a new line.
-            $result = rtrim($result) . coder_br() . coder_br();
+            $result = rtrim($result);
+            coder_br($result);
+            coder_br($result);
             
             // Remove carriage returns.
             $text = str_replace("\r", '', $text);
@@ -612,7 +717,8 @@ function coder_format_string($code = '')
               
               // Add a new line between function description and first parameter description.
               if (!$params_fixed && substr($lines[$l], 0, 8) == '* @param' && $lines[$l - 1] != '*') {
-                $result .= ' *'. coder_br();
+                $result .= ' *';
+                coder_br($result);
                 $params_fixed = true;
               }
               else if (!$params_fixed && substr($lines[$l], 0, 8) == '* @param') {
@@ -622,7 +728,8 @@ function coder_format_string($code = '')
               
               // Add a new line between function params and return.
               if (substr($lines[$l], 0, 9) == '* @return' && $lines[$l - 1] != '*') {
-                $result .= ' *'. coder_br();
+                $result .= ' *';
+                coder_br($result);
               }
               
               // Add one space indent to get ' *[...]'.
@@ -631,21 +738,35 @@ function coder_format_string($code = '')
               }
               $result .= $lines[$l];
               if ($l < count($lines)) {
-                $result .= coder_br();
+                coder_br($result);
               }
             }
           }
           else {
+            // Move the comment above if it's embedded.
+            $statement = false;
+            if ($after_semicolon && !$after_case) {
+              $nl_position = strrpos(rtrim($result, " \n"), "\n");
+              $statement = substr($result, $nl_position);
+              $result = substr($result, 0, $nl_position);
+              $after_semicolon = false;
+              coder_br($result);
+            }
             $result .= trim($text);
             if ($parenthesis) {
               // Add extra indent for each parenthesis in multiline definitions (f.e. arrays).
               $_coder_indent = $_coder_indent + $parenthesis;
-              $result = rtrim($result) . coder_br();
+              $result = rtrim($result);
+              coder_br($result);
               $_coder_indent = $_coder_indent - $parenthesis;
             }
             else {
               // Discard any whitespace, just insert a line break.
-              $result .= coder_br();
+              coder_br($result);
+            }
+            if ($statement) {
+              $result = rtrim($result, "\n ");
+              $result .= $statement;
             }
           }
           break;
@@ -655,12 +776,14 @@ function coder_format_string($code = '')
           break;
         
         case T_START_HEREDOC:
-          $result .= trim($text) . coder_br(false);
+          $result .= trim($text);
+          coder_br($result, false);
           $in_heredoc = true;
           break;
         
         case T_END_HEREDOC:
-          $result .= trim($text) . coder_br(false);
+          $result .= trim($text);
+          coder_br($result, false);
           $in_heredoc = false;
           break;
         
@@ -678,20 +801,36 @@ function coder_format_string($code = '')
 
 /**
  * Generate a line feed including current line indent.
- *
+ * 
+ * This function will also remove all line indentation from the
+ * previous line if no text was added.
+ * 
+ * @param &$result
+ *   Result variable to append break and indent to, passed by reference.
  * @param $add_indent
  *   Whether to add current line indent after line feed.
- * @return
- *   The resulting string.
  */
-function coder_br($add_indent = true) {
+function coder_br(&$result, $add_indent = true) {
   global $_coder_indent;
   
+  // Scan result backwards for whitespace.
+  for ($i = strlen($result) - 1; $i >= 0; $i--) {
+    if ($result[$i] == ' ') {
+      continue;
+    }
+    if ($result[$i] == "\n") {
+      $result = rtrim($result, ' ');
+      break;
+    }
+    // Non-whitespace was encountered, no changes necessary.
+    break;
+  }
+  
   $output = "\n";
   if ($add_indent && $_coder_indent >= 0) {
     $output .= str_repeat('  ', $_coder_indent);
   }
-  return $output;
+  $result .= $output;
 }
 
 /**
@@ -843,36 +982,6 @@ function coder_preprocessor_line_breaks_
   );
 }
 
-function coder_preprocessor_ml_array_add_comma() {
-  // @bug coder.module:1010.
-  return array(
-    '#title' => 'Append a comma to the last value of multiline arrays.',
-    // ^[\040\t]*(?!\*|\/\/)[^\*\/\n]*? matches anything in front of array, but not comments.
-    // \sarray\( prevents matching of in_array() and function calls.
-    // (\n|(?X>!\);).+?,?\n) matches a line break or the first array item.
-    // (.*?[^,;]) matches the rest array items.
-    // ,?(\n\s*)\); matches the end of multiline array, optionally including a comma.
-    '#search' => '/(^[\040\t]*(?!\*|\/\/)[^\*\/\n]*?\sarray\()(\n|(?>!\);).+?,?\n)(.*?[^,;]),?(\n\s*\);)/ism',
-    '#replace' => '$1$2$3,$4',
-    //'#debug' => true,
-  );
-}
-
-function coder_preprocessor_inline_comment() {
-  return array(
-    '#title' => 'Move inline comments above remarked line.',
-    '#weight' => 2,
-    // [\040\t] matches only a space or tab.
-    // (?!case) prevents matching of case statements.
-    // \S prevents matching of lines containing only a comment.
-    // [^:] prevents matching of URL protocols.
-    // [^;\$] prevents matching of CVS keyword Id comment and double slashes.
-    //   in quotes (f.e. "W3C//DTD").
-    '#search' => '@^([\040\t]*)(?!case)(\S.+?)[\040\t]*[^:]//\s*([^;\$]+?)$@m',
-    '#replace' => "$1// $3\n$1$2",
-  );
-}
-
 function coder_preprocessor_php() {
   return array(
     '#title' => 'Always use &lt;?php ?&gt; to delimit PHP code, not the &lt;? ?&gt; shorthands.',
@@ -948,6 +1057,64 @@ function coder_replace_multiple_vars($ma
   return $return;
 }
 
+function coder_postprocessor_indent_multiline_array() {
+  // Still buggy, disabled for now.
+  return array(
+    '#title' => 'Align equal signs of multiline array assignments in the same column.',
+    // ?: prevents capturing
+    // \s* initial whitespace
+    // ([\'"]).+?\1 matches a string key
+    // .+? matches any other key w/o whitespace
+    // \s*=>\s* matches associative array arrow syntax
+    // .+? matches value
+    '#search' => '/^(?:\s*(?:(?:([\'"]).+?\1|.+?)\s*=>\s*.+?|\),\s?)$){3,}/mi',
+    //'#replace_callback' => 'coder_replace_indent_multiline_array',
+  );
+}
+
+function coder_replace_indent_multiline_array($matches) {
+  // Separate out important components of the multiline array:
+  // (\s*) matches existing indent as \1
+  // (([\'"]).+?\2|\$.+?|[+\-]?(?:0x)?[0-9A-F]+) matches key as \2
+  //    ([\'"]).+?\3 matches a quoted key, quote used is \3
+  //    \.+? matches anything else
+  // \),\s*? matches a closing parenthesis in a nested array
+  // \s*=>\s* matches existing indentation and arrow to be discarded
+  // (.+?) matches value as \4
+  // {3,} requires three or more of these lines
+  // mi enables multiline and caseless mode
+  preg_match_all('/^(\s*)(?:(([\'"]).+?\3|\.+?)\s*=>\s*(.+?),?|\),)\s*?$/mi', $matches[0], $vars, PREG_SET_ORDER);
+  // Determine max key length for varying indentations.
+  $maxlengths = array();
+  foreach ($vars as $var) {
+    list(, $indent, $key) = $var;
+    if (!isset($maxlengths[$indent])) {
+      $maxlengths[$indent] = 0;
+    }
+    if (($t = strlen($key)) > $maxlengths[$indent]) {
+      $maxlengths[$indent] = $t;
+    }
+  }
+  // Reconstruct variable array declaration.
+  $return = '';
+  foreach ($vars as $var) {
+    list(, $indent, $key,, $value) = $var;
+    if ($key === null) {
+      $return .= "$indent),\n";
+      continue;
+    }
+    $spaces = str_repeat(' ', $maxlengths[$indent] - strlen($key));
+    if ($value !== 'array(') {
+      $comma = ',';
+    } else {
+      $comma = '';
+    }
+    $return .= "$indent$key$spaces => $value$comma\n";
+  }
+  $return = rtrim($return, "\n");
+  return $return;
+}
+
 function coder_postprocessor_array_rearrange() {
   // @bug common.inc, comment.module:
   // Not yet working properly 25/03/2007 sun.
Index: scripts/coder_format/tests/tests/comments.phpt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/comments.phpt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 comments.phpt
--- scripts/coder_format/tests/tests/comments.phpt	19 Jan 2008 20:15:21 -0000	1.1.2.1
+++ scripts/coder_format/tests/tests/comments.phpt	20 Jan 2008 03:37:34 -0000
@@ -24,6 +24,109 @@ function foo() {
   return 'boo';
 }
 
+function _batch_progress_page_nojs() {
+  else {
+    // This is one of the later requests: do some processing first.
+
+    // Error handling: if PHP dies due to a fatal error (e.g. non-existant
+    // function), it will output whatever is in the output buffer,
+    // followed by the error message.
+  }
+}
+
+function drupal_page_cache_header($cache) {
+  if ($if_modified_since && $if_none_match
+      && $if_none_match == $etag // etag must match
+      && $if_modified_since == $last_modified) {  // if-modified-since must match
+    header('HTTP/1.1 304 Not Modified');
+    // All 304 responses must send an etag if the 200 response for the same object contained an etag
+    header("Etag: $etag");
+    exit();
+  }
+}
+
+function parse_size($size) {
+  $suffixes = array(
+    '' => 1,
+    'k' => 1024,
+    'm' => 1048576, // 1024 * 1024
+    'g' => 1073741824, // 1024 * 1024 * 1024
+  );
+}
+
+function drupal_to_js($var) {
+  switch (gettype($var)) {
+    case 'boolean':
+      return $var ? 'true' : 'false'; // Lowercase necessary!
+    case 'integer':
+    case 'double':
+      return $var;
+  }
+}
+
+function _db_query_callback($match, $init = FALSE) {
+  switch ($match[1]) {
+    case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?)
+      return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
+    case '%f':
+      return (float) array_shift($args);
+    case '%b': // binary data
+      return db_encode_blob(array_shift($args));
+  }
+}
+
+function db_query_range($query) {
+  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
+    $args = $args[0];
+  }
+}
+
+function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL, $group = 'default') {
+  $fd = fopen($file->filepath, "rb"); // File will get closed by PHP on return
+  $context = "COMMENT"; // Parser context: COMMENT, MSGID, MSGID_PLURAL, MSGSTR and MSGSTR_ARR
+  $current = array();   // Current entry being read
+  $plural = 0;          // Current plural form
+  $lineno = 0;          // Current line
+
+  while (!feof($fd)) {
+    $line = fgets($fd, 10*1024); // A line should not be this long
+    if (!strncmp("#", $line, 1)) { // A comment
+      if ($context == "COMMENT") { // Already in comment context: add
+        $current["#"][] = substr($line, 1);
+      }
+      elseif (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { // End current entry, start a new one
+        _locale_import_one_string($op, $current, $mode, $lang, $file, $group);
+      }
+      else { // Parse error
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgid_plural", $line, 12)) {
+      if ($context != "MSGID") { // Must be plural form for current entry
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgid", $line, 5)) {
+      if ($context == "MSGSTR") {   // End current entry, start a new one
+        $current = array();
+      }
+      elseif ($context == "MSGID") { // Already in this context? Parse error
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgstr[", $line, 7)) {
+      if (($context != "MSGID") && ($context != "MSGID_PLURAL") && ($context != "MSGSTR_ARR")) { // Must come after msgid, msgid_plural, or msgstr[]
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgstr", $line, 6)) {
+      if ($context != "MSGID") {   // Should come just after a msgid block
+        return FALSE;
+      }
+    }
+  }
+}
+
 --EXPECT--
 // Change:
 // Move up.
@@ -48,3 +151,133 @@ $string = array(
 function foo() {
   return 'boo';
 }
+
+function _batch_progress_page_nojs() {
+  else {
+    // This is one of the later requests: do some processing first.
+
+    // Error handling: if PHP dies due to a fatal error (e.g. non-existant
+    // function), it will output whatever is in the output buffer,
+    // followed by the error message.
+  }
+}
+
+function drupal_page_cache_header($cache) {
+  if ($if_modified_since && $if_none_match
+    // etag must match
+    && $if_none_match == $etag
+    // if-modified-since must match
+    && $if_modified_since == $last_modified) {
+    header('HTTP/1.1 304 Not Modified');
+    // All 304 responses must send an etag if the 200 response for the same object contained an etag
+    header("Etag: $etag");
+    exit();
+  }
+}
+
+function parse_size($size) {
+  $suffixes = array(
+    '' => 1,
+    'k' => 1024,
+    // 1024 * 1024
+    'm' => 1048576,
+    // 1024 * 1024 * 1024
+    'g' => 1073741824,
+  );
+}
+
+function drupal_to_js($var) {
+  switch (gettype($var)) {
+    case 'boolean':
+      // Lowercase necessary!
+      return $var ? 'true' : 'false';
+
+    case 'integer':
+    case 'double':
+      return $var;
+  }
+}
+
+function _db_query_callback($match, $init = FALSE) {
+  switch ($match[1]) {
+    case '%d':
+      // We must use type casting to int to convert FALSE/NULL/(TRUE?)
+      // We don't need db_escape_string as numbers are db-safe
+      return (int) array_shift($args);
+
+    case '%f':
+      return (float) array_shift($args);
+
+    case '%b':
+      // binary data
+      return db_encode_blob(array_shift($args));
+  }
+}
+
+function db_query_range($query) {
+  // 'All arguments in one array' syntax
+  if (isset($args[0]) and is_array($args[0])) {
+    $args = $args[0];
+  }
+}
+
+function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL, $group = 'default') {
+  // File will get closed by PHP on return
+  $fd = fopen($file->filepath, "rb");
+  // Parser context: COMMENT, MSGID, MSGID_PLURAL, MSGSTR and MSGSTR_ARR
+  $context = "COMMENT";
+  // Current entry being read
+  $current = array();
+  // Current plural form
+  $plural = 0;
+  // Current line
+  $lineno = 0;
+
+  while (!feof($fd)) {
+    // A line should not be this long
+    $line = fgets($fd, 10*1024);
+    // A comment
+    if (!strncmp("#", $line, 1)) {
+      // Already in comment context: add
+      if ($context == "COMMENT") {
+        $current["#"][] = substr($line, 1);
+      }
+      // End current entry, start a new one
+      elseif (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) {
+        _locale_import_one_string($op, $current, $mode, $lang, $file, $group);
+      }
+      // Parse error
+      else {
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgid_plural", $line, 12)) {
+      // Must be plural form for current entry
+      if ($context != "MSGID") {
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgid", $line, 5)) {
+      // End current entry, start a new one
+      if ($context == "MSGSTR") {
+        $current = array();
+      }
+      // Already in this context? Parse error
+      elseif ($context == "MSGID") {
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgstr[", $line, 7)) {
+      // Must come after msgid, msgid_plural, or msgstr[]
+      if (($context != "MSGID") && ($context != "MSGID_PLURAL") && ($context != "MSGSTR_ARR")) {
+        return FALSE;
+      }
+    }
+    elseif (!strncmp("msgstr", $line, 6)) {
+      // Should come just after a msgid block
+      if ($context != "MSGID") {
+        return FALSE;
+      }
+    }
+  }
+}
Index: scripts/coder_format/tests/tests/control.phpt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/control.phpt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 control.phpt
--- scripts/coder_format/tests/tests/control.phpt	19 Jan 2008 20:15:21 -0000	1.1.2.1
+++ scripts/coder_format/tests/tests/control.phpt	20 Jan 2008 04:41:12 -0000
@@ -20,6 +20,23 @@ function menu_tree_page_data($menu_name 
   }
 }
 
+function language_url_rewrite(&$path, &$options) {
+  if (!$options['external']) {
+    switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
+      case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
+        $default = language_default();
+        // Intentionally no break here.
+
+      case LANGUAGE_NEGOTIATION_PATH:
+      default:
+        if (!empty($options['language']->prefix)) {
+          $options['prefix'] = $options['language']->prefix .'/';
+        }
+        break;
+    }
+  }
+}
+
 
 --EXPECT--
 // No change:
@@ -29,10 +46,10 @@ function menu_tree_page_data($menu_name 
     // Collect all the links set to be expanded, and then add all of
     // their children to the list as well.
     do {
-      $result   = db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND expanded = 1 AND has_children = 1 AND plid IN (". $placeholders .') AND mlid NOT IN ('. $placeholders .')', array_merge(array($menu_name), $args, $args));
+      $result = db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND expanded = 1 AND has_children = 1 AND plid IN (". $placeholders .') AND mlid NOT IN ('. $placeholders .')', array_merge(array($menu_name), $args, $args));
       $num_rows = FALSE;
       while ($item = db_fetch_array($result)) {
-        $args[]   = $item['mlid'];
+        $args[] = $item['mlid'];
         $num_rows = TRUE;
       }
       $placeholders = implode(', ', array_fill(0, count($args), '%d'));
@@ -40,3 +57,19 @@ function menu_tree_page_data($menu_name 
   }
 }
 
+function language_url_rewrite(&$path, &$options) {
+  if (!$options['external']) {
+    switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
+      case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
+        $default = language_default();
+        // Intentionally no break here.
+      case LANGUAGE_NEGOTIATION_PATH:
+      default:
+        if (!empty($options['language']->prefix)) {
+          $options['prefix'] = $options['language']->prefix .'/';
+        }
+        break;
+    }
+  }
+}
+
Index: scripts/coder_format/tests/tests/operators.phpt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/operators.phpt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 operators.phpt
--- scripts/coder_format/tests/tests/operators.phpt	19 Jan 2008 20:15:21 -0000	1.1.2.1
+++ scripts/coder_format/tests/tests/operators.phpt	20 Jan 2008 01:50:11 -0000
@@ -2,7 +2,6 @@
 TEST: Arithmetic operators
 
 --INPUT--
-// No change:
 function foo() {
   $foo = $bar ? -1 : 0;
   $foo = (-1 + 1);
@@ -14,18 +13,3 @@ function file_download() {
     return drupal_access_denied();
   }
 }
-
---EXPECT--
-// No change:
-function foo() {
-  $foo = $bar ? -1 : 0;
-  $foo = (-1 + 1);
-  return ($a_weight < $b_weight) ? -1 : 1;
-}
-
-function file_download() {
-  if (in_array(-1, $headers)) {
-    return drupal_access_denied();
-  }
-}
-
Index: scripts/coder_format/tests/tests/parenthesis.phpt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/parenthesis.phpt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 parenthesis.phpt
--- scripts/coder_format/tests/tests/parenthesis.phpt	19 Jan 2008 20:15:21 -0000	1.1.2.1
+++ scripts/coder_format/tests/tests/parenthesis.phpt	20 Jan 2008 02:52:14 -0000
@@ -2,29 +2,62 @@
 TEST: Parenthesis
 
 --INPUT--
-// No change:
 function drupal_mail_send($message) {
   return mail(
     $message['to'],
     mime_header_encode($message['subject']),
-    // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF.
-    // They will appear correctly in the actual e-mail that is sent.
     str_replace("\r", '', $message['body']),
     join("\n", $mimeheaders)
   );
 }
 
+function drupal_to_js($var) {
+  switch (gettype($var)) {
+    case 'string':
+      return '"'. str_replace(array("\r", "\n", "<", ">", "&"),
+                              array('\r', '\n', '\x3c', '\x3e', '\x26'),
+                              addslashes($var)) .'"';
+    default:
+      return 'null';
+  }
+}
+
+function drupal_urlencode($text) {
+  if (variable_get('clean_url', '0')) {
+    return str_replace(array('%2F', '%26', '%23', '//'),
+                       array('/', '%2526', '%2523', '/%252F'),
+                       rawurlencode($text));
+  }
+}
 
 --EXPECT--
-// No change:
 function drupal_mail_send($message) {
   return mail(
     $message['to'],
     mime_header_encode($message['subject']),
-    // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF.
-    // They will appear correctly in the actual e-mail that is sent.
     str_replace("\r", '', $message['body']),
     join("\n", $mimeheaders)
   );
 }
 
+function drupal_to_js($var) {
+  switch (gettype($var)) {
+    case 'string':
+      return '"'. str_replace(array("\r", "\n", "<", ">", "&"),
+        array('\r', '\n', '\x3c', '\x3e', '\x26'),
+        addslashes($var)) .'"';
+
+    default:
+      return 'null';
+  }
+}
+
+function drupal_urlencode($text) {
+  if (variable_get('clean_url', '0')) {
+    return str_replace(array('%2F', '%26', '%23', '//'),
+      array('/', '%2526', '%2523', '/%252F'),
+      rawurlencode($text)
+    );
+  }
+}
+
Index: scripts/coder_format/tests/tests/references.phpt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/references.phpt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 references.phpt
--- scripts/coder_format/tests/tests/references.phpt	19 Jan 2008 20:15:21 -0000	1.1.2.1
+++ scripts/coder_format/tests/tests/references.phpt	20 Jan 2008 02:15:31 -0000
@@ -2,14 +2,6 @@
 TEST: Assignments by reference
 
 --INPUT--
-// No change:
-function &foo() {
-  echo 'asdf';
-}
-
-
---EXPECT--
-// No change:
 function &foo() {
   echo 'asdf';
 }
Index: scripts/coder_format/tests/tests/variables.phpt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coder/scripts/coder_format/tests/tests/Attic/variables.phpt,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 variables.phpt
--- scripts/coder_format/tests/tests/variables.phpt	19 Jan 2008 20:15:21 -0000	1.1.2.1
+++ scripts/coder_format/tests/tests/variables.phpt	20 Jan 2008 03:55:52 -0000
@@ -2,47 +2,50 @@
 TEST: Variables
 
 --INPUT--
-// No change:
-function node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) {
-  $node = (object)$node;
-
-  $node = node_build_content($node, $teaser, $page);
-
-  if ($links) {
-    $node->links = module_invoke_all('link', 'node', $node, $teaser);
-    drupal_alter('link', $node->links, $node);
-  }
+function db_status_report($phase) {
+  $foo = bar();
+  $t = get_t();
+
+  $version = db_version();
+
+  $form['mysql'] = array(
+    'title' => $t('MySQL database'),
+    'value' => ($phase == 'runtime') ? l($version, 'admin/reports/status/sql') : $version,
+  );
 }
 
-function node_content_form($node, $form_state) {
-  $type = node_get_types('type', $node);
-  $form = array();
+class CoderTestFile extends SimpleExpectation {
+  private $expected;
+
+  /* Filename of test */
+  var $filename;
 
-  if ($type->has_title) {
-    $form['title'] = array();
+  protected function describeException($exception) {
+    return get_class($exception) .": ". $exception->getMessage();
   }
 }
 
-
 --EXPECT--
-// No change:
-function node_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) {
-  $node = (object)$node;
-
-  $node = node_build_content($node, $teaser, $page);
-
-  if ($links) {
-    $node->links = module_invoke_all('link', 'node', $node, $teaser);
-    drupal_alter('link', $node->links, $node);
-  }
+function db_status_report($phase) {
+  $foo = bar();
+  $t   = get_t();
+
+  $version = db_version();
+
+  $form['mysql'] = array(
+    'title' => $t('MySQL database'),
+    'value' => ($phase == 'runtime') ? l($version, 'admin/reports/status/sql') : $version,
+  );
 }
 
-function node_content_form($node, $form_state) {
-  $type = node_get_types('type', $node);
-  $form = array();
+class CoderTestFile extends SimpleExpectation {
+  private $expected;
+
+  /* Filename of test */
+  var $filename;
 
-  if ($type->has_title) {
-    $form['title'] = array();
+  protected function describeException($exception) {
+    return get_class($exception) .": ". $exception->getMessage();
   }
 }
 
