You are here

function coder_review_page_form in Coder 7.2

Same name and namespace in other branches
  1. 7 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.

See also

_coder_review_settings_array()

_coder_review_settings_form()

1 call to coder_review_page_form()
drush_coder_review in coder_review/coder_review.drush.inc
Performs the actual review for drush.
3 string references to 'coder_review_page_form'
coder_review_menu in coder_review/coder_review.module
Implements hook_menu().
coder_review_menu_alter in coder_review/coder_review.module
Implements hook_menu_alter().
drush_coder_review in coder_review/coder_review.drush.inc
Performs the actual review for drush.

File

coder_review/coder_review.module, line 552
Developer module to assist with coder reviews and API upgrade suggestions.

Code

function coder_review_page_form($form, &$form_state, $arg = '') {

  // Bootstrap enough to run.
  if (!function_exists('_coder_review_reviews')) {
    require_once 'coder_review.common.inc';
  }
  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');
    if (function_exists('drupal_set_title')) {
      drupal_set_title(t('Code review (submitted options)'), PASS_THROUGH);
    }
  }
  else {
    $settings = _coder_review_get_default_settings($arg);
    if ($arg && function_exists('drupal_set_title')) {
      drupal_set_title(t('Code review (@options)', array(
        '@options' => isset($arg) ? $arg : 'default options',
      )), PASS_THROUGH);
    }
  }
  $settings += array(
    'coder_patches' => '',
    'coder_patch_link' => '',
    'coder_patch_text' => '',
    'coder_all' => 0,
    'coder_core' => 0,
    'coder_contrib' => 0,
    'coder_includes' => 0,
    'coder_includes_exclusions' => '',
    'coder_files' => 0,
    'coder_files_list' => '',
    'coder_active_modules' => 0,
    'coder_ignore' => 1,
  );
  $coder_form = _coder_review_settings_form($settings, $system, $files);
  if ($coder_form) {

    // Get this list of the reviews to perform.
    $avail_reviews = _coder_review_reviews();
    $reviews = array();
    foreach ($settings['coder_reviews'] as $name => $checked) {
      if ($checked) {
        $reviews[$name] = $avail_reviews[$name];
      }
    }

    // Code review non-module core files.
    $module_weight = 0;
    if ($settings['coder_core'] && function_exists('drupal_alter')) {
      $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(),
        '#cache' => TRUE,
        '#settings_ignore' => $settings['coder_ignore'],
      );
      drupal_alter('coder_review_args', $coder_args);

      // Review core PHP files.
      $form['core_php'] = array(
        '#type' => 'fieldset',
        '#title' => 'core (php)',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => ++$module_weight,
      );
      $phpfiles = _file_list('.', '/\\.php$/', FALSE);
      sort($phpfiles);
      _coder_review_page_form_includes($form, $coder_args, 'core_php', $phpfiles, 2, 0, $settings['coder_includes_exclusions']);

      // Review core include files.
      $form['core_includes'] = array(
        '#type' => 'fieldset',
        '#title' => 'core (includes)',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => ++$module_weight,
      );
      $includefiles = _file_list('includes', '/\\.inc$/');
      sort($includefiles);
      _coder_review_page_form_includes($form, $coder_args, 'core_includes', $includefiles, 0, $settings['coder_includes_exclusions']);
    }

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

      // Determine which file extensions to include, based on the rules.
      $php_extensions = _coder_review_php_ext();
      $include_extensions = _coder_review_get_reviews_extensions($php_extensions, $reviews);
      $includes = array_merge($php_extensions, $include_extensions);

      // Avoid duplicate includes.
      $dups = $stats = array();
      $patch = '';
      foreach ($system as $name => $checked) {
        if ($checked) {
          $filename = isset($files[$name]) ? $files[$name] : FALSE;
          if (!$filename) {
            _message(t('Code review file for %module not found', array(
              '%module' => $name,
            )), 'error');
            continue;
          }
          if ($settings['coder_patches']) {
            $patch = $filename;
            $filename = $name;
          }
          $pathinfo = pathinfo($filename);
          $coder_args = array(
            '#reviews' => $reviews,
            '#severity' => $settings['coder_severity'],
            '#filename' => $filename,
            '#patch' => $patch,
            '#php_extensions' => $php_extensions,
            '#include_extensions' => $include_extensions,
            '#cache' => TRUE,
            '#settings_ignore' => $settings['coder_ignore'],
          );
          if (function_exists('drupal_alter')) {
            drupal_alter('coder_review_args', $coder_args);
          }

          // Process this one file.
          $results = do_coder_reviews($coder_args);
          $stats[$filename] = $results['#stats'];
          unset($results['#stats']);
          if (function_exists('module_invoke_all')) {
            module_invoke_all('coder_review_results_view', $results);
          }

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

          // Process the same directory include files.
          if ($settings['coder_includes']) {
            $path = str_replace('\\', '/', dirname(realpath($filename)));
            if ($path && !isset($dups[$path])) {
              if ($pathinfo['extension'] == 'module') {
                $coder_args['#php_minor'] = 1;
              }
              $dups[$path] = 1;
              $regex = '/\\.(' . implode('|', $includes) . ')$/';
              $includefiles = _file_list($pathinfo['dirname'], $regex);
              $includefiles = array_diff($includefiles, $system);
              if ($includefiles) {
                sort($includefiles);
                $offset = strpos($filename, dirname($filename));
                $stats[$filename]['#includes'] = _coder_review_page_form_includes($form, $coder_args, $name, $includefiles, $offset, $settings['coder_includes_exclusions']);
              }
            }
          }
        }
      }
      if ($stats) {
        $summary = array(
          'files' => 0,
          'sums' => array(
            'minor' => 0,
            'normal' => 0,
            'critical' => 0,
            'ignored' => 0,
          ),
        );
        foreach ($stats as $stat) {
          ++$summary['files'];
          foreach (array_keys($summary['sums']) as $key) {
            $summary['sums'][$key] += $stat[$key];
          }
          if (isset($stat['#includes'])) {
            foreach ($stat['#includes'] as $includestat) {
              ++$summary['files'];
              foreach (array_keys($summary['sums']) as $key) {
                $summary['sums'][$key] += $includestat[$key];
              }
            }
          }
        }
        if ($settings['coder_patches']) {
          $display[] = _t('Coder found @count patch hunks', array(
            '@count' => count($stats),
          ));
        }
        else {
          $display[] = _t('Coder found @count projects', array(
            '@count' => count($stats),
          ));
          $display[] = _t('@count files', array(
            '@count' => $summary['files'],
          ));
        }
        foreach (array(
          'critical',
          'normal',
          'minor',
        ) as $severity_name) {
          if ($summary['sums'][$severity_name] > 0) {
            $display[] = _t('@count %severity_name warnings', array(
              '@count' => $summary['sums'][$severity_name],
              '%severity_name' => $severity_name,
            ));
          }
        }
        $display[] = _t('@count warnings were flagged to be ignored', array(
          '@count' => $summary['sums']['ignored'],
        ));
        _message(implode(', ', $display));
        if (_drush()) {
          coder_review_print_drush_messages($summary);
        }
      }
    }
    if (!_drush()) {

      // Prepend the settings form.
      $form['settings'] = array(
        '#type' => 'fieldset',
        '#title' => _t('Selection form'),
        '#collapsible' => TRUE,
        '#collapsed' => !empty($form),
        '#weight' => -2,
      );
      $form['disclaimer'] = array(
        '#prefix' => '<div class="coder-disclaimer">',
        '#markup' => coder_review_help('coder_review#disclaimer', array()),
        '#suffix' => '</div>',
        '#weight' => -3,
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => _t('Run reviews'),
        '#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;
      if (function_exists('drupal_get_path')) {
        $form['#attached']['css'][] = _coder_review_path() . '/coder_review.css';
      }
    }
  }
  return $form;
}