Zend Code Analyzer in TextMate

Blog calendar

RSS feed from Michal Frackowiak's blog

subscribe to the RSS feed

— or —

get my blog posts via email

michal frackowiakmichal frackowiak
SquarkSquark
anjelanjel
Killer8845Killer8845
shark797039shark797039
Watch: site | category | page

Blog tags


View my profile on LinekdIn

My Twitter

1268137834|%e %b, %H:%M|agohover
bugs.wikidot.com - fixing bugs today. As many as we can. Report your own if you find one!

1268092513|%e %b, %H:%M|agohover
A Brief History of Pretty Much Everything - pretty brilliant www.youtube.com/watch?v=gNYZH9kuaYM

1268091732|%e %b, %H:%M|agohover
I have a feeling that Wikidot.com is now blazingly fast after server upgrade: blog.wikidot.com/blog:files-service-migrated

1268091379|%e %b, %H:%M|agohover
Inspiring story of Pandora - finally real revenue after 10 years: www.nytimes.com/2010/03/08/technology/08pandora.html

1267964484|%e %b, %H:%M|agohover
@antirez Switch to PostgreSQL? It has much more resonable locking mechanisms.

1267794675|%e %b, %H:%M|agohover
Power is back ;-) . Fun is over :-(

Photos

Blogroll

Piotr Gabryjeluk's blog (Gabrys on Wikidot)
Lukasz Tarka's blog (Squark on Wikidot)

Recent posts by my friends


Me in other networks:

facebook, last.fm, del.icio.us, Flickr, Flaker

« Back to the Blog

1261178760|%e %b, %H:%M (%O ago)

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:

20091218-qp7jyykgqytqu3depeak8rm88u.png

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.

20091218-bmaw41ddg1mhu4n3kej6rk8dhw.jpg

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:
20091218-p112kgt5j21xbwdedrhpu1cb8y.png

(choose any other keyboard shortcut if you wish)

  • Edit PHP Bundle menu and move the Analyze Code below Validate Code (pure esthetics)
20091218-x5y47sgdgyf942tjq1ts8afcai.png

5. Try it!

Let us try the analyzer on a simple code:

<?php
 
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

rating: 0+x

del.icio.usdiggSimpyRedditYahooMyWebFurl

Many thanks, but what about Studio 7.1?
Yegor (guest) 1266826284|%e %b %Y, %H:%M %Z|agohover

Great article, very helpful! Thank! But what about Zend Studio 7.1? What to do?

Reply  |  Options
Unfold Many thanks, but what about Studio 7.1? by Yegor (guest), 1266826284|%e %b %Y, %H:%M %Z|agohover
michal frackowiakmichal frackowiak 1267086190|%e %b %Y, %H:%M %Z|agohover

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

Reply  |  Options
Unfold by michal frackowiakmichal frackowiak, 1267086190|%e %b %Y, %H:%M %Z|agohover
Add a New Comment
Page tags: php textmate
asdad