Corsix is doing a GHOP task for me and identified several coding violations that are not being flagged by Coder: http://drupal.org/node/211739#comment-700563

Comments

stella’s picture

Status: Active » Needs review
StatusFileSize
new1.21 KB

The attached patch will now alert on trailing spaces and control structures which are all on the one line.

Doug - if you decide that trailing spaces shouldn't be allowed in the code, then we're going to have to do a tidy up of the coder module itself. I can create a patch for this if you want.

Cheers,
Stella

nancydru’s picture

patching file tests\coder_style.inc
"patch unexpectedly ends in the middle of line"
HUNK #1 ended at 80

This could very well be a user error.

stella’s picture

StatusFileSize
new1.68 KB

Yes, I think there was something wrong with that last one, so I've regenerated it. Sorry for the hassle.

Cheers,
Stella

nancydru’s picture

Well, you apparently have some other patches I don't have, so I can't apply this.

stella’s picture

Ah I'm patching against 6.x (CVS version). I think there are some differences between 5.x and 6.x versions due to some recent comment changes. It looks like those comment changes weren't applied to the 5.x branch yet. Otherwise the files should be identical.

Cheers,
Stella

nancydru’s picture

And I'm trying to apply to 5.x

douggreen’s picture

I think I read in another thread that webchick added the trailing spaces to the trailing spaces to the coding standards document. So, yes we should implement it. And yes, I removed all the trailing spaces from coder.

stella’s picture

StatusFileSize
new1.68 KB

Attached a new version of the patch (again 6.x) as it was giving false hits on lines like:

$var = 123; // initialising variable

Changing the #source from 'php' to 'all fixed this.

Patch for 5.x coming as soon as I finish comparing 5.x and 6.x include files.

Cheers,
Stella

douggreen’s picture

I'm looking at this line in the patch and wondering why check for {.*}.

+      '#value' => '\s(if|elseif|while|foreach|switch|return|for|catch)\s*\(.*\)\s*{.*}',

In other words, why are you looking for the trailing }. Should we just flag anything that's not a space or comment. For example, something like {[^\s]+ would catch:

  if (something) { $x = true;
  }
stella’s picture

StatusFileSize
new1.75 KB

Good point, I hadn't considered that scenario. However your rule above didn't quite work - it would catch if (something) {$x = true; but not if (something) { $x = true;. So I changed the rule to:

'#value' => '\s(if|elseif|while|foreach|switch|return|for|catch)\s*\(.*\)\s*{\s*[^\s]+',

I've re-rolled the patch, see attached.

Cheers,
Stella

douggreen’s picture

This looks better. I think that the error text "control structures should not be all on one line" is not very descriptive for the actual error we are underscoring. What do you think?

douggreen’s picture

Title: FYI » FYI: Warn about trailing spaces and missing newlines

This title might not be the best, but it's better than "FYI"!

nancydru’s picture

Category: support » feature
Priority: Minor » Normal

@doug - agreed on the title. When I created the issue, I had no idea it would generate so much effort. Also, I think it's really now a feature request.

@stella - ah, that explains why it wasn't catching my control structures (yes, I'd like to see a better message). I always put a blank after the brackets. Thanks for catching that.

stella’s picture

Category: feature » support
Priority: Normal » Minor
StatusFileSize
new1.79 KB

Hmmm, could change it to control statements should be on a separate line ? I re-rolled a patch that uses this instead, but feel free to suggest alternative wordings.

Nancy - if you check out the latest version of the 5.x branch, the attached patch should apply to it and the latest 6.x version.

Cheers,
Stella

nancydru’s picture

On "includes\coder_style.inc":

patch: **** malformed patch at line 11: t only a style issue, but a known performance problem',

douggreen’s picture

Status: Needs review » Fixed

I fixed the patch, slightly changed the comment, and committed it. Thanks!

nancydru’s picture

okie dokie

nancydru’s picture

Category: support » bug
Priority: Minor » Critical
Status: Fixed » Needs work

I just downloaded the both the 5.x and 6.x versions and ran it on the Faq_Ask module and got false hits on virtually every line.

douggreen’s picture

It is displaying the correct error on coder and coder tests. Could you be specific about which version and lines of Faq_Ask are giving valse hits.

stella’s picture

i just downloaded the latest faq ask module version (5.x-1.0 beta4) and ran a code review on it. Each error regarding control structures being all on one line and trailing whitespaces appear to be correct when I looked at the code itself.

Cheers,
Stella

nancydru’s picture

StatusFileSize
new35.92 KB

faq_ask 5.x-1.x-dev - I'm attaching the code

If there are blanks there, I have no editor that can detect them and I have tried several, including MS Word.

Here's a start:
#
Line 1: There should be no trailing spaces

<?php

#
severity: normalLine 2: There should be no trailing spaces

// $Id: faq_ask.module,v 1.22 2008/01/31 03:36:53 nancyw Exp $

#
severity: normalLine 4: There should be no trailing spaces

////////////////////////////////////////////////////

#
severity: normalLine 5: There should be no trailing spaces

// TODO:

#
severity: normalLine 6: There should be no trailing spaces

////////////////////////////////////////////////////

#
severity: normalLine 8: There should be no trailing spaces

/**

#
severity: normalLine 9: There should be no trailing spaces

* Display help and module information

#
severity: normalLine 10: There should be no trailing spaces

* @param section which section of the site we're displaying help

#
severity: normalLine 11: There should be no trailing spaces

* @return help text for section

#
severity: normalLine 12: There should be no trailing spaces

*/

#
severity: normalLine 13: There should be no trailing spaces

function faq_ask_help($section='') {

#
severity: normalLine 14: There should be no trailing spaces

$output = '';

#
severity: normalLine 16: There should be no trailing spaces

switch ($section) {

#
severity: normalLine 17: There should be no trailing spaces

case 'admin/help#faq_ask':

#
severity: normalLine 18: There should be no trailing spaces

$output .= '

'. t("This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.") .'

'.

#
severity: normalLine 19: There should be no trailing spaces

'

'. t("The module shows an abbreviated version of the FAQ form without an answer field. The node is created without the 'published' attribute. There is a block that will show the unanswered questions to the 'expert' (generally, this requires a separate role).") .'

'.

#
severity: normalLine 20: There should be no trailing spaces

'

'. t("Viewing of the completed question and answer pair is done by the FAQ module.") .'

'.

#
severity: normalLine 21: There should be no trailing spaces

'

'. t("Simply adding the 'FAQ' content type to a vocabulary will not make it eligible for experts; you must go to the settings page and add it there.") .'

';

#
severity: normalLine 22: There should be no trailing spaces

return $output;

#
severity: normalLine 24: There should be no trailing spaces

case 'faq_ask/'. arg(1):

#
severity: normalLine 25: There should be no trailing spaces

case 'faq_ask':

#
severity: normalLine 26: There should be no trailing spaces

return t("Add a question for our expert to answer. After being answered, your question and the answer will be displayed in the FAQ pages. If the question will not fit in the box below, please try to rephrase it.");

#
severity: normalLine 27: There should be no trailing spaces

}

#
severity: normalLine 28: There should be no trailing spaces

}

#
severity: normalLine 30: There should be no trailing spaces

/**

#
severity: normalLine 31: There should be no trailing spaces

* Implementation of hook_perm()

#
severity: normalLine 32: There should be no trailing spaces

* Define the permissions this module uses

#
severity: normalLine 33: There should be no trailing spaces

*/

#
severity: normalLine 34: There should be no trailing spaces

function faq_ask_perm() {

#
severity: normalLine 35: There should be no trailing spaces

return array('ask question', 'answer question');

#
severity: normalLine 36: There should be no trailing spaces

}

#
severity: normalLine 38: There should be no trailing spaces

/**

#
severity: normalLine 39: There should be no trailing spaces

* Implementation of hook_access()

#
severity: normalLine 40: There should be no trailing spaces

*/

#
severity: normalLine 41: There should be no trailing spaces

function faq_ask_access($op, $node) {

#
severity: normalLine 42: There should be no trailing spaces

global $user;

#
severity: normalLine 43: The control statement should be on a separate line from the control conditional

if ($op == 'create') { return user_access(t('ask_question')) || user_access('answer question'); }

#
severity: normalLine 43: There should be no trailing spaces

if ($op == 'create') { return user_access(t('ask_question')) || user_access('answer question'); }

#
severity: normalLine 44: There should be no trailing spaces

else { return user_access('answer question') || user_access('edit_own_faq'); }

#
severity: normalLine 45: There should be no trailing spaces

}

#
severity: normalLine 47: There should be no trailing spaces

/**

#
severity: normalLine 48: There should be no trailing spaces

* Implementation of hook_enable()

#
severity: normalLine 49: There should be no trailing spaces

* This function reminds the user to configure the module.

#
severity: normalLine 50: There should be no trailing spaces

*/

#
severity: normalLine 51: There should be no trailing spaces

function faq_ask_enable() {

#
severity: normalLine 52: There should be no trailing spaces

drupal_set_message(t('The Faq_Ask module has been enabled.') .' '. t('Please go to the settings page to configure this module.', array('!url' => url('admin/settings/faq/ask'))), 'notice');

#
severity: normalLine 53: There should be no trailing spaces

}

#
severity: normalLine 55: There should be no trailing spaces

/**

#
severity: normalLine 56: There should be no trailing spaces

* Implementation of hook_menu()

#
severity: normalLine 57: There should be no trailing spaces

*/

#
severity: normalLine 58: There should be no trailing spaces

function faq_ask_menu($may_cache) {

#
severity: normalLine 59: There should be no trailing spaces

$items = array();

#
severity: normalLine 61: There should be no trailing spaces

if ($may_cache) {

#
severity: normalLine 62: There should be no trailing spaces

$items[] = array(

#
severity: normalLine 63: There should be no trailing spaces

'path' => 'admin/settings/faq/ask',

#
severity: normalLine 64: There should be no trailing spaces

'title' => t('Experts'),

#
severity: normalLine 65: There should be no trailing spaces

'callback' => 'drupal_get_form',

#
severity: normalLine 66: There should be no trailing spaces

'callback arguments' => array('faq_ask_settings_form'),

#
severity: normalLine 67: There should be no trailing spaces

'access' => user_access('administer faq'),

#
severity: normalLine 68: There should be no trailing spaces

'description' => t('Allows the user to configure the Ask_FAQ module.'),

#
severity: normalLine 69: There should be no trailing spaces

'type' => MENU_LOCAL_TASK,

#
severity: normalLine 70: There should be no trailing spaces

'weight' => -2,

#
severity: normalLine 71: There should be no trailing spaces

);

#
severity: normalLine 73: There should be no trailing spaces

$items[] = array(

#
severity: normalLine 74: There should be no trailing spaces

'path' => 'faq_ask',

#
severity: normalLine 75: There should be no trailing spaces

'title' => t('Ask a question'),

#
severity: normalLine 76: There should be no trailing spaces

'callback' => 'faq_ask_page',

#
severity: normalLine 77: There should be no trailing spaces

'access' => user_access('ask question'),

#
severity: normalLine 78: There should be no trailing spaces

);

#
severity: normalLine 79: There should be no trailing spaces

}

#
severity: normalLine 80: There should be no trailing spaces

else {

#
severity: normalLine 81: There should be no trailing spaces

$items[] = array(

#
severity: normalLine 82: There should be no trailing spaces

'path' => 'faq_ask/answer',

#
severity: normalLine 83: There should be no trailing spaces

'title' => t('Answer a question'),

#
severity: normalLine 84: There should be no trailing spaces

'callback' => 'faq_ask_answer',

#
severity: normalLine 85: There should be no trailing spaces

'access' => user_access('answer question'),

#
severity: normalLine 86: There should be no trailing spaces

'type' => MENU_CALLBACK,

#
severity: normalLine 87: There should be no trailing spaces

);

#
severity: normalLine 89: There should be no trailing spaces

$items[] = array(

#
severity: normalLine 90: There should be no trailing spaces

'path' => 'faq_ask/edit',

#
severity: normalLine 91: There should be no trailing spaces

'title' => t('Edit a question'),

#
severity: normalLine 92: There should be no trailing spaces

'callback' => 'faq_ask_edit',

#
severity: normalLine 93: There should be no trailing spaces

'access' => user_access('answer question'),

#
severity: normalLine 94: There should be no trailing spaces

'type' => MENU_CALLBACK,

#
severity: normalLine 95: There should be no trailing spaces

);

#
severity: normalLine 97: There should be no trailing spaces

$items[] = array(

#
severity: normalLine 98: There should be no trailing spaces

'path' => 'faq_ask/more',

#
severity: normalLine 99: There should be no trailing spaces

'title' => t('List more unanswered questions'),

#
severity: normalLine 100: There should be no trailing spaces

'callback' => 'faq_ask_list_more',

#
severity: normalLine 101: There should be no trailing spaces

'access' => user_access('answer question') || user_access('ask question'),

#
severity: normalLine 102: There should be no trailing spaces

'type' => MENU_CALLBACK,

#
severity: normalLine 103: There should be no trailing spaces

);

#
severity: normalLine 104: There should be no trailing spaces

}

#
severity: normalLine 106: There should be no trailing spaces

return $items;

#
severity: normalLine 107: There should be no trailing spaces

}

#
severity: normalLine 109: There should be no trailing spaces

/**

#
severity: normalLine 110: There should be no trailing spaces

* Get the ask question form.

#
severity: normalLine 111: There should be no trailing spaces

*/

#
severity: normalLine 112: There should be no trailing spaces

function faq_ask_page($tid = null) {

#
severity: normalLine 113: There should be no trailing spaces

$output = null;

#
severity: normalLine 114: There should be no trailing spaces

if ($tid) {

#
severity: normalLine 115: There should be no trailing spaces

$term = taxonomy_get_term($tid);

#
severity: normalLine 116: There should be no trailing spaces

$taxo_image = null;

#
severity: normalLine 117: There should be no trailing spaces

if (module_exists('taxonomy_image')) {

#
severity: normalLine 118: There should be no trailing spaces

$taxo_image = taxonomy_image_display($tid, 'align="left"');

#
severity: normalLine 119: There should be no trailing spaces

}

#
severity: normalLine 120: There should be no trailing spaces

$output .= '

'. $taxo_image . $term->description .'

';

#
severity: normalLine 121: There should be no trailing spaces

$output .= '

';

#
severity: normalLine 122: There should be no trailing spaces

}

#
severity: normalLine 123: There should be no trailing spaces

$output .= drupal_get_form('faq_ask_form', $tid, null);

#
severity: normalLine 124: There should be no trailing spaces

return $output;

#
severity: normalLine 125: There should be no trailing spaces

}

#
severity: normalLine 127: There should be no trailing spaces

/**

#
severity: normalLine 128: There should be no trailing spaces

* Get the edit question form.

#
severity: normalLine 129: There should be no trailing spaces

*/

#
severity: normalLine 130: There should be no trailing spaces

function faq_ask_edit($nid = null) {

#
severity: normalLine 131: There should be no trailing spaces

$form = drupal_get_form('faq_ask_form', null, $nid);

#
severity: normalLine 132: There should be no trailing spaces

return $form;

#
severity: normalLine 133: There should be no trailing spaces

}

#
severity: normalLine 135: There should be no trailing spaces

/**

#
severity: normalLine 136: There should be no trailing spaces

* Implementation of hook_form()

#
severity: normalLine 137: There should be no trailing spaces

* This is the "ask question" form.

#
severity: normalLine 138: There should be no trailing spaces

*/

#
severity: normalLine 139: There should be no trailing spaces

function faq_ask_form($tid, $nid, $form_values = null) {

#
severity: normalLine 140: There should be no trailing spaces

$form = array();

#
severity: normalLine 142: There should be no trailing spaces

// Find all the categories for which we have experts.

#
severity: normalLine 143: There should be no trailing spaces

$cat_list = array_flip(db_result_array(db_query('SELECT DISTINCT(name), e.tid FROM {faq_expert} e JOIN {term_data} t USING (tid)')));

#
severity: normalLine 144: There should be no trailing spaces

if (count($cat_list) == 0) {

#
severity: normalLine 145: There should be no trailing spaces

$msg = t('Currently, there are no categories defined. ') .' ';

#
severity: normalLine 146: There should be no trailing spaces

if (user_access('administer faq')) {

#
severity: normalLine 147: There should be no trailing spaces

nancydru’s picture

BTW:
coder.module

*
severity: normalLine 1: There should be no trailing spaces

<?php

*
severity: normalLine 2: There should be no trailing spaces

// $Id: coder.module,v 1.55.2.19.4.17 2008/01/30 14:08:56 snpower Exp $

*
severity: normalLine 4: There should be no trailing spaces

/**

*
severity: normalLine 5: There should be no trailing spaces

* @file

douggreen’s picture

I'm not getting any of those errors. @nancyw, are you on Windows? If so, it's possible that the download of the file turns \n into \r\n and these are causing problems. I no longer have a Drupal install setup on Windows so I can't test this.

nancydru’s picture

Yes, I'm using Windows. As for the download, I have no idea. How would I check that? And, BTW, Faq_Ask was not downloaded; it was born on my PC. However, Coder itself was downloaded, obviously.

Drupal 5.7
Configuration file Protected
Cron maintenance tasks Last run 3 sec ago
Database schema Up to date
Drupal core update status No update data available
File system Writable (public download method)
GD library bundled (2.0.28 compatible)
MySQL database 5.0.27
PHP 5.2.0
PHP register globals Disabled
Unicode library PHP Mbstring Extension
Web server Apache/2.2.3 (Win32) mod_ssl/2.2.3 OpenSSL/0.9.8d PHP/5.2.0 mod_perl/2.0.3-dev Perl/v5.8.8

Just to be sure I dropped back to 5.6 and had the same problem.

stella’s picture

We could try removing the \n and \r\n from the line as we're reviewing it in the module itself. Perhaps something like this would do it:

$line = rtrim($line, "\r\n");

Cheers,
Stella

nancydru’s picture

Where would I put that?

douggreen’s picture

I changed the rule to check for trailing spaces and tabs ONLY, and not newlines and carriage returns. Please check out the latest copy and let me know if that fixes anything.

nancydru’s picture

I've not had the best luck with checkouts, but I will try.

nancydru’s picture

Status: Needs work » Fixed

That took care of the blanks. Thanks.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.