Web Development

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.

[gads]

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.

[gads]

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.

[gads]

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 :
[cc]
php go-pear.phar
[/cc]

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.

[gads]

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.

[cc]
pear install PHP_CodeSniffer
[/cc]

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.

Shares:
  • ralan
    ralan
    July 29, 2014 at 3:30 am

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

    Reply
  • MeadSteve
    August 7, 2014 at 4:10 pm

    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.

    Reply
  • Divy Singh Rathore
    Divy Singh Rathore
    August 16, 2014 at 4:19 pm

    Nice tips. Its very helpful

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *