How to setup PHP CodeSniffer on Windows machine ?

How to use CodeSniffer on Windows ? How to setup the PHP CodeSniffer on Windows ? These questions come to mind when you see various articles, tutorials which provides ways/steps to setup this on Linux. So here are simple steps to setup PHP CodeSniffer on Windows. Before going into this, for those who don’t know what is CodeSniffer? here is a simple answer.

If you are a developer or code reviewer and you want to check the code for any mistakes/if the code following particular coding standards, then CodeSniffer is a good tool to achieve that.

PHP_CodeSniffer is a PHP5 script that tokenises PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

Now, lets setup CodeSniffer to check if our code follows a defined coding standard. ๐Ÿ™‚

My setup environment:

  • Windows 7 Professional,
  • XAMPP for Windows( Apache, MySQL, PHP and Perl ) and the PHP install directory is C:xamppphp
Steps:
  • Open windows command prompt by going.
    Start -> Run -> type in ‘ cmd ‘ -> press Enter
  • Go to your php install directory.
    cd C:xamppphp
  • Then type in the command to install PHP Code_Sniffer from the pear.php.net

    pear install PHP_CodeSniffer
    pear install --alldeps PHP_CodeSniffer
  • subharanjan_codesniffer_installation

  • After completion, to check successfull installation run this command.
    phpcs -i if everything goes well, then it should show list of coding standard definitions found.

As the CodeSniffer is installed now, you can start using this to check files by using below commands. ( To run any commands, you should be inside C:xamppphp )

Checking a single file or folder:
$ phpcs /path/to/code/myfile.inc
$ phpcs /path/to/code/my_dir

Sample PHP_CodeSniffer output with no warnings:
$ phpcs -n /path/to/code/myfile.php

FILE: /path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR | Missing file doc comment
 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
 51 | ERROR | Missing function doc comment
 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------

Specifying a coding standard to use:
$ phpcs --standard=PEAR /path/to/code/myfile.inc
For more usage commands: http://pear.php.net/manual/en/package.php.php-codesniffer.usage.php

How to add a new coding standard definition into PHP CodeSniffer ?

Lets take WordPress coding standard for demo purpose found here: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards

Steps:
– If you have git installed on you machine, then run this command on git bash.
$ git clone git://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git C:/xampp/php/pear/PHP/CodeSniffer/Standards/WordPresssubharanjan_codesniffer_git_bash
OR

If you don’t have git on your machine, then

subharanjan_codesniffer_directory_structure
Now you can check any file for WordPress coding standard.
phpcs -n --standard=WordPress /path/to/code/cron.php


Posted

in

, ,

by

Comments

12 responses to “How to setup PHP CodeSniffer on Windows machine ?”

  1. […] coding standard definition, then setup that standard before hand on CodeSniffer. How to do that ? Follow my previous post to know how to install CodeSniffer and various Coding Standard definitions. Go to the CodeSniffer setting tab on NetBeans and set the Coding Standard definition name there in […]

  2. Muthamizhchelvan Veeramuthu Avatar

    I got installed PHPCS on windows, I just comfirm with pear list comment ut when I try to use this comment on my windows comment prompt I am getting error like ‘phpcs’ is not recognized as an internal or external command

    1. dwarkesh Avatar
      dwarkesh

      same error occurs…how to solve this?

  3. Vikram Avatar

    I have my XAMPP installed in D: directory. It gives me error

    ERROR: failed to mkdir C:\php\pear\tests\PHP_CodeSniffer\CodeSniffer\Core\File

    1. Kazimierz Matan Avatar

      I had the same error. But it works.
      Install script probably uses hardcoded path.

  4. Justinas Avatar
    Justinas

    Perfect article. I used it for XAMPP and PHPStorm.
    Just had to specify full path to pear: c:\xampp\php\pear install […].
    And it auto shows up in PHPStorm at code inspection to chose from.

  5. Mohammed Avatar

    Thank you very much.

  6. peterforest Avatar
    peterforest

    i can not understand “phpcs -n –standard=WordPress /path/to/code/cron.php” this command how it will be used?

  7. Manoj Kumar Swain Avatar
    Manoj Kumar Swain

    How to add CodeIgniter plugin, so that we can run the codeigniter projects and get error removed.

    1. Subharanjan Avatar

      Hi Manoj, Thanks for stopping by. PHP CodeSnifer doesn’t automatically fix the coding standard issues. For that you may need to check this: https://github.com/FriendsOfPHP/PHP-CS-Fixer Regarding the CodeIgniter coding standards please fetch and setup this: https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer#installation Hope this helps ๐Ÿ™‚

  8. Shankar Avatar
    Shankar

    I have my application hosted on IIS. Install was successful but while running “phpcs -i” getting this error. \iis was unexpected at this time.

  9. Joseph Avatar
    Joseph

    How to install instructions:
    ***on Windows 10 you need to run cmd as administrator!***
    Open windows command prompt by going.
    Start -> Run -> type in โ€˜ cmd โ€˜ -> right-click and select “run as administrator”

Leave a Reply

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