You are here

function do_coder_review_regex in Coder 6.2

Same name and namespace in other branches
  1. 5.2 coder.module \do_coder_review_regex()
  2. 5 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 1620

Code

function do_coder_review_regex(&$coder_args, $review, $rule, $lines, &$results) {
  if (isset($rule['#value']) && !empty($lines)) {
    $regexflags = isset($rule['#case-sensitive']) ? '' : 'i';
    $regex = '/' . $rule['#value'] . '/' . $regexflags;
    $class_regex = isset($rule['#class']) ? '/' . $rule['#class'] . '/' : '';
    $class_not_regex = isset($rule['#class-not']) ? '/' . $rule['#class-not'] . '/' : '';
    $class_current = '';
    $class_paren = 0;
    $function_regex = isset($rule['#function']) ? '/' . $rule['#function'] . '/' : '';
    $function_not_regex = isset($rule['#function-not']) ? '/' . $rule['#function-not'] . '/' : '';
    $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_array) {
      foreach ($line_array as $line) {

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

        // Some rules apply only within certain classes.
        if ($class_regex || $class_not_regex) {
          if (preg_match('/class (\\w+)/', $line, $match) || preg_match('/interface (\\w+)/', $line, $match)) {
            $class_current = $match[1];
          }
          if (preg_match('/([{}])/', $line, $match)) {
            $class_paren += $match[0] == '{' ? 1 : -1;
          }
          if ($class_paren < 0 || $class_regex && $class_current == '' || $class_regex && !preg_match($class_regex, $class_current) || $class_not_regex && preg_match($class_not_regex, $class_current)) {
            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, $coder_args['#ignore_lines'][$review['#review_name']]);
        }
      }
    }
  }
}