Integrating PHP CodeSniffer with Activestate Komodo Edit
February 15, 2010 in Web Development
I’ve recently been looking into standards of code and how they would apply to myself and my code. To enable this, I’ve recently installed PHP_CodeSniffer and for a few days been using this on the command line.. which by itself isn’t bad, though I really wanted to have something inside my editor of choice (happens to be Komodo Edit).
Setting up Pear
Since I will be using Pear to install the code sniffer we might as well cover that to start with.
There are lots of tutorials out there with the aim of helping you to get pear installed no matter what the platform. I Will leave that job for them. What I will do is cover how to get it running easily with WampServer from http://www.wampserver.com/en/ Please do Note that this is all on a Windows Based machine
- Open a command prompt
- Make a note of your php Directory… you will need this in a few places
“WAMPDIR\bin\php\PHPVERSION”
where WAMPDIR = the directory WampServer is installed to
and PHPVERSION = php+version number..
i’m using php 5.2.11 on wampserver so the location for me looks like this
“D:\Wamp\bin\php\php5.2.11″ - CD /D PHPDIR – where PHPDIR is the location you should have noted down in the last step
- Type go-pear and watch as it installs
- bring up the Advanced system properties
- Environment Variables
- Edit Path to add your php folder prefixed with a ;
e.g. for my path was already c:\SharedLibs so that became
c:\SharedLibs;d:\wamp\bin\php\php5.2.11 – Close and save all settings
Installing PHP_CodeSniffer via Pear
Now that I had pear installed I could use it to install PHP_CodeSniffer, this was a simple task with 1 line of code..
pear install PHP_Codesniffer and that was it.. Actually I tell a small lie here, to make it work within the confines of other folders was to do as follows:
- Open the phpcs.bat file that resides in the php folder outlined above in notepad or similar editor
- Change “./php.exe” to “php.exe”, save the file and close
Now we are ready to run PHP_Codesniffer.
Setting up Komodo Edit
To start with, I use Komodo Edit for this, that is because that is what I use. I thoroughly recommend it over the likes of notepad++ etc mainly because of its feature set, and the way that it handles code completion from the project. But enough of all this, lets get down to creating a “run Command” for the active Tab/File in Komodo.
- Click Tools, then Run Commandin the Run box enter the following:
phpcs -n --standard=%(ask:Which Standard do you wish to use?:ZEND) --report=emacs "%F"Check the Add to Toolbox optionToggle the More options So you can see the Advanced Options Panemake sure the “Run in” is set to Command Output TabCheck the “Parse Output with:” checkbox and enter the following in the select field:^(?P<file>.*):(?P<line>\d+):(?P<column>\d+):(?P<content>.*)- Check the “Show parsed output as a list” checkbox
- Click the New button to begin adding a envrionment variable
- Set the “Variable Name” to PATH and the “Variable Value” to the location of PHP_Codesniffer – If you are following through this is the same as step 2 of “setting up pear”- then click ok
- click Run button – PHP_CodeSniffer will run and then will ask you for the standard you wish to check against .. by default It’s set to ZEND
- Lastly right click the toolbox entry and bring up the properties. The Top left Text entry is the “nice name” for this. You can also assign keys to launch this command later
Now you have successfully created a run Tool in Komodo that will run PHP_Codesniffer against the active file and will output the results in a nice list in the command output tab at the bottom panel. I chose the emacs output method as it was easier to get nice looking format without the \” escape sequence appearing in the “content”. The Reason for the regex and the list is that it will now allow you to double click and entry and it will jump to the right spot in the file.
I’ve also included a Komodo Package of 3 run time commands .. 1 for file, 1 for all files in the project and 1 for all files the directory shared with the currently active file. You can download them here: CodeSniff Package – Recommended to Right-Click – Save Target as etc – This packages comes with predefined keystrokes
- CTRL+L,CTRL+S,CTRL+F to Codesniff the currently Active file
- CTRL+L,CTRL+S,CTRL+P to Codesniff all files in the Project
- CTRL+L,CTRL+S,CTRL+D to Codesniff all files in the Active File’s Directory
I do hope that this will prove as useful for someone else as it has for me, comments are welcome.
Hi,
Just trying to get this working on Ubuntu and got an issue with phpcs.
When I try to phpcs any .module file (drupal) from command line it doesn’t output anything, but if I append .php to the file, i.e, ‘ phpcs drupal.module.php ‘ then it does.
This goes for the other files as well (.install etc), basically it is not parsing non .php files, Do you know why this could be ?
Hi – thanks very much for posting this – it’s been very helpful to me getting the basics working – I’ve now got PHP CodeSniffer triggering as a macro on ‘beforesave’ in Komodo.
Unfortunately, I was unable to download the CodeSniff Package – it looks like the link in your post is broken.
I’m struggling with what should be really straightforward – how do I access the output of the PHP CodeSniffer command in the JS macro in order to check whether there WAS any output? Based on this, I want to allow or deny saving the file. I’ve scoured the web for documentation on this but am not having much luck.
BTW, the formatting of your post is a bit awry – some of what should be line breaks are missing e.g. in step 2 under ‘Setting up Komodo Edit’
Thanks in advance for your help
Jowan, Nigel sorry I stopped using Komodo quite a while ago and can’t seem to find the file. If I can I’ll upload and point to it. Sorry I can’t be much more help.