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…”.
- Setup PHP Codesniffer in Eclipse
- URL to use for “Install new Software” in Eclipse: http://www.phpsrc.org/eclipse/pti/
- Restart Eclipse
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.
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:
- Click Window > Preferences
- General > Editors > Text Editors
- Set Displaid tab width to 4
- Tick Insert spaces for tabs
- Tick Show Print Margin
- Set Print Margin Column to 120
- General > Perspectives
- Select PHP and click Make Default
- General > Workspace
- Set Text File Encoding to UTF-8
- Set New Text File Delimiter to Unix
- PHP > Code Style > Formatter
- Set Tab Policy to Spaces
- Set Indentation size to 4
- PHP > Editor > Save Actions
- Tick Remove trailing whitespace
- Check All Lines
If you have any comments, please feel free to share them below.