Tags

PHP_CodeSniffer by Squizlabs is a great way to enforce certain coding standards. One of these standards is PSR-2. It enforces rules like 4 spaces for indenting and starting the opening brace for a class on its own line.

The Netbeans IDE is a text editor specifically designed to edit code. One of its features is a code formatting tool that will automatically correct deviations from preset format settings. Ideally you should be able to inform Netbeans that PSR-2 settings should apply. This not being the case you should be able to set the configuration as close as possible to the PSR-2 standard. In that way the Netbeans code formatting tool and PHP_Codesniffer will never clash.

Is it turns out Netbeans is unable to handle one specific PSR-2 rule: there should not be a space between the end of the last function and the end of a class. As a workround the PHP_CodeSniffer application can be extended. In a custom ruleset both PSR-1 and PSR-2 are adopted but for PSR-2 the PSR2.Classes.ClassDeclaration is excluded and replaced by a custom Netbeans.Classes.ClassDeclaration.

In this class the section handling the no-space-before-class-end rule is simply outcommented. In the meanwhile Netbeans should be aware of the problem through this bugreport: https://netbeans.org/bugzilla/show_bug.cgi?id=226806. The codesniffer extension is available at https://github.com/hrvanderlingen/netbeans-codesniffer.
PSR2

Usage

Linux Install:
* pear install PHP_CodeSniffer-2.2.0
* Test if PSR1 and PSR2 are in the standards with phpcs -i
* phpcs –standard=full_path_to_netbeans_standard your_directory_name

* Codesniffer lives in C:\xampp\php\pear\PHP\CodeSniffer. The custom standard must be placed here. You can then use phpcs –standard=Netbeans your_directory_name

If in Xampp PSR1 and PSR2 standards are not included, best replace the entire C:\xampp\php\pear\PHP\CodeSniffer with the folder of the same name from the Linux install.

Advertisements