You are here

function drush_coder_review in Coder 7.2

Performs the actual review for drush.

File

coder_review/coder_review.drush.inc, line 263
Command line utility support for Coder_review module.

Code

function drush_coder_review() {

  // Bootstrap Drupal if it is available.
  if (drush_conf_path(_drush_bootstrap_selected_uri()) && !function_exists('drupal_goto')) {
    drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_LOGIN);
  }

  // Bootstrap enough of coder review to run.
  require_once realpath(__DIR__) . '/coder_review.common.inc';

  // Check for the 'git' option.
  $git = drush_get_option('git');
  if (!empty($git)) {
    $uninstall = $git === 'no' || $git === 'off' || $git === 'remove' || $git === 'uninstall';
    return _drush_coder_review_git($uninstall);
  }

  // Get the command line options severity options.
  $severity = drush_get_option('severity');
  foreach (array(
    'major',
    'minor',
    'critical',
  ) as $option) {
    if ($severity == $option || drush_get_option($option)) {
      $severity_name = $option;
    }
  }

  // Get the command line options module grouping options.
  foreach (array(
    'active',
    'core',
    'contrib',
    'all',
    'default',
  ) as $option) {
    if (drush_get_option($option)) {
      $settings = _coder_review_get_default_settings($option);
      $settings['coder_includes'] = 1;
      break;
    }
  }
  if (!isset($settings)) {
    $settings = _coder_review_get_default_settings();
  }

  // Get the command line review options.
  $avail_reviews = _coder_review_reviews();
  $reviews = array();
  $reviews_option = drush_get_option('reviews');
  if ($reviews_option) {
    if ($reviews_option == 'all') {

      // @note: Don't use drupal_map_assoc because of bootstrap level.
      $avail_review_keys = array_keys($avail_reviews);
      $reviews += array_combine($avail_review_keys, $avail_review_keys);
    }
    else {
      foreach (explode(',', $reviews_option) as $option) {
        if (isset($avail_reviews[$option])) {
          $reviews[$option] = $option;
        }
      }
    }
  }
  foreach (array_keys($avail_reviews) as $option) {
    if (_coder_review_drush_get_option_no($option)) {
      unset($reviews[$option]);
    }
    elseif (drush_get_option($option)) {
      $reviews[$option] = $option;
    }
  }
  if (!$reviews) {
    $args = drush_parse_args();
    $review = $args[0] == 'sniffer' ? 'sniffer' : 'style';
    $reviews[$review] = $review;
  }

  // Process command line arguments.
  $args = func_get_args();
  $modules = array();
  $output = array();
  if (!empty($args)) {
    foreach ($args as $arg) {
      switch ($arg) {
        case 'summary':
        case 'no-empty':
        case 'xml':
        case 'checkstyle':
        case 'active':
        case 'core':
        case 'contrib':
        case 'all':
        case 'default':
          return drush_set_error(dt('use --@option.', array(
            '@option' => $arg,
          )));
        case 'major':
        case 'minor':
        case 'critical':
          return drush_set_error(dt('use --severity or --@option.', array(
            '@option' => $arg,
          )));
        default:
          if (isset($avail_reviews[$arg])) {
            return drush_set_error(dt('use --reviews or --@option.', array(
              '@option' => $arg,
            )));
          }
          elseif (function_exists('db_query') && (strpos($arg, '*') !== FALSE || strpos($arg, '%') !== FALSE)) {
            $result = db_query('SELECT name FROM {system} WHERE name LIKE :name', array(
              ':name' => str_replace('*', '%', $arg),
            ));
            foreach ($result as $system) {
              $settings['coder_modules-' . $system->name] = 1;
              $match = TRUE;
            }
            if (!isset($match)) {
              _coder_review_drush_print(dt('no matches found for @name', array(
                '@name' => $arg,
              )));
              return;
            }
            unset($settings['coder_active_modules']);
            unset($settings['coder_core']);
            unset($settings['coder_all']);
            unset($settings['coder_modules']);
            $settings['coder_includes'] = 1;
          }
          else {
            $root = dirname(__FILE__);
            if (_coder_review_drush_is_patch_arg($arg, $root)) {
              $settings['coder_patches'] = 1;
              $settings['coder_patch_link'] = $arg;
            }
            else {
              $path = realpath($arg);
              if (!file_exists($path)) {
                $path = preg_replace(",^{$root}/,", '', (function_exists('drush_getcwd') ? drush_getcwd() : getcwd()) . '/' . $arg);
                if (!file_exists($path)) {
                  $path = $root . '/' . $arg;
                  if (!file_exists($path)) {
                    $path = '';
                  }
                }
              }
              if ($path) {
                $settings['coder_files'] = 1;
                if (empty($settings['coder_file_list'])) {
                  $settings['coder_file_list'] = '';
                }
                if (is_dir($path)) {
                  $regex = '/\\.(' . implode('|', _coder_review_php_ext()) . '$)/';
                  $settings['coder_file_list'] .= implode("\n", _file_list($path, $regex));
                  $settings['coder_includes'] = 1;
                }
                else {
                  if (empty($settings['coder_file_lsit'])) {
                    $settings['coder_includes'] = 0;
                  }
                  $settings['coder_file_list'] .= $path . "\n";
                }
              }
              else {
                $settings['coder_modules-' . $arg] = 1;
                $settings['coder_includes'] = 1;
              }
            }
            unset($settings['coder_active_modules']);
            unset($settings['coder_core']);
            unset($settings['coder_all']);
            unset($settings['coder_modules']);
          }
          break;
      }
    }
  }
  if (drush_get_option('checkstyle')) {
    _coder_review_drush_xml_output_header('checkstyle');
  }
  elseif (drush_get_option('xml')) {
    _coder_review_drush_xml_output_header('xml');
  }
  if (!empty($severity_name)) {
    if (drush_get_option('xml')) {
      _coder_review_drush_xml_output_severity($severity_name);
    }
    $output[] = dt('Severity @severity_name', array(
      '@severity_name' => $severity_name,
    ));
    $settings['coder_severity'] = _coder_review_severity($severity_name);
  }
  if ($reviews) {
    if (drush_get_option('xml') && !drush_get_option('checkstyle')) {
      _coder_review_drush_xml_output_reviews($reviews, $avail_reviews);
    }
    else {
      foreach ($reviews as $review) {
        $output[] = $avail_reviews[$review]['#title'];
      }
    }
    $settings['coder_reviews'] = $reviews;
  }
  if (!empty($output) && !drush_get_option('summary') && !drush_get_option('xml') && !drush_get_option('checkstyle')) {
    _coder_review_drush_print(implode(', ', $output) . "\n");
  }
  if (_coder_review_drush_get_option_no('ignore')) {
    $settings['coder_ignore'] = 0;
  }
  _coder_review_set_form_modules($settings);
  $settings['op'] = 'drush';

  // Bootstrap the module.
  // @todo: separate this out better into an API, so we don't need the module.
  if (!function_exists('coder_review_page_form')) {
    require_once realpath(__DIR__) . '/coder_review.module';
  }
  $form_state['storage'] = $settings;
  coder_review_page_form(array(), $form_state);
  if (drush_get_option('checkstyle')) {
    _coder_review_drush_xml_output_footer('checkstyle');
  }
  elseif (drush_get_option('xml')) {
    _coder_review_drush_xml_output_footer('xml');
  }
}