You are here

function coder_review_page_form in Coder 7

Same name and namespace in other branches
  1. 7.2 coder_review/coder_review.module \coder_review_page_form()

Implements hook_form().

Implements coder_review's main form, in which a user can select reviews and modules/themes to run them on.

1 call to coder_review_page_form()
coder_review_drush_review in coder_review/coder_review.drush.inc
Do the actual review.
1 string reference to 'coder_review_page_form'
coder_review_page in coder_review/coder_review.module
Code review page.

File

coder_review/coder_review.module, line 781

Code

function coder_review_page_form($form, &$form_state, $arg = '') {
  if (isset($form_state['storage'])) {
    $settings = $form_state['storage'];
    $settings['coder_modules'] = _coder_review_settings_array($form_state, 'module');
    $settings['coder_themes'] = _coder_review_settings_array($form_state, 'theme');
    drupal_set_title(t('Code review (submitted options)'), PASS_THROUGH);
  }
  else {
    $settings = _coder_review_get_default_settings($arg);
    if ($arg) {
      drupal_set_title(t('Code review (@options)', array(
        '@options' => isset($arg) ? $arg : 'default options',
      )), PASS_THROUGH);
    }
  }

  // Get this once: list of the reviews to perform.
  $reviews = array();
  $avail_reviews = _coder_review_reviews();
  $selected_reviews = $settings['coder_reviews'];
  foreach ($selected_reviews as $name => $checked) {
    if ($checked) {
      $reviews[$name] = $avail_reviews[$name];
    }
  }
  if ($coder_form = _coder_review_settings_form($settings, $system, $files)) {

    // Get the includes_exclusions settings once, it is used multiple times.
    $coder_includes_exclusions = isset($settings['coder_includes_exclusions']) ? $settings['coder_includes_exclusions'] : '';

    // Get the list of lines to ignore.
    $ignores = coder_review_get_ignores();

    // Code review non-module core files.
    $module_weight = 0;
    if (isset($settings['coder_core']) && $settings['coder_core']) {
      $php_extensions = variable_get('coder_review_php_ext', array(
        'inc',
        'php',
        'install',
        'test',
      ));
      $coder_args = array(
        '#reviews' => $reviews,
        '#severity' => $settings['coder_severity'],
        '#php_extensions' => $php_extensions,
        '#include_extensions' => array(),
        //'#filename' => $filename,
        '#cache' => TRUE,
      );
      drupal_alter('coder_review_args', $coder_args);
      $form['core_php'] = array(
        '#type' => 'fieldset',
        '#title' => 'core (php)',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => ++$module_weight,
      );
      $phpfiles = file_scan_directory('.', '/.*\\.php/', array(
        'recurse' => FALSE,
        'key' => 'name',
      ));
      _coder_review_page_form_includes($form, $coder_args, 'core_php', $phpfiles, 2, 0, $coder_includes_exclusions, $ignores);
      $form['core_includes'] = array(
        '#type' => 'fieldset',
        '#title' => 'core (includes)',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => ++$module_weight,
      );
      $includefiles = drupal_system_listing('/.*\\.inc$/', 'includes', 'filepath', 0);
      _coder_review_page_form_includes($form, $coder_args, 'core_includes', $includefiles, 0, $coder_includes_exclusions, $ignores);
    }

    // Loop through the selected modules and themes.
    if (isset($system)) {

      // Determine which file extensions to include, based on the rules.
      $php_extensions = variable_get('coder_review_php_ext', array(
        'inc',
        'php',
        'install',
        'test',
      ));
      $include_extensions = _coder_review_get_reviews_extensions($php_extensions, $reviews);
      $includes = array_merge($php_extensions, $include_extensions);

      // Used to avoid duplicate includes.
      $dups = array();
      $stats = array();
      $patch = '';
      foreach ($system as $name => $checked) {
        if ($checked) {

          // Process this one file.
          $filename = isset($files[$name]) ? $files[$name] : FALSE;
          if (!$filename) {
            drupal_set_message(t('Code review file for %module not found', array(
              '%module' => $name,
            )), 'error');
            continue;
          }
          if (!empty($settings['coder_patches'])) {
            $patch = $filename;
            $filename = $name;
          }
          $coder_args = array(
            '#reviews' => $reviews,
            '#severity' => $settings['coder_severity'],
            '#filename' => $filename,
            '#patch' => $patch,
            '#php_extensions' => $php_extensions,
            '#include_extensions' => $include_extensions,
            '#ignore_lines' => isset($ignores[$filename]) && is_array($ignores[$filename]) ? $ignores[$filename] : array(),
            '#cache' => TRUE,
          );
          drupal_alter('coder_review_args', $coder_args);
          $results = do_coder_reviews($coder_args);
          $stats[$filename] = $results['#stats'];
          unset($results['#stats']);
          module_invoke_all('coder_review_results_view', $results);

          // Output the results in a collapsible fieldset.
          $form[$name] = array(
            '#type' => 'fieldset',
            '#title' => $filename,
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#weight' => ++$module_weight,
          );
          if (empty($results)) {
            $results[] = t('No Problems Found');
          }
          else {
            $form[$name]['#collapsed'] = FALSE;
          }
          $form[$name]['output'] = _coder_review_form_output($name, $filename, $results);
          $form[$name]['output']['#weight'] = -1;

          // Process the same directory include files.
          if (!empty($settings['coder_includes'])) {

            // NOTE: Convert to the realpath here so drupal_system_listing
            // doesn't return additional paths (i.e., try "module").
            if ($path = str_replace('\\', '/', dirname(realpath($filename)))) {
              if (!isset($dups[$path])) {
                if (drupal_substr($filename, -7) == '.module') {
                  $coder_args['#php_minor'] = 1;
                }
                $dups[$path] = 1;
                $regex = '/\\.(' . implode('|', $includes) . ')$/';
                $includefiles = drupal_system_listing($regex, dirname($filename), 'filepath', 0);
                $offset = strpos($filename, dirname($filename));
                $stats[$filename]['#includes'] = _coder_review_page_form_includes($form, $coder_args, $name, $includefiles, $offset, $coder_includes_exclusions, $ignores);
              }
            }
          }
        }
      }
      if (count($stats)) {
        $summary = array(
          'files' => 0,
          'minor' => 0,
          'normal' => 0,
          'critical' => 0,
          'ignored' => 0,
        );
        foreach ($stats as $stat) {
          $summary['minor'] += $stat['minor'];
          $summary['normal'] += $stat['normal'];
          $summary['critical'] += $stat['critical'];
          $summary['ignored'] += $stat['ignored'];
          if (isset($stat['#includes'])) {
            foreach ($stat['#includes'] as $includestat) {
              $summary['files']++;
              $summary['minor'] += $includestat['minor'];
              $summary['normal'] += $includestat['normal'];
              $summary['critical'] += $includestat['critical'];
              $summary['ignored'] += $includestat['ignored'];
            }
          }
          $summary['files']++;
        }
        $display = array();
        if (empty($settings['coder_patches'])) {
          $display[] = t('Coder found @count projects', array(
            '@count' => count($stats),
          ));
          $display[] = t('@count files', array(
            '@count' => $summary['files'],
          ));
        }
        else {
          $display[] = t('Coder found @count patch hunks', array(
            '@count' => count($stats),
          ));
        }
        foreach (array(
          'critical',
          'normal',
          'minor',
        ) as $severity_name) {
          if ($summary[$severity_name] > 0) {
            $display[] = t('@count %severity_name warnings', array(
              '@count' => $summary[$severity_name],
              '%severity_name' => $severity_name,
            ));
          }
        }
        $display[] = t('@count warnings were flagged to be ignored', array(
          '@count' => $summary['ignored'],
        ));
        drupal_set_message(implode(', ', $display));
        if (function_exists('_coder_review_drush_is_option') && _coder_review_drush_is_option('drush')) {
          print coder_review_print_drush_messages();
        }
      }
    }

    // Prepend the settings form.
    $form['settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Selection form'),
      '#collapsible' => TRUE,
      '#collapsed' => !empty($form),
      '#weight' => -1,
    );
    if ($form['settings']['#collapsed']) {
      $form['settings']['#prefix'] = t('<div class="coder-settings">Use the Selection form to select options for this code review, or change the <a href="@settings">Default Settings</a> and use the <a href="@default">Default</a> tab above.</div>', array(
        '@settings' => url('admin/config/development/coder/review/settings'),
        '@default' => url('admin/config/development/coder/review/default'),
      ));
    }
    $form['settings'][] = $coder_form;
    $form['settings']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Run reviews'),
    );
    $form['#attached']['css'] = array(
      drupal_get_path('module', 'coder_review') . '/coder_review.css',
    );
  }
  return $form;
}