Feedback only, no action required, please close on reading.

The Drupal coding standard regarding whitespace requires:

Lines should have no trailing whitespace at the end.

Even if one has NetBeans IDE configured to use these excellent tips on NetBeans formatting settings for Drupal - which go a long way to help one meet the Coder rules - NetBeans often introduces whitespace automatically, which trips up Coder. It can be a real time waster and annoyance.

It happens for example when one hits ENTER/RETURN at the end of a line of code like this:

  $a = $b + 1;
  
  // NetBeans will put in 2 whitespaces above, they have to be removed by hand

Comments

klausi’s picture

Yep, I'm using Netbeans myself and this is annoying.

Thanks for reporting, I guess we cannot do anything at Coder's end. Do you know if a bug report against Netbeans already exists regarding this?

klausi’s picture

Tip: if you use PHPCS 2.x and Coder 8.x-2.x you can fix such whitespace issues automatically with phpcbf.

webel’s picture

Hi @klausi, thanks for the tip.

For now I am doing a lot by hand in NetBeans, because I have a special coding recipe that is amenable o reverse engineering for PEAR PHP_UML and MagicDrawUML, so I am avoiding any other code manipulation tools.

The problem I am reporting here is really a NetBeans IDE formatting problem, not a Coder problem.

Here is another example, if I hit return after the end of a Class declaration (after the {) NetBeans inserts 2 spaces:

class DemoCommon extends RenderHelper {
__

There are actually 2 spaces there above that I have to remove by hand.

[EDIT: to show it on Drupal.org (which takes away my whitespace as pasted) I have now inserted 2 underscores]

webel’s picture

Another example using the NetBeans configuration as per the excellent link above.

NetBeans8.0 makes the line longer than 80, although the config seems to have 80 lines as the line length setting:

  const MARKUP_TIP_HOVER_LINKS = 'For more information on each demo please hover the mouse over each link:';

I have to train it to do some things differently to pass some Coder rules automatically.

Eventually, I want to be able to pass all Coder rules directly from NetBeans, without any further manipulation.

webel’s picture

@klausi commented a day ago:

Tip: if you use PHPCS 2.x and Coder 8.x-2.x you can fix such whitespace issues automatically with phpcbf.

May I ask 2 questions.

Q1: My current module project is for Drupal7, so I am using Coder 7.x-2.x. Are you saying that the automatic whitespace fix only works with Coder 8.x-2.x ?

From http://www.squizlabs.com/php-codesniffer:

PHP_CodeSniffer now comes with a second script; phpcbf, the PHP Code Beautifier and Fixer. This script piggy-backs off PHP_CodeSniffer to provide fixes for a lot of common errors. It will never fix 100% of the errors PHP_CodeSniffer finds as many require a developer to make a decision (e.g., can you use === here?) or may cause the code to execute differently if changed (e.g., uppercasing a constant name). But there are still a lot of errors that can be corrected without causing issues. Out of the 566 unique error messages that PHP_CodeSniffer can currently produce, version 2.0.0a1 is able to correct 202 of them automatically, which is about 35%. When you run PHP_CodeSniffer and get a report of the errors found, you will now be able to see which of those errors can be automatically corrected.

Along with this new script, PHP_CodeSniffer adds a new report type; the diff report. If you don't like the idea of PHP_CodeSniffer fixing errors automatically, you can instead ask it to output a diff of the fixes that it would make using the command line argument --report=diff. If you like what you see, you can simply change the phpcs command to phpcbf, leave all the command line options the same, and let PHP_CodeSniffer patch your files.

All this new functionality is documented on the wiki, and includes sample output, so please take a read: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automati...

Q2: It seems from the above I can only choose to accept all suggested changes from a Coder run, or none. Would it be possible to only accept whitespace changes ?

The reason is that some of the changes Coder is suggesting break my own coding rules for PHP that is amenable to reverse engineering to graphical UML with PEAR: PHP_UML, and if I use a tool to perform automated changes, it could break the synchronisation between my PHP code and my reverse engineered UML models.

Editing whitespace is harmless for that synchronisation, but such issues as the following are not: OO: Coder complains about camel caps (camelCase) argument to setter method in Class. I do not only strongly, and with good reason, completely reject that current rule, if I run it (along with say the whitespace change) it will cause me a heck of a lot of work changing my UML model to match, by hand, and reexporting and reuploading these UML diagrams.

webel’s picture

From Using coder.module to auto-format your code with drush:

drush coder-format sites/all/modules/custom/{$modulename}

It is not clear to me whether one can choose to apply only specific rules, such as whitespace cleanup.

klausi’s picture

Yes, the fixer part of PHPCS only works on Coder 8.x-2.x, since only that branch is compatible with PHPCS 2.x.

We don't have automatic fixer rules for all errors that Coder reports, only for the obvious ones (not even completely for those, patches welcome). You can pass config options to phpcbf to only run certain fixers and not all of them, see https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automati...

webel’s picture

Status: Active » Fixed

Solution for NetBeans trailing whitespace

This works for removing trailing whitespace in NetBeans 8.0 (Mac version):

NetBeans > Preferences > Editor > On Save > Remove Trailing Whitespace From: All Lines 

Make sure you have:

Language = PHP

And you may need to deselect:

[ ] Use All Languages settings.

It was not at all obvious to me that this might be under On Save.

You have to at least perform a dummy edit then save the file for it to work.

NetBeans still seems to introduce the whitespaces on an empty next line
or empty documentation line sometimes on [ENTER]/[RETURN], but at
least it gets removed on save.

See also: http://stackoverflow.com/questions/4119872/netbeans-removing-trailing-wh...

Status: Fixed » Closed (fixed)

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