You are here

function do_coder_review in Coder 6

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. 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.

2 calls to do_coder_review()
coder_test in ./coder.module
Helper function to run the review on the code snippet.
do_coder_reviews in ./coder.module
Perform batch coder reviews for multiple files.

File

./coder.module, line 1374
Developer Module that assists with code review and version upgrade that supports a plug-in extensible hook system so contributed modules can define additional review standards.

Code

function do_coder_review($coder_args, $review) {
  $results = array(
    '#stats' => array(
      'minor' => 0,
      'normal' => 0,
      'critical' => 0,
    ),
  );
  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;
    foreach ($review['#rules'] as $rule) {

      // Ignore rules that don't match the file extension.
      $filename = empty($coder_args['#patch']) ? $coder_args['#filename'] : 'coder.patch';
      if (!empty($rule['#filename'])) {
        $regex = '/' . $rule['#filename'] . '/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'] : (empty($rule['#filename']) || !preg_match('/\\\\.js\\$/i', $rule['#filename'], $matches) ? '\\.js$' : null);
      if ($not) {
        $regex = '/' . $not . '/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['#original'])) {

          // Deprecated.
          $lines = $coder_args['#all_lines'];
        }
        elseif (isset($rule['#source'])) {

          // Values: all, html, comment, allphp or php.
          $source = '#' . $rule['#source'] . '_lines';
          $lines = $coder_args[$source];
        }
        else {
          $lines = $coder_args['#php_lines'];
        }
        if ($lines) {
          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;
}