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 :

php go-pear.phar
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.

pear install PHP_CodeSniffer
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.

Share This Article

  • ralan

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

  • http://meadsteve.github.io/ MeadSteve

    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.

  • http://www.techwila.com/ Divy Singh Rathore

    Nice tips. Its very helpful