Code Sniffer

PHP Code Sniffer breaks apart in tokens PHP, JavaScript and CSS files and detect violation of coding standards.

Installing Code Sniffer

Let’s start with Installation step and then Netbeans configuration

On Linux

(tested only on Ubuntu)

sudo pear install --alldeps php_codesniffer

On Mac (with brew)

brew install php-code-sniffer

You can install brew by going to http://brew.sh/

On  Windows

TODO

Installing WordPress Coding Standard

Check available coding standards

Run following command to check available coding standards by default.

phpcs -i

You will get something like:

The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend

You can also find them in filesystem:

cd $(pear config-get php_dir)/PHP/CodeSniffer/Standards/

Install WordPress Coding Standard

We are following these WordPress coding standard – https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards

You can install it using:

git clone git://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git $(pear config-get php_dir)/PHP/CodeSniffer/Standards/WordPress

CodeSniffer and NetBeans

Configuring CodeSniffer in NetBeans

Note: Instructions given below are for NetBeans 7.4+. If you are using older version, please upgrade NetBeans.

Go to Netbeans Tools > Options > PHP

Options

If all goes well you will see something like below in your netbeans output window:

NetBeans-phpCS window

Missing “Code Analysis” option tab

We are using Netbeans 7.4 beta which has “Code Analysis” built in.

For old Net Beans version you can install it from Tools > Plugins .

Just go to “Available Plugins” tab and search for “phpcs”. You will get 2 options. Try both! Or better upgrade to Netbeans 7.4.

Troubleshooting Mac with Brew

This should have been cakewalk. But if you are on mac and using brew it might get tough.

In any case if NetBeans refuse to show you CodeSniffer standards in drop-down menu, please run following:

pear config-show | grep bin_dir

It outputs

PEAR executables directory     bin_dir          /usr/local/Cellar/php54/5.4.19/bin

Just append phpcs to it so it becomes /usr/local/Cellar/php54/5.4.19/bin/phpcs

Also, you need to use php binary path by brew:

Options-1

Running CodeSniffer in NetBeans

Select a file or a folder. Then click Source >> Inspect Menu.

netbeans-run-codensiffer-1

Then select Code Sniffer configuration from drop-down and hit “Inspect” button.

Condesniffer run in netbeans

CodeSniffer and GitLab-CI

rtCamp uses GitLab and GitLab-CI to manage its product line.

Installation CodeSniffer on Runner

Installation process will be similar to installing on Linux (assuming you are using Linux to run GItLab-CI Runner).

If you are rtCamper using ci.rtcamp.com, then you can ignore this part as all runners have CodeSniffer already installed.

Configuring GitLab-CI Project

Go to GitLab-CI Project Settings and paste following line in “Scripts” section.

phpcs "--standard=WordPress" "--report=full" "--extensions=inc,php,phpt,php4,php5,php3,phtml" "--encoding=UTF-8"

Screenshot:

GitLab CI