Blog calendar
— or —
... and more
Blog tags
18 Dec 2009 23:26
Zend Code Analyzer (part of Zend Studio) is a powerful tool that analyzes PHP scripts and detects errors and warnings possibly generated by your code. I have been using Zend Studio for a while, and I miss this feature a lot when working with TextMate. Fortunately, thanks to great extensibility of TM, one can include code analysis in the PHP bundle within just a few minutes.
After completing the steps below, you will be able to press a combination of keys and ZendCodeAnalyzer will analyze your PHP script and render its output like on the screen below:

And here is how.
1. Download Zend Studio (demo)
Go to http://www.zend.com/en/products/studio/downloads-prev and download a 30-day trial of Zend Studio 7.0.

You can skip this option if you already have Zend Studio, but since you are using TextMate — I bet you don't. You might need to create an account at Zend, but this is all free. Note that the newest Zend Studio 7.1 does not have a standalone ZendCodeAnalyzer binary, and cannot be (yet) used in TextMate.
2. Copy the binary file
One way to get ZendCodeAnalyzer is to install Zend Studio and look for the file in the installation path. But I do not like to waste my time installing stuff I will not need. Besides, we are interested just in one single executable file.
Open (mount) the downloaded dmg image. If you had just downloaded this, it should be mounted. Now paste those commands:
mkdir /tmp/zca
cd /tmp/zca
unzip /Volumes/ZendStudio\ 7.0.*/ZendStudio.app/Contents/Resources/Java/Disk1/InstData/Resource1.zip \
'$IA_PROJECT_DIR$/resources/Mac/plugins_zg_ia_sf.jar'
unzip '$IA_PROJECT_DIR$/resources/Mac/plugins_zg_ia_sf.jar' '*ZendCodeAnalyzer'
sudo cp `find . -name ZendCodeAnalyzer` /usr/local/bin/zca
sudo chmod a+x /usr/local/bin/zca
cd
rm -rf /tmp/zca
By doing so you end up having a zca executable in /usr/local/bin. Verify whether it works:
zca --help
4. Edit TextMate PHP bundle
Now you need to add a new command to the PHP bundle.
- Select Bundles » Bundle Editor » Edit Commands…
- Add a new command — I called mine Analyze Code
- Paste the following code:
#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/textmate'
result = `/usr/local/bin/zca #{ENV['TM_FILEPATH']} 2>&1`
puts result.gsub /^Analyzing.*?\n/m, ''
if result =~ /line (\d+)/
TextMate.go_to :line => $1
else
puts "No problems found"
end
- Set other options as on the screenshot:

(choose any other keyboard shortcut if you wish)
- Edit PHP Bundle menu and move the Analyze Code below Validate Code (pure esthetics)

5. Try it!
Let us try the analyzer on a simple code:
function foo($bar) { $a = 3; return "123" . $c; } echo foo("bar");
The code itself is broken in many places, although it does not have any syntax errors. Now try the code analyzer and you should see something like in the first image up there.
The command automatically moves the cursor to the first line that contains a problem.
Still missing
ZendCodeAnalyzer has been replaced by Semantic Analysis in Zend Studio 7.1 and completely integrated into Eclipse framework. It does not provide a standalone version. Perhaps in the future such a version will be available from Zend, but not now.
ZendCodeAnalyzer has some tuning options, mainly: --enable <warning-name>, --disable <warning-name>, --recursive for including required files and —include-path. Perhaps it would be interesting to play with those to provide more in-depth code analysis.
Show as Tool Tip might not be the best way to display output of analyzer, but I could not figure out anything smarter.
Adding code analysis to TextMate is IMHO a game changer, it can potentially save you many headaches caused by various coding mistakes one can make. Although the above installation uses only basic functions of ZCA, it proved to be a very useful addition to the PHP bundle.
If you also find it useful — please add your comment! I will highly appreciate any suggestions and improvements!
rating: 0, tags: php textmate
Great article, very helpful! Thank! But what about Zend Studio 7.1? What to do?
As far as I know there is no standalone ZendCodeAnalyzer executable and this functionality is hidden deeper in an Eclipse plugin. Unless there is an API to get to it (there was not at the time of writing), I cannot see any way to use it in TextMate.
But there is no problem in installing Zend Studio 7.1 on your Mac — the only thing TextMate needs is one independent file (ZendCodeAnalyzer, which you can copy e.g. to /usr/local/bin), the rest of Zend Studio 7.0 you can safely remove. There are no dependencies here.
Michał Frąckowiak @ Wikidot Inc.
Visit my blog at michalf.me
Thanks again, works good so far. Btw, do you know where I can get a full list of ZCA warnings to disable some of them?
Still useful now - you can still find the ZendCodeAnalyzer in the 6.x eclipse plug-in version (though zend studio 7.0 is sadly unobtainable from Zend), I guess there will be differences as it's 6.x? I'm guessing 5.2 would be the last version of PHP supported
Some useful zca warnings to disable?
disable var-use-before-def-global
disable bool-assign
—disable var-arg-unused
Great writing it is such a cool and nice idea thanks for sharing your post. I like your post very much. Thanks for your post.
Thanks for sharing this nice article. I read it completely and get some interesting knowledge from this. I again thanks for sharing such a nice blog.
Thanks for sharing this nice article. I read it completely and get some interesting knowledge from this. I again thanks for sharing such a nice blog.
Thanks for sharing this nice article. I read it completely and get some interesting knowledge from this. I again thanks for sharing such a nice blog.
Thanks for sharing this nice article. I read it completely and get some interesting knowledge from this. I again thanks for sharing such a nice blog.