You are here

function do_coder_review in Coder 7.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.2 coder.module \do_coder_review()
  4. 6 coder.module \do_coder_review()
  5. 7 coder_review/coder_review.module \do_coder_review()

Performs a code review for a review array.

Parameters

array $coder_args: An array of coder_review settings, which must have been prepared with _coder_review_read_and_parse_file(). See do_coder_reviews() for more information on the array format.

array $review: A review array. See hook_review() for format details.

Return value

array An array of coder_review results. See do_coder_reviews() return value for more information on the format.

See also

do_coder_reviews()

hook_review()

_coder_review_read_and_parse_file()

1 call to do_coder_review()
do_coder_reviews in coder_review/coder_review.common.inc
Performs coder reviews for multiple code review definition files.

File

coder_review/coder_review.common.inc, line 925
Common functions used by both the drush and form interfaces.

Code

function do_coder_review(array $coder_args, array $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',
    'theme',
  ));
  if (!empty($review['#js'])) {
    $allowed_extensions[] = 'js';
  }
  if ($review['#rules']) {

    // Get the review's severity, used when the rule severity is not defined.
    $default_severity = isset($review['#severity']) ? _coder_review_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 (!empty($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_name => $rule) {
      $rule += array(
        '#rule_name' => $rule_name,
        '#review_name' => $review['#review_name'],
      );

      // Ignore rules that don't match the file extension.
      $filename_includes = isset($rule['#filename']) ? $rule['#filename'] : (isset($rule['#filename-not']) ? $allowed_extensions : NULL);
      if ($filename_includes) {
        $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_review_severity(isset($rule['#severity']) ? $rule['#severity'] : '', $default_severity);
      if ($severity >= $coder_args['#severity']) {

        // #source can be an array, a string, or empty.
        $rule_sources = isset($rule['#source']) ? is_array($rule['#source']) ? $rule['#source'] : array(
          $rule['#source'],
        ) : array(
          'php',
        );
        foreach ($rule_sources as $source) {

          // Permissible values for #source: all, html, comment, allphp or php.
          $source_lines = '#' . $source . '_array_lines';
          $lines = isset($coder_args[$source_lines]) ? $coder_args[$source_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;
            default:
              _message(_t('Invalid rule type @type', array(
                '@type' => $rule['#type'],
              )), 'error');
              break;
          }
        }
      }
    }
  }
  return $results;
}