Install PHP CodeSniffer on Windows Machine

In Programming world there are many tools available which can help us to improve productivity. This productivity tools can be anything which can help to reduce development time, help to improve code quality, help to track bugs easily, help to build products easily, etc.

In this article we will focus on improving Code Quality. Very first step towards improving code quality is to maintain coding standards across developers. I understand that its is really tough job to maintain the same as every developer have their own way to code stuff. e.g. some prefer all lower case for method name, some prefer camelcase for that, etc.

PHP CodeSniffer

Very first step towards improving code quality is to maintain coding standards across developers.

Here we will talk about PHP CodeSniffer, which help us to maintain coding standard across multiple developer. Dealing with CodeSniffer is much easier: create rule set, validate your file against your rule set and get the result immediately. It will immediately show how many mistakes you have made in terms of following coding standards and eventually all developer will start coding as per coding standards you have defined.

To begin with we will start our article series with Installation of PHP CodeSniffer on Windows and Linux Server. And later we will see how we can check/validate files against rules, how we can create our own rules, etc. Hope you find this article series useful.

Note: In this article we assume that PHP is already installed in your system. So we will skip that step here.

Install PHP CodeSniffer on Windows

Before we start with PHP CodeSniffer, we need to make sure PHP PEAR is installed in your system. Let’s start installing the same now.

Install PEAR on Windows

1) Download PEAR PHAR file first from : http://pear.php.net/go-pear.phar
2) Move this file to directory where PHP is installed. In my case it is C:/PHP
3) Open a command prompt with administrative access/permission (All programs –> accessories and right-click command prompt and select “run as administrator”)
4) Now go to your PHP installation directory: cd C:\PHP
5) Its time to install PEAR now, run following command to install PEAR :

1
php go-pear.phar

Install PEAR on Windoes

6) Next you will have to select system for install type (system is by default selected, so you can hit enter to continue.)
7) Now next step will ask you for PHP which PEAR is going to use. If you want to edit that type 11 press enter and then type c:\PHP\pear.ini
8) On this last step you need to register some environment variable in your system, which can be done by running c:\php\PEAR_ENV.reg file.

Install PHP CodeSniffer on Windows

We have PHP PEAR is installed and now its time to install PHP CodeSniffer which is one of the Package of PEAR.

1) Open command prompt with administrative access.
2) Move to your PHP installation directory which is C:/PHP in my case.
3) Start installation of PHP CodeSniffer by running following command.

1
pear install PHP_CodeSniffer

Install PHP CodeSniffer

Once installation is done, you can execute phpcs -i in command prompt to see version of installed PHP CodeSniffer. You should get output as per below image if everything is done correct.

PHP CodeSniffer Installed

So far what we have done is installation of PEAR and PHP Code Sniffer on windows machine. Later we will see how to install same stuffs on Linux machine.

About the Author: Avinash

Experienced full stack freelance web developer with a track record of producing excellent results for 11+ years. Excellent communication and collaboration skills, managing with a team of 20+ people. I’m motivated to make a positive impact on your revenue through the customer web portal development project.

3 Comments

  1. ralan July 29, 2014 at 3:30 am - Reply

    Please don’t use pear anymore. Use and enjoy composer.

  2. MeadSteve August 7, 2014 at 4:10 pm - Reply

    This is what I did to setup codesniffer on a windows machine I use at work using composer:

    0. Setup composer (guides can be found on doing this.)

    1. Create a folder for codesniffer and add a file called composer.json to the folder with the contents:

    {
    “require-dev”: {
    “squizlabs/php_codesniffer”: “1.*”
    }
    }

    2. Run “composer install”. This will download all the required parts. After the command has completed there will be a vendor/bin folder containing a phpcs.bat

    That’s it. Whenever I want to update codesniffer all I need to do is running “composer update”. Sorted.

  3. Divy Singh Rathore August 16, 2014 at 4:19 pm - Reply

    Nice tips. Its very helpful

Leave A Comment

FREE QUOTE

Do you need help with services? Request a free quote to get in touch.