You are here

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

Performs a 'regex' type of coder_review.

This type of do_coder_review_* attempts to locate occurances based on a regular expression string.

Parameters

array $coder_args: A Coder settings array, passed by reference. See do_coder_review() for format.

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

array $rule: Rule array being checked.

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

array $results: Results array variable to save errors to, passed by reference.

See also

do_coder_review()

_coder_review_severity_name()

1 call to do_coder_review_regex()
do_coder_review in coder_review/coder_review.common.inc
Performs a code review for a review array.

File

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

Code

function do_coder_review_regex(array &$coder_args, array $review, array $rule, array $lines, array &$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'] . '/' : '';
    $function_regex = isset($rule['#function']) ? '/' . $rule['#function'] . '/' : '';
    $function_not_regex = isset($rule['#function-not']) ? '/' . $rule['#function-not'] . '/' : '';
    $not_regex = isset($rule['#not']) ? '/' . $rule['#not'] . '/' . $regexflags : '';
    $never_regex = isset($rule['#never']) ? '/' . $rule['#never'] . '/' . $regexflags : '';
    $review_name = $review['#review_name'];
    foreach ($lines as $lineno => $line_array) {
      foreach ($line_array as $line) {
        if (preg_match($regex, $line, $matches)) {

          // Some rules apply only within certain functions and classes.
          list($class_current, $function_current) = isset($coder_args['#stack'][$lineno]) ? $coder_args['#stack'][$lineno] : array(
            '',
            '',
          );
          if ($function_regex && (!$function_current || !preg_match($function_regex, $function_current)) || $function_not_regex && $function_current && preg_match($function_not_regex, $function_current) || $class_regex && (!$class_current || !preg_match($class_regex, $class_current[0])) || $class_not_regex && $class_current && preg_match($class_not_regex, $class_current[0])) {
            continue;
          }

          // 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_review_severity_name($coder_args, $review, $rule);
          _coder_review_error($results, $rule, $severity_name, $lineno, $line, $coder_args['#ignores']);
        }
      }
    }
  }
}