Skip to content
coder_style.inc 6.37 KiB
Newer Older
Doug Green's avatar
Doug Green committed
<?php
// $Id$

 * This include file implements coder functionality for Drupal Standards.
Doug Green's avatar
Doug Green committed
  $br = 'br';
  $rules = array(
    array(
      '#type' => 'regex',
      '#value' => '\t',
      '#warning' => 'Use an indent of 2 spaces, with no tabs',
    ),
    array(
      '#type' => 'regex',
      '#value' => '^ (  )*[^ \'".]',
      '#warning' => 'Use an indent of 2 spaces, with no tabs',
      '#severity' => 'minor',
    ),
Doug Green's avatar
Doug Green committed
    array(
      '#type' => 'regex',
      '#value' => '\s(if|elseif|while|foreach|switch|return|for|catch)\(',
Doug Green's avatar
Doug Green committed
      '#warning' => 'Control statements should have one space between the control keyword and opening parenthesis',
    ),
    array(
      '#type' => 'regex',
      '#value' => '[\s\(](\w+)\s\(',
      '#not' => '^(if|elseif|while|foreach|switch|return|for|list|catch)$',
Doug Green's avatar
Doug Green committed
      '#warning' => 'Functions should be called with no spaces between the function name',
    ),
    array(
      '#type' => 'regex',
      '#value' => '\){',
      '#warning' => 'use a space between the closing parenthesis and the open bracket',
    ),
    array(
      '#type' => 'regex',
      '#value' => '(\S=>|=>\S)',
      '#warning' => 'Arrays should be formatted with a space separating each element and assignment operator',
    ),
    array(
      '#type' => 'regex',
      '#value' => '(\.\s\'\'|\'\'\s\.|\.\s""|\.\s"")',
      '#warning' => 'string concatenation should be formatted without a space separating the operators (dot .) and a quote',
    ),
    array(
      '#type' => 'regex',
      '#value' => '([^\'\"\s0-9]\.|\.[^\'\"\=\s0-9])',
      '#warning' => 'string concatenation should be formatted with a space separating the operators (dot .) and non-quote terms',
Doug Green's avatar
Doug Green committed
    ),
    array(
      '#type' => 'regex',
      '#value' => '<\?(\w+)',
      '#not' => '^(php|xml)$',
      '#warning' => 'Always use &lt;?php ?&gt; to delimit PHP code, not the &lt;? ?&gt; shorthand',
    ),
Doug Green's avatar
Doug Green committed
    array(
Doug Green's avatar
Doug Green committed
      '#type' => 'regex',
      '#value' => 'global\s+\$(\w+)(,\s\$(\w+))*',
      '#not' => '^_|^('. _coder_style_core_global_regex() .')$',
Doug Green's avatar
Doug Green committed
      '#warning' => 'global variables should start with a single underscore followed by the module and another underscore',
    ),
    array(
      '#type' => 'callback',
Doug Green's avatar
Doug Green committed
      '#source' => 'all',
      '#value' => '_coder_style_callback',
Doug Green's avatar
Doug Green committed
    ),
    array(
      '#type' => 'regex',
      '#value' => '}\s*else',
      '#warning' => 'else statements should begin on a new line',
    ),
    array(
      '#type' => 'regex',
      '#value' => '[,][^ \n\r]',
      '#warning' => 'missing space after comma',
    ),
    array(
      '#type' => 'regex',
      '#value' => '^\s*{',
      '#warning' => 'curly braces { should end a line, not start one',
    ),
    array(
      '#type' => 'regex',
      '#value' => '(?-i)(function\s+|\$)(([a-z]+[A-Z]+([a-z]*[A-Z]*)*)|([A-Z]+[a-z]+([A-Z]*[a-z]*)*))',
Doug Green's avatar
Doug Green committed
      '#warning' => 'do not use mixed case (camelCase), use lower case and _',
    ),
    array(
      '#type' => 'regex',
      '#value' => '\s(stdclass)\s*\(',
      '#not' => '^(?-i)stdClass$',
      '#warning' => 'use stdClass caseCapitalization, it\'s the one exception to the mixed case style standard',
    ),
    array(
      '#type' => 'regex',
      '#source' => 'html',
      '#value' => '<'. $br .'>', // NOTE: use $br only to avoid a warning.
Doug Green's avatar
Doug Green committed
      '#warning' => 'use &lt;br /&gt; instead of &lt;br&gt;',
      '#severity' => 'minor',
Doug Green's avatar
Doug Green committed
    ),
    array(
      '#type' => 'regex',
      '#source' => 'html',
      '#value' => '(?-i)<[A-Z]+',
      '#warning_callback' => '_coder_style_xhtml_warning',
      '#value' => '\[\s*[a-z][a-z0-9_]+\\s*]',
      '#warning' => 'use quotes around a string literal array index, this is not only a style issue, but a known performance problem',
Doug Green's avatar
Doug Green committed
  );
  $review = array(
    '#title' => t('Drupal Coding Standards'),
    '#link' => 'http://drupal.org/node/318',
    '#rules' => $rules,
    '#description' => t('every developer should use'),
  return array('style' => $review);
 * Define the rule callbacks for style.
function _coder_style_callback(&$coder_args, $review, $rule, $lines, &$results) {
Doug Green's avatar
Doug Green committed
  for ($lineno = -1; $last = array_slice($lines, $lineno); $lineno --) {
    $lastline = $last[0];
    if (preg_match('/\S/', $lastline)) {
      break;
    }
  }
  if ($last && $lastline && preg_match('/\?>\s*$/i', $lastline)) {
Doug Green's avatar
Doug Green committed
    $severity_name = _coder_severity_name($coder_args, $review, $rule);
    _coder_error_msg($results, 'the final ?> should be omitted from all code files', $severity_name, count($lines));
function _coder_style_core_global_regex() {
Doug Green's avatar
Doug Green committed
  static $coreglobalregex, $coreglobalvars;
  if (!isset($coreglobalregex)) {
    // Note: there's a little extra overhead in formatting this list as an
    // array, but I think it makes it more readable and maintainable.
Doug Green's avatar
Doug Green committed
    $coreglobalvars = array(
      // From the Drupal 5 includes/ directory.
Doug Green's avatar
Doug Green committed
      'active_db',
      'base_path',
      'base_root',
      'base_url',
      'conf',
      'custom_theme',
      'db_prefix',
      'db_type',
      'db_url',
      'form_button_counter',
      'form_submitted',
      'form_values',
      'install_locale',
      'installed_profile',
      'language',
Doug Green's avatar
Doug Green committed
      'last_result',
      'locale',
      'multibyte',
      'pager_page_array',
      'pager_total',
      'pager_total_items',
      'profile',
      'queries',
      'sidebar_indicator',
Doug Green's avatar
Doug Green committed
      'theme',
      'theme_engine',
      'theme_key',
      'theme_path',
Doug Green's avatar
Doug Green committed
      'timers',
      'user',
      'xrds_services',
      'xrds_open_elements',
      'xrds_current_service',
      // From the Drupal 5 modules/ directory -
Doug Green's avatar
Doug Green committed
      // Note: IMHO these should not be allowed, but until we fix core,
      // other modules will need them.
Doug Green's avatar
Doug Green committed
      'channel',
      'element',
      'forum_topic_list_header',
      'id',
      'image',
      'item',
      'items',
      'last_change',
      'last_nid',
      'nid',
      'recent_activity',
      'tag',
    );
    $coreglobalregex = implode('|', $coreglobalvars);
  }
  return $coreglobalregex;
}
 * Define the warning callbacks.
 */

function _coder_style_xhtml_warning() {
  return t('use lowercase html tags to comply with <a href="@xhtml">XHTML</a>',
      '@xhtml' => 'http://www.w3.org/TR/xhtml1/#h-4.2',