On Habré, the description of the PHP_CodeSniffer tool, which is used to check the compliance of the written code with the coding standards, has already been met. The tool is very convenient and useful, I think, no one will argue. There are articles on the Internet that describe how to integrate it with the version control system to check before the next commit, but I wanted to tie it to my IDE to check the code while it was being written.
Historically, I use Nusphere IDE - PhpEd in my work. PhpEd allows you to connect to your php scripts, which can perform arbitrary operations on the code in the editor. The script can also launch an external program and return data from the standard output stream directly to the code editor.
The Nusphere forum has a whole section on
automation scripts.I have long known about this feature, but it is not quite suitable for using CodeSniffer. I will explain why. We can redirect all the data obtained as a result of the analysis of our code to the debug window of the editor, but this will be just a static text. Line numbers with errors are also plain text and it will not be possible to navigate through the code. You’ll have to watch with your eyes the line number with an error, move your hands to this line and edit it. Of course, I didn’t like this case at all.
')
In the editor's log window, I noticed the location column. Most likely this is what I needed, but I did not know how to display the data in this column.
Breaking the entire developer forum, I found several topics dedicated to this problem, but everywhere there were no answers or recipes. I was already desperate, as I came across the following message.
File location regexp: ^([^ ]+) (\([0-9]*\)) .*$
(or say Output location regexp)
by applying this regexp on each line in the output, IDE will get array of two elements (when matched, of course). And one of the elements will be file name and the other is line number.
In other words, if we form the output line in accordance with the given regular expression, the IDE will understand that we give it information about the file and the line number in it. The task was to run CodeSniffer, retrieve data from it, reformat the output a bit and send the editor to the logs window, which is a trifle.
Now everything is in steps:
0. Set pear for php, which comes as part of PhpEd
c:\Program Files\NuSphere\PhpED\php5> go-pear.bat
and php_sniffer package
c:\Program Files\NuSphere\PhpED\php5> pear install php_sniffer
1. Swing
my little wrapper class over CodeSniffer. We put it, let's say, in
c:\Program Files\NuSphere\PhpED\scripts\
2. Open PhpEd.
Settings> Tools> Integration
Create a new menu "Scripts"
Create a new submenu "CodeSniffer"
3. Edit the created item:
- Execute with "Shell"
- Command line:
"c:\Program Files\NuSphere\PhpED\php5\php.exe" "c:\Program Files\NuSphere\PhpED\scripts\codesnifferwrapper.php" "@Fname@"
- Shortcut: CTL-ALT-0 (or any convenient)
- Put the crosses in the following paragraphs:
Show this command in Workspace popup – for files
Show this command in Explorer popup – for files
Show this command in File Bar popup
Redirect Output Stream to log window
Save the result, open any php file, click the assigned key combination and admire.
