You are here

function do_coder_review in Coder 6.2

Same name and namespace in other branches
  1. 5.2 coder.module \do_coder_review()
  2. 5 coder.module \do_coder_review()
  3. 6 coder.module \do_coder_review()
  4. 7.2 coder_review/coder_review.common.inc \do_coder_review()
  5. 7 coder_review/coder_review.module \do_coder_review()

Perform code review for a review array.

Parameters

$coder_args: Array coder settings, must have been prepared with _coder_read_and_parse_file(), see do_coder_reviews() for format.

$review: Review array, see hook_review().

Return value

Array results, see do_coder_reviews() return value for format.

1 call to do_coder_review()
do_coder_reviews in ./coder.module
Perform batch coder reviews for multiple files.

File

./coder.module, line 1532

Code

function do_coder_review($coder_args, $review) {
  $results = array(
    '#stats' => array(
      'minor' => 0,
      'normal' => 0,
      'critical' => 0,
      'ignored' => 0,
    ),
  );
  $allowed_extensions = array_merge($coder_args['#php_extensions'], $coder_args['#include_extensions'], array(
    'module',
  ));
  if ($review['#rules']) {

    // Get the review's severity, used when the rule severity is not defined.
    $default_severity = isset($review['#severity']) ? _coder_severity($review['#severity']) : SEVERITY_NORMAL;

    // Get filename of current file.
    $filename = empty($coder_args['#patch']) ? $coder_args['#filename'] : 'coder_review.patch';
    $is_patch = 0;

    // Handle special case filename for patch files, if available.
    if ($coder_args['#patch'] && preg_match('/(.+?):/', $coder_args['#filename'], $matches)) {
      $filename = empty($matches) ? 'coder_review.patch' : $matches[1];
      $is_patch = 1;
    }
    foreach ($review['#rules'] as $rule) {

      // Ignore rules that don't match the file extension.
      if ($filename_includes = isset($rule['#filename']) ? $rule['#filename'] : (isset($rule['#filename-not']) ? $coder_args['#php_extensions'] : NULL)) {
        $regex = '/(' . implode('|', $filename_includes) . ')$/i';
        if (!preg_match($regex, $filename, $matches)) {
          continue;
        }
      }

      // Ignore rules that do match the file extension, javascript files are excluded by default.
      $not = isset($rule['#filename-not']) ? $rule['#filename-not'] : (isset($rule['#filename']) ? NULL : $coder_args['#include_extensions']);
      if ($not) {
        $regex = '/(' . implode('|', $not) . ')$/i';

        // Check filename.  If a patch, also check the .patch extension.
        if (preg_match($regex, $filename, $matches) || $is_patch && preg_match($regex, 'coder_review.patch', $matches)) {
          continue;
        }
      }

      // If it's a patch, it can contain any sort of file, so ensure the file
      // being processed is either php or one of the supported extensions.
      if ($is_patch) {
        $regex = '/(' . implode('|', $allowed_extensions) . ')$/i';
        if (!preg_match($regex, $filename, $matches)) {
          continue;
        }
      }

      // Perform the review if above the user requested severity.
      $severity = _coder_severity(isset($rule['#severity']) ? $rule['#severity'] : '', $default_severity);
      if ($severity >= $coder_args['#severity']) {
        if (isset($rule['#source'])) {

          // Values: all, html, comment, allphp or php.
          $source = '#' . $rule['#source'] . '_array_lines';
          $lines = $coder_args[$source];
        }
        else {
          $lines = isset($coder_args['#php_array_lines']) ? $coder_args['#php_array_lines'] : array();
        }
        switch ($rule['#type']) {
          case 'regex':
            do_coder_review_regex($coder_args, $review, $rule, $lines, $results);
            break;
          case 'grep':
            do_coder_review_grep($coder_args, $review, $rule, $lines, $results);
            break;
          case 'grep_invert':
            do_coder_review_grep_invert($coder_args, $review, $rule, $lines, $results);
            break;
          case 'callback':
            do_coder_review_callback($coder_args, $review, $rule, $lines, $results);
            break;
        }
      }
    }
  }
  return $results;
}