Tuesday, 18 September 2007

PHP_CodeSniffer and the Zend Code Analyzer

Ever since I first used the Zend Code Analyzer, I've wanted to put it into our code sniffers, but I've never got around to doing it. Now, thanks to a contribution from Holger Kral of Zend, and a new config system in PHP_CodeSniffer, you can include ZCA warnings in your coding standards.

I've just committed the CodeAnalyzer sniff into the Zend standard under a new Debug category. The sniff requires you to have ZCA installed on the same machine as PHP_CodeSniffer, and you need to tell PHP_CodeSniffer where to find it.

$ phpcs --config-set zend_ca_path /path/to/ZendCodeAnalyzer
$ phpcs --standard=zend /path/to/file
Zend Code Analyzer produces warnings for things like unused function arguments and unreachable code. Not all the warnings need to be fixed, but it can certainly find some common mistakes in your code.

10 comments:

Amit said...

Just starting out with PHP code sniffer. What if I don't have 'Zend Code Analyzer'? Will I still be able to sniff the rest of the Zend standard?

Greg Sherwood said...

You certainly can use the rest of the standard. The Zend Code Analyzer is only used by one sniff. You will get warnings when you run PHP_CodeSniffer with the Zend coding standard if you don't have it configured, but that will probably be removed in a future release.

signs_logger said...

Hi!

How can I get ZendCodeAnalyzer without ZendStudio?
I need it for our development server on Linux, but I work at Windows version. And I cannot install Studio on server, as it requires WindowX for installation. (and it's clear that windowX isn't needed on dev server :) )

Greg Sherwood said...

The only way I have been able to get ZendCodeAnalyzer is to download the trial of Zend Studio and grab the executable from inside the archive. You don't need to install Zend Studio; just download the right copy for your OS.

Anonymous said...

Thanks for your article. I was able to get a ZendStudioForEclipse-6_0_1.tar.gz then to extract the ".bin" inside. But I can't see how to extract the ZendCodeAnalyzer from this ".bin". Any ideas?

Greg Sherwood said...

@anonymous
Whenever I have downloaded it I just found an executable file that I could run. I don't know anything more about ZendCodeAnaylzer.

Anonymous said...

@anonymous
You can get ZendCodeAnalyzer for linux from your Windows installation of Zend Studio. Path: ZendStudio\plugins\com.zend.php.codeanalyzer.linux.x86_6.1.0.v20080907\resources\ZendCodeAnalyzer or similar.

Anonymous said...

I downloaded zend studeo and also install to get code analyzer. But later i unstall the studeo and try to install code analyzer.Unfortunately, .exe file couldnot run and i couldn't have code analyzer in my system?

My next question:
How to link code analyzer with the code sniffer?

Anonymous said...

I need suggestion.
Please help me

Anonymous said...

By the way, I am using xampp.
In which location should I store the folder PHP_CodeSniffer-1.2.0RC1 in xampp?

So far concerned with code analyzer, i think it ix exe file and after i run it will open an application where we analyze the code.

While running code analyzer, how does it knows the code sniffer?