Installing PHP Codesniffer Properly In Eclipse

PHP Codesniffer Integrated into Eclipse

The Eclipse PHP Codesniffer tool for Eclipse allows you to detect coding standards errors as you are actually typing your code. Unfortunately, not everyone realises this and sets it up simply to output the errors to the console. Here, I will show you how to setup PHP Codesniffer to work natively in Eclipse.

You will need a copy of PHP installed and running on your machine to make this work.

1) Install PHP Codesniffer in Eclipse

Follow these instructions to install PHP Codesniffer in Eclipse.

Zend Studio Users: Please note that if you are using the Zend Studio version of Eclipse, you will need to go to “Help > Software Updates > Search for new feature to install > Find & Install…” then you will need to add a new source by clicking on “Add Remote Site…”.

However, it probably won’t work straight away, because you need to set-up your PHP executables in Eclipse.

2) Setup PHP Executables in Eclipse

Navigate to “Window > Preferences > PHP > PHP Executables” and if there is nothing in the list, then add a new one that points to your PHP executable.

3) PHP Codesniffer Settings

Now all we need to do is setup PHP Codesniffer in the Eclipse preferences settings.

Goto “Window > Preferences > PHP Tools > Codesniffer”.

  • PHP Executable:
    Select the one that you wish to use. You might have created one in the step above?
  • Codesniffer Standards:
    Select the one you prefer, I use Zend.
  • Standard Tab Width:
    Please ensure this is correct to your coding standard, I set it to 4.
  • File Extensions:
    This is a comma separated list of file extensions that PHPCS will try and validate. I only have php in my list. You may have some “.inc” files. Don’t allow it to validate Zend View “.phtml” files. If you allow it to validate .phtml files, it will show that all of your Zend View files are invalid as they contain HTML.

It will ask you if you want to do a “Full Rebuild” – you need to answer yes to this. Depending on the size of your codebase it could take a while to do this, so please be patient.

All done!

Once your rebuild is complete, Eclipse will now show you the files that have coding standards errors with a little red cross next to them. Furthermore, when you open the file, it will show you where the errors are down the left-hand side next to the line number.

Tip: Eclipse Settings

I use Zend Framework coding standards, mainly because I use it every day. I find it eaiser if my development environment is setup correctly. These are my Eclipse settings, feel free to use them:

  1. Click Window > Preferences
  2. General > Editors > Text Editors
    1. Set Displaid tab width to 4
    2. Tick Insert spaces for tabs
    3. Tick Show Print Margin
    4. Set Print Margin Column to 120
  3. General > Perspectives
    1. Select PHP and click Make Default
  4. General > Workspace
    1. Set Text File Encoding to UTF-8
    2. Set New Text File Delimiter to Unix
  5. PHP > Code Style > Formatter
    1. Set Tab Policy to Spaces
    2. Set Indentation size to 4
  6. PHP > Editor > Save Actions
    1. Tick Remove trailing whitespace
    2. Check All Lines

If you have any comments, please feel free to share them below.

Permalink: http://www.websitefactors.co.uk/other/2011/10/installing-php-codesniffer-properly-in-eclipse/

This entry was posted in PHP. Bookmark the permalink.

5 Responses to Installing PHP Codesniffer Properly In Eclipse

  1. Hi,

    I have just installed PHPCS in Eclipse (Yoxos distro with PHP extension), and if it take a huge amount of time on huge project, it run fine and help a lot.
    Thanks!

    Cédric

    • Henry Hayes says:

      Yeah, the extension can take some time to complete for the first time. Though I believe that after this phpcs is only run on file save or periodically during file auto-save – so it’s very fast (unless you have a massive file or a very slow machine).

  2. Peter eman abastillas says:

    funny I kept getting this:
    Cannot complete the install because one or more required items could not be found. Software being installed: PHP Tool Integration Core

  3. Henry Hayes says:

    @Peter eman abastillas

    A colleague of mine had a similar problem to this today. They installed eclipse using the standard distribution, not the PDT version. They then tried to install the PHP PTI tools. They recieved the same message you specify above.

    The reason was that they had not installed the PHP development tools in their Eclipse.

    Explanation:

    Eclipse comes in many flavours. For the purposes of this explanation, there is Standard Eclipse and Eclipse PDT. My colleague installed the Standard Eclipse. If you do this and would like to use any PHP tools, you need to install PHP Development Tools on-top of your Eclipse installation.

    Once you have done this, you can install the PTI tools as demonstrated above.

    Note: The Eclipse PDT package comes with PHP Development Tools included and you do not need to do this, PHP PTI tools should install correctly.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Notify me of followup comments via e-mail. You can also subscribe without commenting.