If you are using NetBeans IDE for Drupal contributed module development - my absolutely favourite IDE for many years, and super for PHP and Drupal work - then you SHOULD take the time and trouble to install a plugin that gives you feedback on departures from Drupal coding standards, this can save you days of fiddly work.
I provide here a description of "what currently works for me" as of 2015-04-02. I am using here the PEAR package manager/installer and Drush; Please note, I am not using 'composer' for installation here at all (however using composer is described in some of the links).
Assuming you already have PEAR installed (and up-to-date), install PEAR:PHP_CodeSniffer. You have have to do this as sudo in bash:
$ sudo pear install PHP_CodeSniffer
I currently have PHP_CodeSniffer-2.3.0
. You should then find you have this executable:
$ which phpcs /usr/local/bin/phpcs
Hence you will often hear PHP_CodeSniffer
simply referred to as phpcs
.
Next, you need to install Coder, which is not a typical Drupal module, however it can be downloaded and installed using Drush. At the time of writing, Drush may offer you only the 7.x series of Coder:
$ drush dl coder There are no stable releases for project coder. [warning] Choose one of the available releases for coder: [0] : Cancel [1] : 7.x-2.x-dev - 2015-Mar-08 - Development [2] : 7.x-2.4 - 2015-Jan-14 - Supported [3] : 7.x-1.x-dev - 2013-Sep-30 - Development [4] : 7.x-1.2 - 2012-Nov-11 - Supported
PHP_CodeSniffer
2.x series, if you really want to use one of them you should instead force an older version of PHP_CodeSniffer
on install !
I will assume you are going to instead install a compatible 8.x version of Coder, which you can choose from the Coder project page. You can install Coder into your local ~/.drush
using this (on Mac/Linux/UNIX):
drush pm-download coder-8.x-2.1 --destination=$HOME/.drush
You should then find you have a Drupal standard definition folder for Code Sniffer:
$ ls -1 ~/.drush/coder/coder_sniffer/Drupal Docs Sniffs Test drupalcs.info ruleset.xml
This is already available to drush drupalcs
, but it is not yet available to your IDE plugin, so before we proceed with the NetBeans plugin, we link it to the PHP Code Sniffer standards folder thus:
cd /usr/local/PEAR/PHP/CodeSniffer/Standards/ ln -s ~/.drush/coder/coder_sniffer/Drupal .
[Check your PEAR install location, it might be slightly different.]
[BTW I have an offline record of the other plugins that don't seem to work with NB8, but to save you any possible mistakes or problems, I am not going to reference them or link to them here.]
Please visit this plugin page for PHPCSMD NetBeans Plugin by vralfy, and please take note of that author, and check again when you come to the plugin install stage that you have the correct one, and note that:
'This plugin is also available on the NetBeans Plugin Portal Update Center. Use 'Tools > Plugins' action from the NetBeans IDE main menu for convenient installation of this plugin'
This is the full description of the Plugin as it appears in the NetBeans8 Plugin tool:
Plugin Description
phpcsmd
This plugin displays the output of phpcs, phpmd, phpcpd or pdepend in your Netbeans IDE as Reports or Annotations in your PHP source code.
All you need is a functional version of phpcs, phpmd, phpcpd or pdepend (depending on what you want to use).
Usage:
- Open Tools->Options->PHP->PHPCSMD
to configure the plugin
- Open Tasks by pressing Ctrl + 6
- You can scan with a right click on the file or source code and choose "Check for vialoations" or "Scan with Pdepend"
That's all!
Tips:
- enable "try threading analysis" to prevent Netbeans from freezing during scan
- enable "update on save" to rescan the file each time you save
- enter "\.(svn|git)" (or any other regex) to prevent this file from beeing scanned
- if the "Task" tab is open (Ctrl + 6) PHPCSMD will scan the file after Netbeans opened it
- first configure the path to phpcs, click "show standards" and choose your favorite standardyou can also enter a path to standard in the "--standard" option if your favorite standard is not installed in pear
- enable PHPCS->Extras and PHPCS->Extras->Unreachable Code
to see dead code
- enable "Use tabs to organize PdependReport" and "Create JDepend statistics" to get a better view on the metrics
- Deactivate "Create JDepend statics" if you don't care about package abstraction/instability to increase the pdepend performance
Module installed: phpcsmd
Now the Plugin is installed, on restart of NetBeans IDE, you should have one (and one only) extra tab PHPCSMD under (on Mac):
NetBeans > Preferences > PHP > PHPCSMD
(If you have more than one new tab there, you have inadvertently installed one of the other similar plugins, please uninstall it and make sure you have only this recommended one.)
Activate PHPCS (only, for this guide for use of a passive code sniffer only, do not activate PHPMD or anything below that line).
Click on show standards (you MUST do this to be offered Standards selections) and select Drupal
, which you linked to previously above.
You can also play with the General Settings. This option is nice but can sometimes make file opening slow:
[x] check file on open
I also like these (but they are not essential):
[x] show notification icon [x] show annotations in error stripe
Once you have chosen your settings and clicked OK, you then have to adapt an existing (or create a new) Action Items filter set so that it uses the PHPCS code sniffer. I like to create one separate from the usual TODOs list. To do this, open up Window > Action Items
, then click on the little "coffee filter/funnel" symbol on the left of the Action Itemswindow (and note, in NB8 this is completely separate from the Tasks window, which is now primarily for synching with issue tracking systems). Choosing action item filter options is common sense, see the image.
And now you're done; just load a PHP file and choose the PHPCS Action Items filter mode. To refresh the Code Sniffer reports, just choose the right context menu item Check for violations
!
And remember again, make sure you have read TIP: NetBeans: removing trailing whitespace (to meet the Drupal Coding Standards and pass Coder/PHP_CodeSniffer) !