filename); $_coder_coders[] = $file->name; } } /** * Get all of the code review modules */ function _coder_reviews() { $reviews = array(); // get the review definitions from the include directory global $_coder_coders; if ($_coder_coders) { foreach ($_coder_coders as $coder) { $function = $coder . '_reviews'; if (function_exists($function)) { if ($review = call_user_func($function)) { $reviews = array_merge($reviews, $review); } } } } // get the contributed module review definitions if ($review = module_invoke_all('reviews')) { $reviews = array_merge($reviews, $review); } return $reviews; } /** * Implementation of hook_help() */ function coder_help($section) { switch ($section) { case 'admin/modules#description': return t('Developer Module that assists with code review and version upgrade'); default : return; } } /** * Implementation of hook_cron(). * * TODO: move some of the work here... */ function coder_cron() { } /** * Implementation of hook_perm(). */ function coder_perm() { return array('view code review'); } /** * Implementation of hook_menu(). */ function coder_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/coder', 'title' => t('Code review'), 'callback' => 'coder_page', 'access' => user_access('view code review'), ); if (function_exists('drupal_system_listing')) { $items[] = array( 'path' => 'admin/settings/coder', 'title' => t('Code review'), 'description' => t('Select code review plugins and modules'), 'callback' => 'drupal_get_form', 'callback arguments' => 'coder_admin_settings', 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, ); } } return $items; } /** * Implementation of hook_form_alter(). */ function coder_form_alter($form_id, &$form) { if ($form_id == 'system_modules') { if (user_access('view code review')) { foreach ($form['name'] as $name => $data) { $form['name'][$name]['#value'] = l($data['#value'], "admin/coder/$name"); } } } } function _coder_default_reviews() { return drupal_map_assoc(array('drupal', 'security')); } /** * Implementation of hook_settings(). */ function coder_settings() { // get this variable once - do we want only active modules? $active = variable_get('coder_active_modules', 1); // create the list of review options from the coder review plug-ins $reviews = _coder_reviews(); foreach ($reviews as $name => $review) { $review_options[$name] = $review['#title']; } // what review standards should be applied $form['coder_reviews_group'] = array( '#type' => 'fieldset', '#title' => t('Reviews'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['coder_reviews_group']['coder_reviews'] = array( '#type' => 'checkboxes', '#options' => $review_options, '#description' => t('apply the checked coding reviews'), '#default_value' => variable_get('coder_reviews', _coder_default_reviews()), ); // get the modules $sql = "SELECT name, status FROM {system} WHERE type = 'module'"; if ($active == 1) { $sql .= " AND status=1"; } $sql .= " ORDER BY weight ASC, filename ASC"; $result = db_query($sql); while ($module = db_fetch_object($result)) { $name = $module->name; if (!$active && $module->status) { $name .= ' (active)'; } $module_options[$module->name] = l($name, "admin/coder/$name"); if ($module->status) { $default_coder_modules[] = $module->name; } } // display active modules option $form['coder_modules_group'] = array( '#type' => 'fieldset', '#title' => t('Modules'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['coder_modules_group']['coder_active_modules'] = array( '#type' => 'checkbox', '#default_value' => $active, '#title' => t('code review all active modules'), ); // display the modules in a fieldset $form['coder_modules_group']['coder_modules_subgroup'] = array( '#type' => 'fieldset', '#title' => t('Select Specific Modules'), '#collapsible' => TRUE, '#collapsed' => $active, ); $modules = $active ? $default_coder_modules : variable_get('coder_modules', $default_coder_modules); $form['coder_modules_group']['coder_modules_subgroup']['coder_modules'] = array( '#type' => 'checkboxes', '#options' => $module_options, '#description' => t('code review the selected modules only'), '#default_value' => $modules, ); return $form; } /** * Implementation of settings page for Drupal 5 */ function coder_admin_settings() { return system_settings_form(coder_settings()); } /** * Implementation of code review page */ function coder_page() { if (!user_access('view code review')) { return drupal_access_denied(); } // get this variable once - do we want only active modules? $active = variable_get('coder_active_modules', 1); // get this once - list of the reviews to perform $reviews = array(); $avail_reviews = _coder_reviews(); $selected_reviews = variable_get('coder_reviews', _coder_default_reviews()); foreach ($selected_reviews as $name => $checked) { if ($checked) { $reviews[$name] = $avail_reviews[$name]; } } // get the list of the modules $sql = "SELECT name, filename, status FROM {system} WHERE type = 'module'"; if ($active == 1) { $sql .= " AND status=1"; } $result = db_query($sql); while ($module = db_fetch_object($result)) { if ($module->status) { $default_coder_modules[$module->name] = $module->name; } $files[$module->name] = $module->filename; } // determine which modules are selected if ($module = arg(2)) { $modules = array($module => $module); $do_includes = 1; } else { $modules = $active ? $default_coder_modules : variable_get('coder_modules', $default_coder_modules); } if ($modules) { // loop through the selected modules, preparing the code review results $dups = array(); // used to avoid duplicate includes foreach ($modules as $name => $checked) { if ($checked) { // process this one file $filename = $files[$name]; $coder_args = array( '#reviews' => $reviews, '#name' => $name, '#filename' => $filename, ); $results = do_coder_reviews($coder_args); $output .= theme('coder', $name, $filename, $results); // process the same directory include files if ($do_includes) { // NOTE: convert to the realpath here so system_listing // doesn't return additional paths (i.e., try "module"). $path = str_replace('\\', '/', dirname(realpath($filename))); $offset = strpos($path, dirname($filename)); if (!isset($dups[$path])) { $dups[$path] = 1; $system_listing = (function_exists('drupal_system_listing')) ? 'drupal_system_listing' : 'system_listing'; $includefiles = $system_listing('.*\.inc$', $path, 'name', 0); foreach ($includefiles as $file) { $filename = drupal_substr($file->filename, $offset); $coder_args = array( '#reviews' => $reviews, '#name' => $name, '#filename' => $filename, ); $results = do_coder_reviews($coder_args); $output .= theme('coder', $name, $filename, $results); } } } } } // prepend any output with the list of code reviews performed if ($output) { foreach ($reviews as $review) { $items[] = l($review['#title'], $review['#link']); } $output = '
' . theme('reviews', $items, t('The code was checked using the following review guidelines')) . '
' . $output; } } return $output; } function do_coder_reviews($coder_args) { $results = array(); // get the path to the module file $filename = $coder_args['#filename']; if ($filename && $filepath = realpath($filename)) { // read the file $orig_lines = file($filepath); // replace strings inside quotes with less complicated strings // many thanks to http://ad.hominem.org/log/2005/05/quoted_strings.php $lines = $orig_lines; foreach (array('\'', '"') as $c) { /** * warning: string concatenation should be formatted with a space * separating the operators (dot .) and terms ('string') * * I broke with coding style because IMHO the extra spaces made this line * less readable, pushing this complicated regex onto multiple lines. * * I also want to make the point that the coder module is making * suggestions. You can bend or break them if there is good reason. */ $regex = '['.$c.']([^'.$c.'\\\\]*(?:\\\\.[^'.$c.'\\\\]*)*)['.$c.']'; $lines = preg_replace("/$regex/", "$c$c", $lines); } // strip single line comments // NOTE: do this after quote processing // to avoid removing comments that are actually in quotes $lines = preg_replace('/(\/\/[^:].*|\/\*.*\*\/)/', '', $lines); // remove multi-comments (original code from code-style.pl) foreach ($lines as $lineno => $line) { // catch multi-line comments if (isset($comment)) { if (preg_match('/.*\*\//', $line)) { $line = preg_replace('/.*\*\//', '', $line); unset($comment); } else { $line = ''; } } if (preg_match('/\/\*.*/', $line)) { $line = preg_replace('/\/\*.*/', '', $line); $comment = 1; } // save the new line, without the comments $lines[$lineno] = $line; } // add the files lines to the arguments $coder_args['#orig_lines'] = $orig_lines; $coder_args['#lines'] = $lines; // now do all of the code reviews foreach ($coder_args['#reviews'] as $review) { if ($result = do_coder_review($coder_args, $review)) { $results += $result; } } } // always display something if (count($results) == 0) { _coder_error_msg($results, t('No Problems Found')); } else { ksort($results, SORT_NUMERIC); } return $results; } function do_coder_review($coder_args, $review) { $results = array(); if ($review['#rules']) { foreach ($review['#rules'] as $rule) { $lines = $coder_args[isset($rule['#original']) ? '#orig_lines' : '#lines']; switch ($rule['#type']) { case 'regex': do_coder_review_regex($coder_args, $rule, $lines, $results); break; case 'grep': do_coder_review_grep($coder_args, $rule, $lines, $results); break; case 'callback': do_coder_review_callback($coder_args, $rule, $lines, $results); break; } } } return $results; } function do_coder_review_regex(&$coder_args, $rule, $lines, &$results) { if ($regex = $rule['#value']) { $regex = '/' . $regex . '/'; if (!isset($rule['#case'])) { $regex .= 'i'; } foreach ($lines as $lineno => $line) { if (preg_match($regex, $line)) { _coder_error($results, $rule, $lineno, $line); } } } } function _coder_error(&$results, $rule, $lineno = -1, $line = '') { if (isset($rule['#warning_callback'])) { if (function_exists($rule['#warning_callback'])) { $warning = $rule['#warning_callback'](); } else { // if this happens, there is an error in the rule definition $warning = t('please !report this !warning', array( '!report' => l('report', 'http://drupal.org/node/add/project_issue/coder/bug'), '!warning' => $rule['#warning_callback'], ) ); } } else { $warning = t($rule['#warning']); } return _coder_error_msg($results, $warning, $lineno, $line); } function _coder_error_msg(&$results, $warning, $lineno = -1, $line = '') { global $_coder_errno; $key = $lineno + 1 + ($coder_errno ++ / 100000); $results[$key] = theme('coder_warning', $lineno + 1, $warning); } function do_coder_review_grep(&$coder_args, $rule, $lines, &$results) { if ($regex = $rule['#value']) { $regex = '/' . $regex . '/'; if (!isset($rule['#case'])) { $regex .= 'i'; } foreach ($lines as $lineno => $line) { if (preg_match($regex, $line)) { return; } } _coder_error($results, $rule); } } function do_coder_review_callback(&$coder_args, $rule, $lines, &$results) { if ($function = $rule['#value']) { if (function_exists($function)) { call_user_func_array($function, array(&$coder_args, $rule, $lines, &$results)); } } } /** * Theming functions below... */ function theme_reviews($items, $message = '') { return $message . theme('item_list', $items, NULL, 'ol'); } function theme_coder($name, $filename, $results) { $title = ($_GET['q'] == "admin/coder/$name") ? $filename : l($name, "admin/coder/$name"); $output = '

' . $title . '

'; if (count($results)) { $output .= theme('item_list', $results); } $output .= '
'; return $output; } function theme_coder_warning($lineno, $warning) { if ($lineno) { return '
' . t('Line') . ' ' . $lineno . ': ' . $warning . '
'; } return '
' . $warning . '
'; } function theme_drupalapi($function, $version = 'HEAD') { return l($function, "http://api.drupal.org/api/$version/function/$function"); }