You are here

function do_coder_review_regex in Coder 5.2

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

Implements do_coder_review_* for regex match.

Parameters

$coder_args: Coder settings array variable, see do_coder_review() for format.

$review: Review array the current rule belongs to, used by _coder_severity_name().

$rule: Rule array being checked.

$lines: Pertinent source file lines according to rule's '#source' value.

$results: Results array variable to save errors to.

1 call to do_coder_review_regex()
do_coder_review in ./coder.module
Perform code review for a review array.

File

./coder.module, line 1245
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_regex(&$coder_args, $review, $rule, $lines, &$results) {
  if (isset($rule['#value'])) {
    $regexflags = isset($rule['#case-sensitive']) ? '' : 'i';
    $regex = '/' . $rule['#value'] . '/' . $regexflags;
    $function_regex = isset($rule['#function']) ? '/' . $rule['#function'] . '/' : '';
    $current_function = '';
    $paren = 0;
    $not_regex = isset($rule['#not']) ? '/' . $rule['#not'] . '/' . $regexflags : '';
    $never_regex = isset($rule['#never']) ? '/' . $rule['#never'] . '/' . $regexflags : '';
    foreach ($lines as $lineno => $line) {

      // Some rules apply only within certain functions.
      if ($function_regex) {
        if (preg_match('/function (\\w+)\\(/', $line, $match)) {
          $current_function = $match[1];
        }
        if (preg_match('/([{}])/', $line, $match)) {
          $paren += $match[0] == '{' ? 1 : -1;
        }
        if ($paren < 0 || $current_function == '' || !preg_match($function_regex, $current_function)) {
          continue;
        }
      }
      if (preg_match($regex, $line, $matches)) {

        // Don't match some regex's.
        if ($not_regex) {
          foreach ($matches as $match) {
            if (preg_match($not_regex, $match)) {
              continue 2;
            }
          }
        }
        if ($never_regex) {
          if (preg_match($never_regex, $coder_args['#all_lines'][$lineno])) {
            continue;
          }
        }
        $line = $coder_args['#all_lines'][$lineno];
        $severity_name = _coder_severity_name($coder_args, $review, $rule);
        _coder_error($results, $rule, $severity_name, $lineno, $line);
      }
    }
  }
}