You are here

function do_coder_reviews in Coder 7.2

Same name and namespace in other branches
  1. 5.2 coder.module \do_coder_reviews()
  2. 5 coder.module \do_coder_reviews()
  3. 6.2 coder.module \do_coder_reviews()
  4. 6 coder.module \do_coder_reviews()
  5. 7 coder_review/coder_review.module \do_coder_reviews()

Performs coder reviews for multiple code review definition files.

Parameters

array $coder_args: An associative array of coder arguments. The valid arguments are:

  • #reviews => An array list of reviews to perform. For more information, see _coder_review_reviews().
  • #severity => An integer magic number. See the constants SEVERITY_*, as defined at the top of coder_review.common.inc.
  • #filename => A string with the filename to check.
  • #patch => A string with the patch lines to perform a review on.

Return value

array An associative array of results, in the form:

  • #stats => An array with error counts for all severities, in the form:

    • 'minor' => An integer count.
    • 'normal' => An integer count.
    • 'critical' => An integer count.
  • integer ID => An HTML error message string for display.

See also

_coder_review_reviews()

2 calls to do_coder_reviews()
coder_review_page_form in coder_review/coder_review.module
Implements hook_form().
_coder_review_page_form_includes in coder_review/coder_review.module
Adds results to form definition for display on the coder review page.

File

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

Code

function do_coder_reviews(array $coder_args) {

  // Load the cached results if they exist, but not for patches.
  if (empty($coder_args['#patch']) && empty($coder_args['#test']) && $coder_args['#cache']) {

    // @todo Replace md5() with with sha256().  See [#723802] for more info.
    $cache_key = 'coder:' . md5(implode(':', array_keys($coder_args['#reviews']))) . $coder_args['#severity'] . ':' . $coder_args['#filename'];
    if (_drush()) {
      if (drush_get_option('checkstyle')) {
        $cache_key .= ':drushcheckstyle';
      }
      elseif (drush_get_option('xml')) {
        $cache_key .= ':drushxml';
      }
    }
    $filepath = realpath($coder_args['#filename']);
    if (file_exists($filepath)) {
      $cache_mtime = filemtime($filepath);
      $cache_results = _cache_get($cache_key, 'cache_coder');
      if ($cache_results && $cache_results->data['mtime'] == $cache_mtime && _coder_review_modified() < $cache_results->created) {
        return $cache_results->data['results'];
      }
    }
  }
  $results['#stats'] = array(
    'minor' => 0,
    'normal' => 0,
    'critical' => 0,
    'ignored' => 0,
  );

  // Skip PHP include files when the user requested severity is above minor.
  if (isset($coder_args['#php_minor']) && _substr($coder_args['#filename'], -4) == '.php') {
    if ($coder_args['#severity'] > 1) {
      return $results;
    }
  }

  // Read the file.
  if (_coder_review_read_and_parse_file($coder_args)) {

    // Do all of the code reviews.
    $errors = array();
    foreach ($coder_args['#reviews'] as $review_name => $review) {
      $review['#review_name'] = $review_name;
      $result = do_coder_review($coder_args, $review);
      if ($result) {

        // Keep track of severity and "ignored" statistics.
        foreach (array(
          'critical',
          'normal',
          'minor',
          'ignored',
        ) as $severity_level) {
          if (isset($result['#stats'][$severity_level])) {
            $results['#stats'][$severity_level] += $result['#stats'][$severity_level];
          }
        }
        $errors += $result;
      }
    }

    // Theme the error messages.
    foreach ($errors as $key => $error) {
      if (is_numeric($key)) {
        $error['warning'] = _coder_review_warning($error['rule']);
        $results[$key] = theme_coder_review_warning($error);
      }
    }

    // Sort the results.
    ksort($results, SORT_NUMERIC);
  }
  else {
    $results[] = theme('coder_review_warning', array(
      'warning' => _t('Could not read the file'),
      'severity_name' => 'critical',
    ));
  }

  // Save the results in the cache if we're not reviewing a patch.
  if (empty($coder_args['#patch']) && empty($coder_args['#test']) && $coder_args['#cache'] && isset($cache_mtime)) {
    $cache_results = array(
      'mtime' => $cache_mtime,
      'results' => $results,
    );
    _cache_set($cache_key, $cache_results, 'cache_coder');
  }
  return $results;
}